.bat批处理文件Windows 7的命令

时间:2013-05-14 10:50:17

标签: batch-file

我有一个.bat脚本。 &安培;在那个脚本"设置Targetfolder" &安培; "设置sourcefolder"命令在那里。 我想删除这两个命令&想在执行脚本后给出这些命令。意味着它应该询问目标文件夹和源文件路径。

因为,我有n个任务&不同的Targefolder&源文件夹路径,但脚本执行相同的功能&对于那些我将不得不创建脚本的所有任务。所以,我想要做到这一点我可以在批处理文件中保持包含相同,只能放置目标文件夹和放大器。源文件夹路径。

简而言之,脚本应该询问Targetfolder& sourcefolder,在给出那些路径后应该完成脚本。

那么,任何人都可以建议我该怎么做?请

该脚本具有以下内容:

@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

2 个答案:

答案 0 :(得分:1)

也许在批处理文件中使用%1和%2更容易,例如

set Targetfolder = %1
set sourcefolder = %2

然后像这样调用文件:

file.bat path1 path2

然后,您可以创建一个具有正确值的快捷方式(或根据您的不同需求提供多个快捷方式)。

当然,上面没有检查,可能想补充一点。

答案 1 :(得分:1)

好吧,您可以使用SET /P并且每次都必须手动输入源文件夹和目标文件夹,或者您可以使用我的BrowseFolder例程并从对话框中选择它。试一试。

@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