繁体   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