niki niki
niki niki

Reputation: 187

.bat Batch file Command for windows 7

I have a .bat script. & In that script "set Targetfolder" & "set sourcefolder" commands are there. I want to remove the both commands & want to give these command after execution of the script. means It should be ask the targetfolder and sourceflder paths.

Because, I have n numbers of Task & different Targefolder & sourcefolder path, however script doing the same function & for those all tasks I would have to create scripts..so, I am thinking rather then to do this I can keep the contain same in the batch file and only can put the Targetfolder & sourcefolder path.

In short that script should ask Targetfolder & sourcefolder, after giving those path It should be complete the script.

So, could any one suggest that how can I do this? PLEASE

The script has the below containt :

@echo off
setlocal
set DateFolder=04.2013
set TargetFolder=F:\Financial\Data\%DateFolder%\Final Reports

:: copy the newest file from AccruntPnLMTD and rename it to PNL.csv
call :copyAndRename "F:\Financial\Data\Reports\AccruntPnLMTD" "%TargetFolder%\PNL.csv"

:: copy the newest file from AccountPnlMTD and rename it to AC.csv
call :copyAndRename "F:\Financial\Data\Reports\AccountPnlMTD" "%TargetFolder%\AC.csv"

:: copy the newest file from ExpensesMTD and rename it to EXPMTD.csv
call :copyAndRename "F:\Financial\Data\Reports\ExpensesMTD" "%TargetFolder%\EXPMTD.csv"

:: copy the newest file from ExpensesYTD and rename it to EXPYTD.csv
call :copyAndRename "F:\Financial\Data\Reports\ExpensesYTD" "%TargetFolder%\EXPYTD.csv"

:: copy the newest file from AccrualPnLYTD and rename it to PNLYTD.csv
call :copyAndRename "F:\Financial\Data\Reports\AccrualPnLYTD" "%TargetFolder%\PNLYTD.csv"

:: copy the newest file from AccountYTD and rename it to ACYTD.csv
call :copyAndRename "F:\Financial\Data\Reports\AccountYTD" "%TargetFolder%\ACYTD.csv"

:: copy the newest file from BalanceMTD and rename it to BSMTD.csv
call :copyAndRename "F:\Financial\Data\Reports\BalanceMTD" "%TargetFolder%\BSMTD.csv"

:: copy the newest file from BalanceYTD and rename it to BSYTD.csv
call :copyAndRename "F:\Financial\Data\Reports\BalanceYTD" "%TargetFolder%\BSYTD.csv"

:: copy the newest file from FinancialStmtMTD and rename it to FSMTD.csv
call :copyAndRename "F:\Financial\Data\Reports\FinancialStmtMTD" "%TargetFolder%\FSMTD.csv"

:: copy the newest file from FinancialStmtYTD and rename it to FSYTD.csv
call :copyAndRename "F:\Financial\Data\Reports\FinancialStmtYTD" "%TargetFolder%\FSYTD.csv"


:: Done
goto :eof

:copyAndRename
set SourceFolder=%~1
set TargetFile=%~2

:: Find the newest file in the source folder
for /f "tokens=*" %%F in ('dir /b /od /a-d "%SourceFolder%"') do set "NewestFile=%%F"

:: copy and rename it to the target
copy "%SourceFolder%\%NewestFile%" "%TargetFile%"


:: Done with this subroutine
goto :eof

Upvotes: 2

Views: 4823

Answers (2)

Matt Williamson
Matt Williamson

Reputation: 7095

Well, you could use SET /P and have to type in the source and target folders manually each time or you could use my BrowseFolder routine and pick it from a dialog. Give this a try.

@echo off
setlocal

Call :BrowseFolder "Choose Target folder" "F:\Financial\Data\"
Set TargetFolder=%Result% 

Call :BrowseFolder "Choose Source folder" "F:\Financial\Data\Reports\"
Set SourceFolder=%Result% 


:: copy the newest file from AccruntPnLMTD and rename it to PNL.csv
call :copyAndRename %SourceFolder% "%TargetFolder%\PNL.csv"

:: copy the newest file from AccrualPnLMTD and rename it to AC.csv
call :copyAndRename "%SourceFolder%" "%TargetFolder%\AC.csv"

:: Done
goto :eof

:copyAndRename
set SourceFolder=%~1
set TargetFile=%~2

:: Find the newest file in the source folder
for /f "tokens=*" %%F in ('dir /b /od /a-d "%SourceFolder%"') do set "NewestFile=%%F"

:: copy and rename it to the target
copy "%SourceFolder%\%NewestFile%" "%TargetFile%"

:: Done with this subroutine
goto :eof


:BrowseFolder
set Result=
set vbs="%temp%\_.vbs"
set cmd="%temp%\_.cmd"
for %%f in (%vbs% %cmd%) do if exist %%f del %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
>%vbs% echo set WshShell=WScript.CreateObject("WScript.Shell") 
>>%vbs% echo set shell=WScript.CreateObject("Shell.Application") 
>>%vbs% echo set f=shell.BrowseForFolder(0,%1,0,%2) 
>>%vbs% echo if typename(f)="Nothing" Then  
>>%vbs% echo wscript.echo "set Result=Dialog Cancelled" 
>>%vbs% echo WScript.Quit(1)
>>%vbs% echo end if 
>>%vbs% echo set fs=f.Items():set fi=fs.Item() 
>>%vbs% echo p=fi.Path:wscript.echo "set Result=" ^& p
cscript //nologo %vbs% > %cmd%
for /f "delims=" %%a in (%cmd%) do %%a
for %%f in (%vbs% %cmd%) do if exist %%f del %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
goto :eof

Upvotes: 1

Daniel Morritt
Daniel Morritt

Reputation: 1817

Maybe it's easier to use %1 and %2 in your batch file, like

set Targetfolder = %1
set sourcefolder = %2

Then call the file like this:

file.bat path1 path2

You can then just create a shortcut with the right values in (or several shortcuts for your different needs).

Of course there's no checking in the above, might want to add that.

Upvotes: 1

Related Questions