2013-05-14 62 views
2

我有一个.bat脚本。 &在该脚本“设置目标文件夹”&“set sourcefolder”命令在那里。 我想删除这两个命令&想要在脚本执行后给这些命令。意味着它应该问问目标文件夹和sourceflder路径。.bat Windows 7的批处理文件命令

因为,我有任务的n个不同& & Targefolder路径sourcefolder但是脚本做同样的功能&对于那些所有的任务,我将不得不创造scripts..so,我想而不是要做到这一点,我可以保持批处理文件中的内容相同,并且只能放置目标文件夹源文件夹路径。

总之,脚本应该问问目标文件夹& sourcefolder,在给出那些路径后应该是完整的脚本。

那么,有没有人可以建议我该怎么做?请

脚本有以下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 
+0

你应该澄清你的问题,它几乎是不可读的。 – Endoro 2013-05-14 11:48:39

+0

嗨Endoro,我已经更新了包含的脚本.. – 2013-05-14 15:02:40

+0

好的。是否重命名为PNL.csv的最新文件的源文件夹始终为F:\ Financial \ Data \ Reports \ AccruntPnLMTD?列出的其他文件也是如此。 – 2013-05-20 17:37:23

回答

1

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

set Targetfolder = %1 
set sourcefolder = %2 

然后调用这样的文件:

file.bat path1 path2 

然后,您可以创建一个快捷方式,其中正确的值(或几个快捷方式为您的dif不同的需求)。

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

+0

@echo off setlocal set TargetFolder =%1,set sourcefolder =%2 ...把​​命令,但是我需要更新“file.bat path1 path2? – 2013-05-14 11:31:47

+0

%1和%2是你提供的参数当你运行这个批处理文件的时候,你必须从'CMD'提示符下运行'.bat'文件,或者像Daniel提到的那样创建一个批处理文件的快捷方式,然后编辑它以添加源文件夹和目标文件夹。你想双击运行批处理文件,然后指定源文件夹和目标文件夹吗? – 2013-05-14 12:09:41

+0

您好Daniel Morritt..I使用set Targetfolder =%1&set sourcefolder =%2更新批处理文件。现在,我应该重命名文件名“file.bat path1 path2”&我怎样才能创建一个右键的快捷方式?..对不起,再次打扰你..你可以描述更多..thx – 2013-05-15 11:34:05

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 
+0

嗨马特,...我按照你的建议做同样的工作,但它不工作..但是,对话框出现了,我给出了正确的道路。但它给出了下面的错误:找不到文件系统找不到指定的路径。 – 2013-05-15 14:33:04

+0

如上所述更改代码以添加Echo。从'CMD'提示符运行脚本并发布结果。直接从'CMD'窗口复制并粘贴它们。 – 2013-05-15 16:17:07

+0

嗨马特..静静地工作,并没有错误按摩这一次。它只是询问两条路径。它也应该是第二条线.B'z有两个文件需要复制和重命名。 – 2013-05-17 10:59:07