簡體   English   中英

Powershell 解壓縮擴展名為 .zip 的文件並將其移動到另一個文件夾

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

我希望你們中的一些人可以幫助我。 我在修改 powershell 腳本時遇到了困難。

該腳本檢查特定文件夾(來源)中的 zip 文件(文件名是一個固定值)並將其移動到另一個文件夾(目的地),但是我需要一個檢查 .zip 擴展名的腳本,而不是一個固定值和也將其移動到另一個文件夾。 我現在正在使用這個腳本:

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); }"

如您所見,我需要將此腳本作為batch.file。

使用Expand-Archive將文件解壓縮到一個目錄,然后從批處理腳本中將文件復制到其他位置。 如果您需要從批處理腳本執行此操作:

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"

請注意,UNC 路徑也應與任一命令一起使用,而不僅僅是本地路徑。

如果您有 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"
)

以下行將通過子文件夾遞歸搜索任何 zip 文件。 在下面的示例中,腳本將在 C: 的根目錄開始遞歸搜索。 文件的路徑將保存為變量,以便稍后調用。

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
)

遞歸搜索多個驅動器號的另一種方法是將驅動器號設置為 FOR 循環中的變量。 以下示例將檢查驅動器號是否存在,然后在整個目錄中搜索 zip 文件。 例子:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM