在 Powershell 中压缩文件夹时捕获错误

发布于 2024-12-17 19:43:45 字数 1309 浏览 0 评论 0原文

我以前从未使用过 Powershell。经过一些研究后,我编写了一个压缩文件夹的脚本。

我想确保我可以捕获执行期间生成的任何错误,因此我添加了一个 Try Catch 块,并尝试记录所有错误。我需要确保压缩过程成功。

该脚本假设我们可以添加到现有的 zip 文件(如果它已经存在)。稍后我将添加一个检查以删除以前的 zip 文件,但现在如果我保持原样以测试日志记录,因为它会生成错误。

但是,错误没有按预期被捕获和记录。关于我做错了什么有什么建议吗?错误是弹出窗口“此文件夹已包含名为“Backups”的文件夹。重命名您要复制的文件夹,然后再次执行该操作。

########################################################
#
# Zip-Folder.ps1
#
# Usage:
# To zip up a folder:
# (Zip Application) (Target Archive path and File Name) (Source Folder Path to Backup)     (Log Path and File Name)
# d:\zip-folder.ps1 d:\Backups_Archive d:\Backups
########################################################

try{
$target = $args[0]
$files = Get-item $args[1]
$log = $args[2]


if (-not $target.EndsWith('.zip')) {$target += '.zip'} 

if (-not (test-Path $target)) { 
  set-content $target ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) 
} 

$ZipFile = (new-object -com shell.application).NameSpace($target) 
# $files | foreach {$zipfile.CopyHere($_.fullname)} 

foreach($file in $files) 
    { 
        $zipfile.CopyHere($file.FullName)
            Start-sleep -milliseconds 500
    }
} catch {
    Add-Content $log $error[0]

}

执行: (Zip 应用程序)(目标存档路径和文件名)(备份的源文件夹路径)(日志路径和文件名) 例如 d:\zip-folder.ps1 d:\Backups_Archive d:\Backups d:\zip.log

I've never done Powershell before. After doing some research I wrote a script that zips a folder.

I want to make sure that I can capture any error generated during execution so I added a Try Catch block and I'm trying to log all errors. I need to make sure that the zipping process is successful.

This scripts assumes that we can add to an existing zip file if it already exist. Later on I will add a check to delete the previous zip file however for now if I leave things as is to test logging as it generates an error.

However the error is not caught and logged as expected. Any suggestions as to what I'm doing wrong? The error is a pop up window "This folder already contains a folder named 'Backups'. Rename the folder you are trying to copy and then perform the operation again.

########################################################
#
# Zip-Folder.ps1
#
# Usage:
# To zip up a folder:
# (Zip Application) (Target Archive path and File Name) (Source Folder Path to Backup)     (Log Path and File Name)
# d:\zip-folder.ps1 d:\Backups_Archive d:\Backups
########################################################

try{
$target = $args[0]
$files = Get-item $args[1]
$log = $args[2]


if (-not $target.EndsWith('.zip')) {$target += '.zip'} 

if (-not (test-Path $target)) { 
  set-content $target ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) 
} 

$ZipFile = (new-object -com shell.application).NameSpace($target) 
# $files | foreach {$zipfile.CopyHere($_.fullname)} 

foreach($file in $files) 
    { 
        $zipfile.CopyHere($file.FullName)
            Start-sleep -milliseconds 500
    }
} catch {
    Add-Content $log $error[0]

}

To execute:
(Zip Application) (Target Archive path and File Name) (Source Folder Path to Backup) (Log Path and File Name)
For example
d:\zip-folder.ps1 d:\Backups_Archive d:\Backups d:\zip.log

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空名 2024-12-24 19:43:46

您看到的弹出窗口是由 Shell 生成的。虽然 您可以通过传递标志 来抑制 UI 1024,我认为不可能将其作为可以在Powershell中捕获的错误来处理,因为Shell不会将其返回给Powershell。您必须明确地处理这种情况,而不是捕获错误。

此外,它被普遍接受这种压缩文件的方式并不是非常灵活,并且也不能真正扩展大量文件(另一点是您在睡眠中的睡眠)代码)

请使用一些命令行压缩工具(例如 7z.exe)进行压缩。

The popup that you see is being generated by the Shell. Though you can suppress the UI by passing the flag 1024, I do not think it is possible to handle it as an error that can be caught in Powershell, because the Shell doesn't return it to the Powershell. You will have to handle the case explicitly rather than catching an error.

Also, it is generally accepted that this way of zipping files is not really very flexible and also does not really scale with large number of files ( another point being the sleep that you have in your code)

Please use some commandline zipping tool like the 7z.exe for zipping.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文