简体   繁体   中英

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. 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. 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.

@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.

@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. The variable named extractdir has the name of the folder to use. A preset directory allows the ability to search for PDF files in an isolated path.

The script is quite verbal so can see what is unzipped, moved etc.

Code operation:

  • Change directory to Downloads directory.
  • Exit if no zip files.
  • Search for zip file.
  • Extract from zip file to %extractdir% .
  • Check if extracted files and folders exist in parent directory. If so, set fail to defined.
  • Search recursively for PDF files and open them.
  • if fail is not defined, move files and folders to parent directory.
  • Cleanup by deleting zip file or removing %extractdir% and moving the zip file to %faildir% if fail is defined.
  • Finally remove %extractdir% if exist so it cannot foul up next run.

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)

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

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 "

在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM