![](/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.