2013-05-14 63 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