2

我有一个 .bat 脚本。& 在那个脚本中,有“set Targetfolder”和“set sourcefolder”命令。我想删除这两个命令并希望在执行脚本后给出这些命令。意味着应该询问目标文件夹和源文件夹路径。

因为,我有 n 个任务和不同的目标文件夹和源文件夹路径,但是脚本执行相同的功能并且对于那些我必须创建脚本的所有任务..所以,我想而不是这样做,我可以保持包含相同在批处理文件中,只能放置 Targetfolder & sourcefolder 路径。

简而言之,在给出这些路径之后,该脚本应该询问 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
4

2 回答 2

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
于 2013-05-15T11:28:08.177 回答
1

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

set Targetfolder = %1
set sourcefolder = %2

然后像这样调用文件:

file.bat path1 path2

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

当然上面没有检查,可能要添加。

于 2013-05-14T11:09:41.523 回答