使用 PowerShell 将几个文件夹压缩成一个 zip
Several folders into a zip with PowerShell
我有几个文件夹 dir_01
、dir_02
、dir_03
,我想将它们备份到一个 zip 文件中,例如 backup.zip
。这必须保存在具有今天日期的文件夹中。我需要一个 .bat 文件来完成这项工作;不允许额外的第三方可执行文件。
Here 我找到了如何创建包含今天日期的文件夹的方法,但我在将 $destination
传递给 -DestinationPath
时遇到问题。我创建了一个调用 PowerShell 的 .bat。给我问题的代码:
powershell.exe $destination = New-Item -Path 'C:\path\to\destionation' -ItemType Directory -Name ("$(Get-Date -f yyyy-MM-dd)")
powershell.exe -nologo -noprofile -command Compress-Archive -Path 'C:\path\dir_01', 'C:\path\dir_02', 'C:\path\dir_03' -DestinationPath $destination\backup.zip -Force
错误信息如下:
New-Object : Exception calling ".ctor" with "2" argument(s): "Access to the path 'C:\backup.zip' is denied."
At
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:729
char:30
+ ... ileStream = New-Object -TypeName System.IO.FileStream -ArgumentList $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
但是,如果我硬编码到目的地的路径,代码就可以工作,如下所示:
powershell.exe -nologo -noprofile -command Compress-Archive -Path 'C:\path\dir_01', 'C:\path\dir_02', 'C:\path\dir_03' -DestinationPath 'C:\whole\path\backup.zip' -Force
但是这样做,我无法在今天的文件夹中保存 backup.zip
。
问题:如何将backup.zip
保存到我用今天日期创建的文件夹中?
您可以在同一个 PowerShell 会话中执行这两个命令,方法是用分号分隔它们。
powershell.exe $destination = New-Item -Path 'C:\path\to\destionation' -ItemType Directory -Name ("$(Get-Date -f yyyy-MM-dd)"); Compress-Archive -Path 'C:\path\dir_01', 'C:\path\dir_02', 'C:\path\dir_03' -DestinationPath $destination\backup.zip -Force
这将启动 PowerShell 并让它创建文件夹,然后在同一会话中将内容压缩到其中,而不是创建一个会话,在该会话中创建一个文件夹,关闭会话,启动一个新的 PowerShell 会话,zip事情搞定,关闭第二个会话。
我有几个文件夹 dir_01
、dir_02
、dir_03
,我想将它们备份到一个 zip 文件中,例如 backup.zip
。这必须保存在具有今天日期的文件夹中。我需要一个 .bat 文件来完成这项工作;不允许额外的第三方可执行文件。
Here 我找到了如何创建包含今天日期的文件夹的方法,但我在将 $destination
传递给 -DestinationPath
时遇到问题。我创建了一个调用 PowerShell 的 .bat。给我问题的代码:
powershell.exe $destination = New-Item -Path 'C:\path\to\destionation' -ItemType Directory -Name ("$(Get-Date -f yyyy-MM-dd)")
powershell.exe -nologo -noprofile -command Compress-Archive -Path 'C:\path\dir_01', 'C:\path\dir_02', 'C:\path\dir_03' -DestinationPath $destination\backup.zip -Force
错误信息如下:
New-Object : Exception calling ".ctor" with "2" argument(s): "Access to the path 'C:\backup.zip' is denied."
At
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:729
char:30
+ ... ileStream = New-Object -TypeName System.IO.FileStream -ArgumentList $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
但是,如果我硬编码到目的地的路径,代码就可以工作,如下所示:
powershell.exe -nologo -noprofile -command Compress-Archive -Path 'C:\path\dir_01', 'C:\path\dir_02', 'C:\path\dir_03' -DestinationPath 'C:\whole\path\backup.zip' -Force
但是这样做,我无法在今天的文件夹中保存 backup.zip
。
问题:如何将backup.zip
保存到我用今天日期创建的文件夹中?
您可以在同一个 PowerShell 会话中执行这两个命令,方法是用分号分隔它们。
powershell.exe $destination = New-Item -Path 'C:\path\to\destionation' -ItemType Directory -Name ("$(Get-Date -f yyyy-MM-dd)"); Compress-Archive -Path 'C:\path\dir_01', 'C:\path\dir_02', 'C:\path\dir_03' -DestinationPath $destination\backup.zip -Force
这将启动 PowerShell 并让它创建文件夹,然后在同一会话中将内容压缩到其中,而不是创建一个会话,在该会话中创建一个文件夹,关闭会话,启动一个新的 PowerShell 会话,zip事情搞定,关闭第二个会话。