简体   繁体   中英

Powershell unzip file with .zip extension and move it to another folder

I hope some of you can help me. I got stuck modifying a powershell script.

The script checks for a zip file (file name is a fix value) in a specific folder (origin) and moves it to another folder (destination), however I need a script which checks for the .zip extension, not a fix value and moves it to another folder as well. I'm using this script at the moment:

powershell.exe -nologo -noprofile -command "& { $shell = New-Object -COM Shell.Application; $target = $shell.NameSpace('D:\Anlagen'); $zip = $shell.NameSpace('C:\Temp\Rechnungen\Outlook'); $target.CopyHere($zip.Items(), 16); }"

As you can see I need this script as a batch.file.

Use Expand-Archive to unzip the file to a directory, and then from your batch script, copy the files somewhere else. If you need to do this from a batch script:

powershell.exe -c "Expand-Archive -Path 'C:\path\to\archive.zip' -DestinationPath 'C:\unzip\directory'"
xcopy /s /e /t "C:\unzip\directory" "C:\final\destination\directory"

Note that UNC paths should also work with either command, not just local paths.

If you have 7-Zip:

set 7zip="C:\Program Files\7-Zip\7z.exe"

IF EXIST "path\to\file.zip" (

    %7zip% x -o"path\to\new\folder" "path\to\file.zip"
)

The following line will search recursively through subfolders for any zip file. In the example below, the script will begin the recursive search at the root of C:. The path to the file will be saved as a variable, in order to be called later.

for /f "tokens=*" %%x in ('forfiles /p C:\ /m *.zip /s /c "cmd /c echo @path"') do if exist %%x (

    %7zip% x -o"path\to\new\folder" %%x
)

Another way to search recursively through multiple drive letters is to set the drive letters as variables in a FOR loop. The following example will check to see if the drive letter exists, then search the entire directory for a zip file. Example:

for %%i in (c:\, d:\, e:\, f:\, <enter as many as needed>) do if exist %%i (

    for /f "tokens=*" %%x in ('forfiles /p %%i /m *.zip /s /c "cmd /c echo @path"') do if exist %%x (

        %7zip% x -o"path\to\new\folder" %%x
    )
)

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