0

我希望你们中的一些人可以帮助我。我在修改 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); }"

如您所见,我需要将此脚本作为批处理文件。

4

2 回答 2

1

用于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 路径也应与任一命令一起使用,而不仅仅是本地路径。

于 2020-01-15T18:16:48.393 回答
1

如果您有 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
    )
)
于 2020-01-15T19:08:44.747 回答