這是一個批處理代碼,爲這個簡單的任務提供額外的獎勵:
工作目錄可以作爲第一個參數傳遞給批處理文件。
如果批處理文件不帶參數啓動,則使用當前目錄。
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "WorkingDirectory="
if "%~1" == "" goto ArchiveFiles
set "WorkingDirectory=%~1"
if "%WorkingDirectory:~-1%" == "\" (
if exist "%WorkingDirectory%*" pushd "%WorkingDirectory%" & goto ArchiveFiles
) else (
if exist "%WorkingDirectory%\*" pushd "%WorkingDirectory%" & goto ArchiveFiles
)
echo Directory "%WorkingDirectory%" does not exist.
endlocal
goto :EOF
:ArchiveFiles
for /F "delims=" %%I in ('dir /A-D /B * 2^>nul') do (
if /I not "%%~xI" == ".rar" (
"%ProgramFiles%\WinRAR\Rar.exe" a [email protected] -cfg- -ep1 -idq -m5 -ma4 "-pPassword" -r- -s- -y -- "%%~nI.rar" "%%~fI"
)
)
if not "%WorkingDirectory%" == "" popd
endlocal
此批處理文件忽略目錄中已存在的* .rar文件。
打開Program Files中WinRAR的上使用的命令a
細節和使用的開關文本文件Rar.txt的文件夾中。
爲了解所使用的命令及其工作方式,請打開命令提示符窗口,在其中執行以下命令,並仔細閱讀爲每個命令顯示的所有幫助頁面。
call /?
...解釋%~1
dir /?
echo /?
endlocal /?
for /?
goto /?
if /?
popd /?
pushd /?
set /?
setlocal /?
閱讀也是微軟的文章關於Using Command Redirection Operators解釋2>nul
其中在該代碼重定向操作>
必須插入符號^
進行轉義是第一個解釋爲文字字符解析FOR命令行和作爲重定向操作符執行DIR作者:對於。
並閱讀Single line with multiple commands using Windows batch file的答案,以瞭解運算符&
的含義,如此處在兩條命令行中所使用的。
請注意,https://stackoverflow.com不是一個免費的腳本/代碼寫入服務。如果您告訴我們您迄今爲止所嘗試的內容(包括您已經使用的腳本/代碼)以及您卡住的位置,那麼我們可以嘗試幫助解決特定問題。您還應該閱讀[我如何提出一個好問題?](https://stackoverflow.com/help/how-to-ask)。 – DavidPostill