![](/img/trans.png)
[英]How to open and list contents of a zip file using windows command prompt?
[英]Auto Open ZIP File Contents After Download
我正在嘗試編寫一個批處理文件,該文件將在下載 zip 文件后由 filewatcher 執行。 下載后解壓同名文件,然后刪除 zip 文件,只留下文件夾和一個 PDF 文件。 我只需要命令來為解壓縮的同一個文件完成此操作。
7z x -oC:\Users\"user"\Downloads\* C:\Users\"user"\Downloads\*.zip
del C:\Users\"user"\Downloads\*.zip
"command opening the file in the unzipped folder"
exit
使用FOR ,如果您有多個 zip 文件,它將遍歷所有文件。
@echo off
for %%I in ("%UserProfile%\Downloads\*.zip") do (
"%ProgramFiles%\7-Zip\7z.exe" e -aoa "-o%%~dpI" -y -- "%%I"
if not errorlevel 1 (
del "%%I"
if exist "%%~dpnI.pdf" start "" "%%~dpnI.pdf"
)
)
這個腳本說,這是下載路徑。 對於此路徑中的每個 PDF,解壓縮並刪除 zip,打開任何已解壓縮的 PDF。
@echo off
setlocal
:: Start in this dir.
cd /d "%userprofile%\Downloads" || exit /b 1
:: Dir where 7z extracts to.
set "extractdir=unzip_tmp"
:: Zips move here if files or folders exist in cd.
set "faildir=unzip_fail"
rem Continue only if zip files exist.
if not exist *.zip (
echo No zip files
exit /b 0
)
for %%A in (*.zip) do (
set "fail="
rem Unzip with 7z.
echo Unzip: "%%~nxA"
7z x -o"%extractdir%" "%%~nxA" >nul
if not errorlevel 1 (
pushd "%extractdir%" && (
rem Check if files or folders exist in parent dir.
for /f "delims=" %%B in ('dir /b') do (
if exist "..\%%~B" (
echo Exist: "..\%%~B"
set "fail=defined"
)
)
rem Open PDF file.
for /r %%B in (*.pdf) do (
echo Open: "%%~nxB"
start "" /wait "%%~fB"
)
rem Move files or folders to parent dir.
if not defined fail for /f "delims=" %%B in ('dir /b') do (
echo Move: "%%~B"
move "%%~B" .. >nul
)
popd
)
rem Cleanup.
if defined fail (
if not exist "%faildir%" (
echo Create: "%faildir%"
md "%faildir%"
)
echo Remove: "%extractdir%"
rd "%extractdir%" /s /q
echo Move: "%%~nxA"
move "%%~nxA" "%faildir%" >nul
) else (
echo Remove: "%%~nxA"
del "%%~nxA" >nul
)
)
)
rem Final cleanup.
if exist "%extractdir%" (
echo Remove: "%extractdir%"
rd "%extractdir%" /s /q
)
這使用預設目錄將 zip 文件解壓縮到。 名為extractdir
的變量具有要使用的文件夾的名稱。 預設目錄允許在獨立路徑中搜索 PDF 文件。
該腳本非常口頭,因此可以看到解壓縮,移動等內容。
代碼操作:
Downloads
目錄。%extractdir%
。fail
定義。fail
,則將文件和文件夾移動到父目錄。%extractdir%
到和移動zip文件%faildir%
如果失效被定義。%extractdir%
如果存在,這樣它就不會在下次運行時%extractdir%
。對於那些想嘗試沒有依賴關系的快速單行代碼的人來說,這將取決於調用 cmd 行的文件監視器(很多人都可以這樣做)
您可以根據需要添加命令以轉到工作下載目錄,例如,如果此頁面在 zip 中作為 PDF 運行
forfiles /m *.zip /C "cmd /c md @fname && move @fname.zip @fname && cd /d @fname && tar -mxf @fname.zip && start \"\" @fname.pdf"
要刪除 zip,請將&& del @fname.zip
添加到上面最后一個"
之前的末尾
幾乎不需要錯誤檢查,因為它的條件&&
所以不應該覆蓋,第一次中止應該是
子目錄或文件blah blah已經存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.