简体   繁体   English

下载后自动打开 ZIP 文件内容

[英]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:代码操作:

  • Change directory to Downloads directory.将目录更改为Downloads目录。
  • Exit if no zip files.如果没有 zip 文件则退出。
  • Search for zip file.搜索 zip 文件。
  • Extract from zip file to %extractdir% .从 zip 文件中提取到%extractdir%
  • Check if extracted files and folders exist in parent directory.检查提取的文件和文件夹是否存在于父目录中。 If so, set fail to defined.如果是这样,设置fail定义。
  • Search recursively for PDF files and open them.递归搜索 PDF 文件并打开它们。
  • if fail is not defined, move files and folders to parent directory.如果未定义fail ,则将文件和文件夹移动到父目录。
  • Cleanup by deleting zip file or removing %extractdir% and moving the zip file to %faildir% if fail is defined.清理通过删除zip文件或删除%extractdir%到和移动zip文件%faildir%如果失效被定义。
  • Finally remove %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.

相关问题 如何使用Windows命令提示符打开和列出zip文件的内容? - How to open and list contents of a zip file using windows command prompt? 打开并打印文件的内容? - Open and print the contents of a file? 如何在IE 11的新标签页中自动打开下载的文件或Chrome中的边缘式文件下载功能 - How to auto open downloaded file in new tab in IE 11 or edge like file download work in chrome 从Amazon S3下载后无法提取大型zip文件 - Unable to extract large zip file after download from Amazon S3 Python:Crossplatform代码,用于下载有效的.zip文件 - Python: Crossplatform code to download a valid .zip file PowerShell 从 GitHub API 下载 Zip 文件 - PowerShell to download Zip file from GitHub API 将文件夹内容复制到创建的.zip文件:'找不到文件或没有读取权限' - Copy folder contents to a created .zip file: 'file not found or no read permissions' 将zip文件内容解压缩到Python 2.7中的特定目录 - Extracting zip file contents to specific directory in Python 2.7 有没有一种方法可以基于密码在zip文件中生成不同的内容? - Is there a way to generate different contents in a zip file based on a password? 如何创建 .BAT 文件以下载和解压 zip 文件? - How to create a .BAT file to download and unpack a zip file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM