简体   繁体   English

Powershell 解压缩扩展名为 .zip 的文件并将其移动到另一个文件夹

[英]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.我在修改 powershell 脚本时遇到了困难。

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.该脚本检查特定文件夹(来源)中的 zip 文件(文件名是一个固定值)并将其移动到另一个文件夹(目的地),但是我需要一个检查 .zip 扩展名的脚本,而不是一个固定值和也将其移动到另一个文件夹。 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.如您所见,我需要将此脚本作为batch.file。

Use Expand-Archive to unzip the file to a directory, and then from your batch script, copy the files somewhere else.使用Expand-Archive将文件解压缩到一个目录,然后从批处理脚本中将文件复制到其他位置。 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.请注意,UNC 路径也应与任一命令一起使用,而不仅仅是本地路径。

If you have 7-Zip:如果您有 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.以下行将通过子文件夹递归搜索任何 zip 文件。 In the example below, the script will begin the recursive search at the root of C:.在下面的示例中,脚本将在 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.递归搜索多个驱动器号的另一种方法是将驱动器号设置为 FOR 循环中的变量。 The following example will check to see if the drive letter exists, then search the entire directory for a zip file.以下示例将检查驱动器号是否存在,然后在整个目录中搜索 zip 文件。 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
    )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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