如何压缩/7zip/或任何其他压缩文件列表
How to zip / 7zip / or any other compress a list of files
我基本上是在尝试做一些基本的日志删除操作
通过几个步骤。
1st - 获取所有早于 X 天的文件(在我的例子中是 7 天)
第二 - 将它们压缩到不同的位置
3rd - 删除压缩文件
4 - 查看压缩日志文件夹并删除 30 天之前的文件
第 1 - 完成 - 我得到了文件列表
第三 - 没问题 - 我认为
第 4 - 与第 1 相同 ...
2nd ...在这里我尝试使用 7zip,因为它已经嵌入 Windows
我们对 3rd 方工具有严格的政策
所以 winrar 不是一个选项
这是我试过的代码,但我没有得到任何结果
它在 zip 命令上失败
if ((Test-Path "$env:ProgramFiles-Zipz.exe") -eq $true){Set-Alias sz "$env:ProgramFiles-Zipz.exe" }
$DateStr = (Get-Date).ToString("dd-MM-yyyy")
$arcPath = "D:\SDDP\LOG_Archive_$DateStr.zip"
$limit = (Get-Date).AddDays(-7)
$path = "D:\SDDP\LOG"
$filesToBackUP = Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit }
ForEach ( $file in $filesToBackUP )
{
sz a -tzip $archPath $file.FullName
}
我得到的错误是:
Open archive: D:\SDDP\LOG\DISTRIBUTOR(232)-09-2015\SDDP_DISTRIBUTOR(232)_04-09-2015_1.csv
sz : ERROR: D:\SDDP\LOG\DISTRIBUTOR(232)-09-2015\SDDP_DISTRIBUTOR(232)_04-09-2015_1.csv
At line:13 char:9
+ sz a -tzip $archPath $file.FullName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (ERROR: D:\SDDP\...4-09-2015_1.csv:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
D:\SDDP\LOG\DISTRIBUTOR(232)-09-2015\SDDP_DISTRIBUTOR(232)_04-09-2015_1.csv
Open ERROR: Can not open the file as [zip] archive
ERRORS:
Is not archive
System ERROR:
Incorrect function.
参考我从here
中获取的zip命令
当我浏览添加参考时,没有关于创建一个新的 7zip 文件来添加文件的内容,所以我想它会自动创建,但不太确定。
请指教
谢谢
尝试以下方法:
[string]$Zip = "C:\path to 7zip-zipz.exe";
[array]$args = "a", "-tzip", "-y", "-r", "$arcPath ";
ForEach ( $file in $filesToBackUP )
{
$Zip $args $file;
}
抱歉,我目前在外,无法测试。
在 PowerShell 5.0 中,有以下示例。您可以用列表替换 dir .\dirToZip\* -File
。
dir .\dirToZip\* -File | %{
Compress-Archive -Path $_.fullname -DestinationPath .\test.zip -Update
}
我基本上是在尝试做一些基本的日志删除操作
通过几个步骤。
1st - 获取所有早于 X 天的文件(在我的例子中是 7 天)
第二 - 将它们压缩到不同的位置
3rd - 删除压缩文件
4 - 查看压缩日志文件夹并删除 30 天之前的文件
第 1 - 完成 - 我得到了文件列表
第三 - 没问题 - 我认为
第 4 - 与第 1 相同 ...
2nd ...在这里我尝试使用 7zip,因为它已经嵌入 Windows 我们对 3rd 方工具有严格的政策 所以 winrar 不是一个选项
这是我试过的代码,但我没有得到任何结果 它在 zip 命令上失败
if ((Test-Path "$env:ProgramFiles-Zipz.exe") -eq $true){Set-Alias sz "$env:ProgramFiles-Zipz.exe" }
$DateStr = (Get-Date).ToString("dd-MM-yyyy")
$arcPath = "D:\SDDP\LOG_Archive_$DateStr.zip"
$limit = (Get-Date).AddDays(-7)
$path = "D:\SDDP\LOG"
$filesToBackUP = Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit }
ForEach ( $file in $filesToBackUP )
{
sz a -tzip $archPath $file.FullName
}
我得到的错误是:
Open archive: D:\SDDP\LOG\DISTRIBUTOR(232)-09-2015\SDDP_DISTRIBUTOR(232)_04-09-2015_1.csv
sz : ERROR: D:\SDDP\LOG\DISTRIBUTOR(232)-09-2015\SDDP_DISTRIBUTOR(232)_04-09-2015_1.csv
At line:13 char:9
+ sz a -tzip $archPath $file.FullName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (ERROR: D:\SDDP\...4-09-2015_1.csv:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
D:\SDDP\LOG\DISTRIBUTOR(232)-09-2015\SDDP_DISTRIBUTOR(232)_04-09-2015_1.csv
Open ERROR: Can not open the file as [zip] archive
ERRORS:
Is not archive
System ERROR:
Incorrect function.
参考我从here
中获取的zip命令
当我浏览添加参考时,没有关于创建一个新的 7zip 文件来添加文件的内容,所以我想它会自动创建,但不太确定。
请指教
谢谢
尝试以下方法:
[string]$Zip = "C:\path to 7zip-zipz.exe";
[array]$args = "a", "-tzip", "-y", "-r", "$arcPath ";
ForEach ( $file in $filesToBackUP )
{
$Zip $args $file;
}
抱歉,我目前在外,无法测试。
在 PowerShell 5.0 中,有以下示例。您可以用列表替换 dir .\dirToZip\* -File
。
dir .\dirToZip\* -File | %{
Compress-Archive -Path $_.fullname -DestinationPath .\test.zip -Update
}