我建議你的任務如下評論批處理文件:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
goto UserPrompt
rem Define environment variable BaseFolder with a double quote character as
rem value in case of the user enters nothing on prompt in which case the
rem variable BaseFolder is still defined with the double quote as value.
rem Then let the user enter the folder path or drag and drop
rem the folder over the console window to enter the path.
rem Next remove all double quotes from folder path and test
rem if the variable BaseFolder still exists with a value.
rem Last replace forward slashes by backslashes in case of user entered
rem the path with forward slashes, make sure the folder path does not
rem end with a backslash and test if the folder really exists in case of
rem user has made a typing mistake on entering manually the folder path.
rem Run the backup and repair operation if entered folder exists as expected.
:UserPrompt
cls
echo/
echo Please type the database base folder path and press ENTER.
echo/
echo Or alternatively drag ^& drop the folder from Windows
echo Explorer on this console window and press ENTER.
echo/
set "BaseFolder=""
set /P "BaseFolder=Path: "
set "BaseFolder=!BaseFolder:"=!"
if "!BaseFolder!" == "" goto UserPrompt
set "BaseFolder=!BaseFolder:/=\!"
if "!BaseFolder:~-1!" == "\" set "BaseFolder=!BaseFolder:~0,-1!"
if "!BaseFolder!" == "" goto UserPrompt
echo/
if not exist "!BaseFolder!\Database\*" (
echo There is no folder "!BaseFolder!\Database".
echo/
choice "Do you want to enter the path once again "
if errorlevel 2 goto ExitBatch
goto UserPrompt
)
set "BackupFolder=%BaseFolder%\Database\RTBackup%DATE:~10,4%_%DATE:~4,2%_%DATE:~7,2%_%TIME:~0,2%%TIME:~3,2%"
rem For German date/time format which is for DATE TIME: dd.mm.yyy hh:mm:ss,xx
rem set "BackupFolder=%BaseFolder%\Database\RTBackup%DATE:~-4%_%DATE:~-7,2%_%DATE:~-10,2%_%TIME:~0,2%%TIME:~3,2%"
if exist "%BackupFolder%\*" goto MakeBackup
md "%BackupFolder%"
if errorlevel 1 (
echo/
echo Error: Failed to create backup folder !BackupFolder!
echo/
choice "Repair without making a backup "
if errorlevel 2 goto ExitBatch
goto RunRepair
)
:MakeBackup
echo Making a backup to folder !BackupFolder! ...
move /-y "%BaseFolder%\Database\*.dbf" "%BackupFolder%" 2>nul
move /-y "%BaseFolder%\Database\*.cdx" "%BackupFolder%" 2>nul
move /-y "%BaseFolder%\Database\*.~cd" "%BackupFolder%" 2>nul
move /-y "%BaseFolder%\Database\*.~db" "%BackupFolder%" 2>nul
move /-y "%BaseFolder%\Database\*.fpt" "%BackupFolder%" 2>nul
move /-y "%BackupFolder%\zipdata.dbf" "%BaseFolder%\Database\" 2>nul
:RunRepair
echo/
echo Running database repair ...
"%BaseFolder%\dbrepair.exe"
:ExitBatch
endlocal
請先閱讀答案上Why is no string output with 'echo %var%' after using 'set var = text' on command line?解釋set variable="value"
和set "variable=value"
之間的差異。
變量Datefolder
是在用雙引號問題批處理文件創建的環境包括在環境變量的值導致擴大
"%letter%:\UTS16\Database\RTBackup%Datefolder%"
例如以
"C:\UTS16\Database\RTBackup"2017_01_13_1250""
這當然是不好的。雙引號字符串中的雙引號通常不正確。
和命令行
mkdir %letter%:\UTS16\Database\"RTBackup%Datefolder%"
擴大例如以
的Windows
mkdir C:\UTS16\Database\"RTBackup"2017_01_13_1250""
糾錯必須做加班修復的文件夾路徑。
環境變量DATE
和TIME
的日期/時間格式取決於當前用戶的Windows區域和語言設置。我需要一個不同的行來爲我的德語Windows機器定義名稱爲日期和時間的備份文件夾。有在How to get current datetime on Windows command line, in a suitable format for using in a filename?貼出例如但是區域獨立的解決方案,如果使用環境變量DATE
並且在使用這個批處理文件的計算機上TIME
工作更快命令行,沒有必要更換一個區域的獨立解決方案,行。
在此答案批處理文件使用環境變量的延遲擴展主要是防止批量處理的出口中的情況下,用戶錯誤地進入這導致沒有語法錯誤延遲擴展的使用的路徑串。
用戶可以從Windows資源管理器在控制檯窗口按提示輸入文件夾路徑拖動&下降的文件夾也爲例子。
echo/
用於輸出一個空行作爲由DosTips論壇話題解釋這是優於echo.
ECHO. FAILS to give text or blank line - Instead use ECHO/
的符號&
由Windows命令解釋解釋爲操作者在一個不帶引號在一個命令行執行多個命令串。因此,必須將脫字符^
的這個字符解釋爲文字字符以回顯到控制檯窗口。見Single line with multiple commands using Windows batch file上的&
,在命令行&&
和||
詳細含義在加引號的字符串,而不是與^
逃脫。
命令choice
用於在詢問用戶是否希望繼續在錯誤或退出該批處理文件。此命令附加在方括號中的提示文字中,按下的鍵爲是或否用Windows語言和問號,即英語Windows上的[Y,N]?
或德語Windows上的[J,N]?
。退出前choice不允許任何其他鍵。分配給errorlevel
退出代碼是2
上沒有和1
上是。
Microsoft支持文章Testing for a Specific Error Level in Batch Files解釋了使用if errorlevel
來測試以前的命令或應用程序的退出代碼。在這種情況下,在errorlevel
大於或等於2的情況下進行測試就足夠了,以便在用戶選擇否的情況下退出批處理。
批處理文件不檢查它試圖執行這個應用程序之前,如果"%BaseFolder%\dbrepair.exe"
確實存在。如果在添加用戶的適當錯誤消息的情況下進行額外的檢查,那麼最好在創建備份文件夾之前進行。
爲了解所使用的命令及其工作方式,請打開命令提示符窗口,在其中執行以下命令,並仔細閱讀爲每個命令顯示的所有幫助頁面。
choice /?
cls /?
echo /?
endlocal /?
goto /?
if /?
md /?
move /?
set /?
setlocal /?
而且還讀的2>nul
解釋Microsoft TechNet文章Using command redirection operators。
您用於驅動器號的代碼相同。 – Squashman
好吧,我可以通過使用與驅動器號相同的代碼來確定文件夾的年份。感謝您的提示。對不起,但我真的很新,而且我試圖在飛行中找出它。現在我無法使用「%Datefolder%」在其創建的RTBackup文件夾末尾顯示日期和時間。我只是想讓它顯示它創建的日期和時間。 –
選擇此命令行主要來自您的批處理文件:'echo%date:〜10,4%_%date:〜4,2%_%date:〜7,2%_%time:〜0.2 %%時間:〜3,2%',並用Ctrl + C將其複製到剪貼板。打開命令提示符窗口,通過右鍵單擊粘貼複製的行到控制檯窗口中,左鍵單擊上下文菜單中的__Paste__,然後按ENTER或RETURN鍵運行此命令。它不是你期望的輸出,運行命令'echo%DATE%%TIME%'並查看輸出日期和時間。 – Mofi