[英]Auto Open ZIP File Contents After Download
I'm trying to write a batch file that will be executed by a filewatcher once a zip file is downloaded.我正在尝试编写一个批处理文件,该文件将在下载 zip 文件后由 filewatcher 执行。 After download the file is unzipped with the same name, then the zip file is deleted leaving only the file folder and a single PDF file inside.
下载后解压同名文件,然后删除 zip 文件,只留下文件夹和一个 PDF 文件。 I just need the command to complete this action for the same file that is unzipped.
我只需要命令来为解压缩的同一个文件完成此操作。
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
Use FOR , if you have multiple zip files, it will iterate over them all.使用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"
)
)
This script says, here is the download path.这个脚本说,这是下载路径。 For each PDF in this path, unzip and then delete the zip, open any PDF that was unzipped.
对于此路径中的每个 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
)
This uses a preset directory to extract the zip files to.这使用预设目录将 zip 文件解压缩到。 The variable named
extractdir
has the name of the folder to use.名为
extractdir
的变量具有要使用的文件夹的名称。 A preset directory allows the ability to search for PDF files in an isolated path.预设目录允许在独立路径中搜索 PDF 文件。
The script is quite verbal so can see what is unzipped, moved etc.该脚本非常口头,因此可以看到解压缩,移动等内容。
Code operation:代码操作:
Downloads
directory.Downloads
目录。%extractdir%
.%extractdir%
。fail
to defined.fail
定义。fail
is not defined, move files and folders to parent directory.fail
,则将文件和文件夹移动到父目录。%extractdir%
and moving the zip file to %faildir%
if fail is defined.%extractdir%
到和移动zip文件%faildir%
如果失效被定义。%extractdir%
if exist so it cannot foul up next run.%extractdir%
如果存在,这样它就不会在下次运行时%extractdir%
。For those that want to try a fast one liner without dependencies this will depend on the file monitor calling the cmd line (as many can do)对于那些想尝试没有依赖关系的快速单行代码的人来说,这将取决于调用 cmd 行的文件监视器(很多人都可以这样做)
you can add a command to go to a working download directory as required then for example if this page was in the zip as PDF run您可以根据需要添加命令以转到工作下载目录,例如,如果此页面在 zip 中作为 PDF 运行
forfiles /m *.zip /C "cmd /c md @fname && move @fname.zip @fname && cd /d @fname && tar -mxf @fname.zip && start \"\" @fname.pdf"
To delete the zip add && del @fname.zip
onto the end of above before the last "
要删除 zip,请将
&& del @fname.zip
添加到上面最后一个"
之前的末尾
There should be little need for error checks as its conditional &&
so should not overwrite, the first abort should be几乎不需要错误检查,因为它的条件
&&
所以不应该覆盖,第一次中止应该是
A subdirectory or file blah blah already exists.
子目录或文件blah blah已经存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.