我正在嘗試在 Batch 中制作一個腳本,以便在每個包含具有某種擴展名的檔案的檔案夾中制作一個 zip,但問題是 7-zip zips 無論如何。即使他沒有找到具有良好擴展名的檔案,他也會制作一個空 zip。你有低于我已經做過的。
setlocal enableDelayedExpansion
set /p ext="Write with the . which extension you want to zip. : "
set "currentdir="
for /f "delims=" %%b in ('dir /b /s /a-d "C:\Users\546802\Desktop\Test"') do (
if "!currentdir!" neq "%%~dpb" (
set "currentdir=%%~dpb"
for /d %%c IN ("%%~dpb.") do "c:\Program Files\7-Zip\7z.exe" a -mx "%%~dpb%%~nxc.zip" %%c\*%ext% )
)
我該如何解決?7-zip 是否有避免這種情況的命令?我閱讀了多個關于 7-zip 命令的主題,但我沒有找到滿足我需要的命令。
uj5u.com熱心網友回復:
此 ZIP 存檔檔案創建任務的批處理檔案是:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FileExtension="
rem Prompt the user in a loop until entering really a file extension.
rem Pressing just RETURN or ENTER results in prompting the user again.
rem Double quotes are always removed from input string and there must
rem be entered something else than just straight double quotes one or
rem more times. A dot at beginning of the file extension is always
rem removed and there must be entered more than just one dot. The file
rem extension entered by the user cannot contain / or \ or . or any
rem other character not allowed in a file extension according to the
rem definition by Microsoft.
:PromptUser
set /P "FileExtension=Enter the file extension to zip: " || goto PromptUser
set "FileExtension=%FileExtension:"=%"
if not defined FileExtension goto PromptUser
if "%FileExtension:~0,1%" == "." set "FileExtension=%FileExtension:~1%"
if not defined FileExtension goto PromptUser
set "FailedSyntaxCheck=1"
for /F "delims=*./:<>?\|" %%I in ("%FileExtension%") do if not "%%I" == "%FileExtension%" (goto PromptUser) else set "FailedSyntaxCheck="
if defined FailedSyntaxCheck goto PromptUser
for /F "delims=" %%I in ('dir "%USERPROFILE%\Desktop\Test" /AD-L /B /S 2^>nul') do if exist "%%I\*.%FileExtension%" "%ProgramFiles%\7-Zip\7z.exe" a -bso0 -bsp0 -mx=9 -r- -tzip -y -- "%%I\%%~nxI.zip" "%%I\*.%FileExtension%"
endlocal
批處理檔案不是100% 故障安全的。檔案擴展名語法驗證不是 100%。因此,用戶仍然可以輸入對檔案擴展名無效的字串,如 Microsoft 在有關Naming Files、Paths 和 Namespaces的檔案頁面上所述。
如果存在具有名稱的目錄Test.txt并且用戶輸入.txt或只是txt作為檔案擴展名,則此代碼中使用的簡單IF條件為真,盡管Test.txt是檔案夾而不是檔案,因此仍然執行7-Zip 。如果也應處理此類用例,則可以改進代碼以使條件更準確。
使用的7-Zip開關在7-Zip的幫助中有所描述。雙擊7-Zip7-zip.chm程式檔案夾中的檔案打開幫助,單擊串列項Command Line Version上的第一個選項卡Contents并閱讀所有有關命令列語法、 命令和開關的參考幫助頁面。
在包含具有指定檔案擴展名的檔案的檔案夾中創建 ZIP 檔案。可以在包含具有指定檔案擴展名的檔案的目錄的父目錄中創建 ZIP 檔案,替換"%%I\%%~nxI.zip"為"%%I.zip". 該問題不包含帶有目錄樹的清晰資訊,其中包含使用用戶輸入執行批處理檔案之前和之后的檔案,txt或者.cmd真正了解此 ZIP 存檔檔案創建任務的所有要求。
要了解使用的命令及其作業原理,請打開命令提示符視窗,在其中執行以下命令,并仔細閱讀每個命令顯示的所有幫助頁面。
dir /?echo /?endlocal /?for /?goto /?rem /?set /?setlocal /?
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/491638.html
下一篇:以編程方式收藏按鈕Swift
