1

Windows 10 64-bit. PowerShell 5.1

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

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 

Update zipfiles with PowerShell 5 gci, regex, $Matches, foreach {$_ PadLeft}, Compress-Archive, and Remove-Item. Avg 29ms 9 files (48KB) 3 archives on ten runs.

For testing clear your desktop. There should be no .txt or .zip files on your desktop. There are 3 Remove-Item without -whatif or -confirm. Makes 9 text files and three zipfiles. Working script, test script, script w/ logfile.

#credit stackoverflow.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

Make test files and test:

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 
#

Make test files, ten run loop with logfile:

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 
# 

LogFile:

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    

How to parse file path, name, basename with more than one dot with powershell.

How to parse basename to first dot.

How to use PadLeft.

How to add leading and ending zeroes to strings with PowerShell.

Update zipfiles with PowerShell 5 or greater. gci, get-childitem, regex, regular expression, regular expressions, $Matches, foreach, the current item from the pipe, $_, PadLeft, Compress-Archive, and Remove-Item. Average 29 milliseconds on 9 files (48KB) in 3 archives on ten runs.

somebadhat
  • 744
  • 1
  • 5
  • 17

1 Answers1

1

Using Group-Object to compress files for each group is a bit faster.

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
rokumaru
  • 1,244
  • 1
  • 8
  • 11
  • 8秒を得るのに6日かかりました。 29msを取得するのに3時間もかかりませんでした。 よくやった。 誰もがもっと良くなるとは思いませんが、あなたの答えを私の質問にコピーして、あなたにそれを称賛したいと思います。 英語しか読めません。 p.s Compress-Archive -updateの使用を忘れた – somebadhat Mar 13 '20 at 22:53
  • Good Japanese! お役に立てて嬉しいです。and please excuse me for my poor English. To update the archives, put the `-Update` switch on `Compress-Archive`. – rokumaru Mar 14 '20 at 01:02
  • I already did. I thought you might want to update your answer. Google translate. 私はすでにやった。 あなたの答えを更新したいと思うかもしれません。 質問が既に更新されているのを見ました。... – somebadhat Mar 14 '20 at 01:21