检查 Compress-Archive 是否可以根据文件归档的成功生成 true/false
checking if Compress-Archive can produce a true/false based on success of the archival of a file
我有以下代码:
$items = Get-ChildItem -Path 'D:\Myoutput\'
$items | ForEach-Object
{
$lastWrite = ($_).LastWriteTime
$timespan = New-Timespan -days 3 -hours 0 -Minutes 0
if(((get-date) - $lastWrite) -gt $timespan) {
$name = $_.Name
$isDir = $_.PSIsContainer
if(!$isDir) {
$_ | Compress-Archive -DestinationPath "D:\Myoutput\Archive$name.zip"
if (**above_line** is success) {
echo "$name is zipped"
$_ | Remove-Item
}
}
}
}
请帮忙,我怎样才能知道'$_ | Compress-Archive -DestinationPath "D:\Myoutput\Archive$name.zip"' 是否成功。
Compress-Archive
如果失败已经抛出错误,您可以在删除原始文件之前捕获它。例如,我使用 continue
跳过其余命令。您还可以使用 Get-ChildItem -File
:
跳过检查文件夹
Foreach ($file in (Get-Item C:\temp\ -File)) {
Try { $file | Compress-Archive -DestinationPath C:\BadPath\test.zip }
Catch { Write-Warning ("Skipping file due to error: " + $file.FullName); continue }
Remove-Item $file
}
下面是我使用上面的错误路径时的输出:
WARNING: Skipping file due to error: C:\temp\test1.txt
WARNING: Skipping file due to error: C:\temp\test2.txt
而且这些文件不会被删除。
Compress-Archive
will throw exceptions if something goes wrong, and it will delete partially created archives (source)。所以,你可以做两件事来确保成功:
- 捕获异常
- 测试存档是否存在
示例:
$items = Get-ChildItem -Path 'D:\Myoutput\'
$items | ForEach-Object
{
$lastWrite = ($_).LastWriteTime
$timespan = New-Timespan -days 3 -hours 0 -Minutes 0
if(((get-date) - $lastWrite) -gt $timespan) {
$name = $_.Name
$isDir = $_.PSIsContainer
if(!$isDir) {
try {
$_ | Compress-Archive -DestinationPath "D:\Myoutput\Archive$name.zip"
if (Test-Path -Path "D:\Myoutput\Archive$name.zip") {
Write-Host "$name is zipped"
$_ | Remove-Item
} else {
Write-Host "$name is NOT zipped" -ForegroundColor Red
}
} catch {
Write-Host "$name is NOT zipped" -ForegroundColor Red
}
}
}
}
我有以下代码:
$items = Get-ChildItem -Path 'D:\Myoutput\'
$items | ForEach-Object
{
$lastWrite = ($_).LastWriteTime
$timespan = New-Timespan -days 3 -hours 0 -Minutes 0
if(((get-date) - $lastWrite) -gt $timespan) {
$name = $_.Name
$isDir = $_.PSIsContainer
if(!$isDir) {
$_ | Compress-Archive -DestinationPath "D:\Myoutput\Archive$name.zip"
if (**above_line** is success) {
echo "$name is zipped"
$_ | Remove-Item
}
}
}
}
请帮忙,我怎样才能知道'$_ | Compress-Archive -DestinationPath "D:\Myoutput\Archive$name.zip"' 是否成功。
Compress-Archive
如果失败已经抛出错误,您可以在删除原始文件之前捕获它。例如,我使用 continue
跳过其余命令。您还可以使用 Get-ChildItem -File
:
Foreach ($file in (Get-Item C:\temp\ -File)) {
Try { $file | Compress-Archive -DestinationPath C:\BadPath\test.zip }
Catch { Write-Warning ("Skipping file due to error: " + $file.FullName); continue }
Remove-Item $file
}
下面是我使用上面的错误路径时的输出:
WARNING: Skipping file due to error: C:\temp\test1.txt
WARNING: Skipping file due to error: C:\temp\test2.txt
而且这些文件不会被删除。
Compress-Archive
will throw exceptions if something goes wrong, and it will delete partially created archives (source)。所以,你可以做两件事来确保成功:
- 捕获异常
- 测试存档是否存在
示例:
$items = Get-ChildItem -Path 'D:\Myoutput\'
$items | ForEach-Object
{
$lastWrite = ($_).LastWriteTime
$timespan = New-Timespan -days 3 -hours 0 -Minutes 0
if(((get-date) - $lastWrite) -gt $timespan) {
$name = $_.Name
$isDir = $_.PSIsContainer
if(!$isDir) {
try {
$_ | Compress-Archive -DestinationPath "D:\Myoutput\Archive$name.zip"
if (Test-Path -Path "D:\Myoutput\Archive$name.zip") {
Write-Host "$name is zipped"
$_ | Remove-Item
} else {
Write-Host "$name is NOT zipped" -ForegroundColor Red
}
} catch {
Write-Host "$name is NOT zipped" -ForegroundColor Red
}
}
}
}