使用 PowerShell 5 或 > 更新压缩文件。寻求改进的工作代码

Update zipfiles with PowerShell 5 or >. Working code looking for improvement

Windows 10 个 64 位。 PowerShell 5.1

使用 PowerShell 5 或 > 更新 zip 文件。正在寻找改进的工作代码。

1.1.txt, 1.1.3.txt and 1.txt go in                   FileGroup0000000001.zip
11.txt and 11.1.txt goes in                          FileGroup0000000011.zip 
2.1.3.6.txt, 2.1.3.6.7.txt, 2.1.txt, and 2.txt go in FileGroup0000000002.zip 

使用 PowerShell 5 更新压缩文件 gci, regex, $Matches, foreach {$_ PadLeft},Compress-Archive, and Remove-Item。平均 29 毫秒 9 个文件 (48KB) 3 个存档,十 运行 秒。

为了测试清理你的桌面。您的桌面上不应有 .txt 或 .zip 文件。有 3 个 Remove-Item 没有 -whatif-confirm。生成 9 个文本文件和三个 zip 文件。 工作脚本、测试脚本、带日志文件的脚本。

#credit whosebug.com/a/60666329/8826818
pushd %USERPROFILE%\Desktop 
gci *.txt | where BaseName -match "^(\d+)(\.\d+)*$" | group { $Matches[1] } | foreach {
$dest = 'FileGroup' +  $_.Name.PadLeft(10, "0") + ".zip"
Compress-Archive $_.Group $dest -ea Stop -update
ri $_.Group} 
popd

制作测试文件并测试:

pushd %USERPROFILE%\Desktop 
ni @("1.1.txt", "1.txt", "11.txt", "2.1.3.6.txt", "2.1.txt","2.txt") > $null
gci *.txt | where BaseName -match "^(\d+)(\.\d+)*$" | group { $Matches[1] } | foreach {
$dest = 'FileGroup' +  $_.Name.PadLeft(10, "0") + ".zip"
Compress-Archive $_.Group $dest -ea Stop -update
ri $_.Group} 
ni @("1.1.3.txt", "11.1.txt", "2.1.3.6.7.txt") > $null
gci *.txt | where BaseName -match "^(\d+)(\.\d+)*$" | group { $Matches[1] } | foreach {
$dest = 'FileGroup' +  $_.Name.PadLeft(10, "0") + ".zip"
Compress-Archive $_.Group $dest -ea Stop -update
ri $_.Group}
Read-Host @" 

Delete all FileGroup*.zip is next.
There should be nine files in three archives.
Press enter key to delete all test files
"@
ri FileGroup*.zip
# clear-variable -name ("Matches", "dest")
popd 
#

制作测试文件,10 运行 循环与日志文件:

If(test-path logfile031822_030657692.txt) {
cls 
Write-Output "`r`nOverwrite logfile031822_030657692.txt?`r`nPress any key to continue ...    "
cmd /c pause> nul} 
$zcommandpath=$MyInvocation.MyCommand.Path
Write-Output "`r`nThis is $zcommandpath`r`nHow long to archive 9 text files.`r`nStarted: "((get-date).ToString('MM/dd/yy hh:mm:ss.ffff tt') -replace " : ", ":") | out-file -nonewline -append logfile031822_030657692.txt
$times=@()
$watch = New-Object System.Diagnostics.Stopwatch
for ($zdoug=0;$zdoug -lt 10; $zdoug++){
$watch.Start() #put this at start of the loop
ni @("1.1.txt", "1.txt", "11.txt", "2.1.3.6.txt", "2.1.txt","2.txt") > $null
gci *.txt | where BaseName -match "^(\d+)(\.\d+)*$" | group { $Matches[1] } | foreach {
$dest = 'FileGroup' +  $_.Name.PadLeft(10, "0") + ".zip"
Compress-Archive $_.Group $dest -ea Stop -update
ri $_.Group} 
ni @("1.1.3.txt", "11.1.txt", "2.1.3.6.7.txt") > $null
gci *.txt | where BaseName -match "^(\d+)(\.\d+)*$" | group { $Matches[1] } | foreach {
$dest = 'FileGroup' +  $_.Name.PadLeft(10, "0") + ".zip"
Compress-Archive $_.Group $dest -ea Stop -update
ri $_.Group} 
$times += $watch.Elapsed.TotalSeconds #this at the end
$watch.reset() } 
$zLoopFinished=((get-date).ToString('MM/dd/yy hh:mm:ss.ffff tt') -replace " : ", ":") 
# clear-variable -name ("Matches", "dest", "i") 
# BEGIN DEBUG
# Read-Host @" 
# Delete all FileGroup*.zip is next.
# Press enter key to delete all test files
# "@
#END DEBUG 
ri FileGroup*.zip
popd
#
$times | Measure-Object -sum -ave -max -min
$times | Measure-Object -sum -ave -max -min | out-file -append logfile031822_030657692.txt    
Write-Output "Finished:  $zLoopFinished" | out-file -append logfile031822_030657692.txt    
(gc logfile031822_030657692.txt) | Where { $_.Trim(" `t") }  | select-string -pattern "Property :" -notmatch | out-file logfile031822_030657692.txt
#https://devblogs.microsoft.com/scripting/trim-your-strings-with-powershell/
$NewLine = gc logfile031822_030657692.txt | ForEach-Object {
$_ 
if ($_ -match ('^' + [regex]::Escape( 'Finished:' ))){ " " }}
$NewLine | Out-File logfile031822_030657692.txt -Force 
# 

日志文件:

This is 
How long to archive 9 text files.
Started: 03/15/20 02:56:34.4380 PM
Count    : 10
Average  : 0.38522078
Sum      : 3.8522078
Maximum  : 1.6380882
Minimum  : 0.2281335
Finished:  03/15/20 02:56:39.9413 PM    

如何使用powershell解析多于一个点的文件路径、名称、基本名称。

如何将基本名称解析为第一个点。

PadLeft 的使用方法

如何使用 PowerShell 将前导零和结尾零添加到字符串。

使用 PowerShell 5 或更高版本更新 zip 文件。 gci、get-childitem、正则表达式、正则表达式、正则表达式、$Matches、foreach、管道中的当前项目、$_、PadLeft、Compress-Archive 和 Remove-Item。 3 个存档中的 9 个文件 (48KB) 在 10 运行 秒内平均耗时 29 毫秒。

使用Group-Object为每个组压缩文件会更快一些。

pushd $env:USERPROFILE\Desktop

ni @(
    "1.1.txt"
    "1.txt"
    "11.txt"
    "2.1.3.6.txt"
    "2.1.txt"
    "2.txt"
) > $null

gci *.txt | where BaseName -match "^(\d+)(\.\d+)*$" | group { $Matches[1] } | foreach {
    $dest = 'FileGroup' +  $_.Name.PadLeft(10, "0") + ".zip"
    Compress-Archive $_.Group $dest -Update -ea Stop
    ri $_.Group
}

Read-Host @" 
Delete all FileGroup*.zip is next.
Press enterkey to delete all test files.
"@

ri FileGroup*.zip
popd