用Y文件更新X档案。 gci,foreach($ files中的$ file),ForEach-Object {split-path},Compress-Archive和Remove-Item。更快的方法?

问题描述 投票:1回答:1

Windows 10 64位。 PowerShell 5.1

使用Y文件更新X归档文件(现在为2000,现在是“到操作系统的限制”)。我正在使用带有默认选项的gci, foreach($file in $files), ForEach-Object {split-path & Current object on the pipeline}, Compress-ArchiveRemove-Item。所有文件和存档都在同一目录中。有没有更快的方法?

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

为了进行测试,请清除您的桌面。您的桌面上不应有.txt或.zip文件。有2个Remove-Item,没有-whatif-confirm。制作6个文本文件进行测试。覆盖logfile031422_030657692.txt。如果您不想查看档案,请在DEBUG部分中添加注释。在实际生产中,0..11将更改为0..2000

两个版本的脚本。一个完整注释的脚本,包括logfile031422_030657692.txt或无注释,无日志文件。

$StartTimeGlobal = (get-date)
echo "The time is: "((Get-Date).tostring("hh:mm:ss tt") -replace " : ", ":") | out-file logfile031422_030657692.txt 
pushd "$env:userprofile\desktop"
# $cd=$pwd | select -ExpandProperty Path
$files = @()
$files +="1.1.txt"
$files +="1.txt"
$files +="11.txt"
$files +="2.1.3.6.txt"
$files +="2.1.txt"
$files +="2.txt"
foreach($file in $files){new-item -itemtype file -path $file}
start-sleep 1
$files=@() 
start-sleep 1
$files = gci *.txt | select -ExpandProperty Name 

foreach($file in $files){ 
0..11 | ForEach-Object { 
$host.ui.rawui.WindowTitle = "ForEach-Object 0-11"
$name1=(Split-Path -Path $file -Leaf).Split(".")[0] 
$name2 = 'FileGroup{0:0000000000}' -f $_
echo "Current object on the pipeline is:"$_ 
echo "name1 is: " $name1 
echo "name2 is: " $name2
If ("$_" -eq "$name1"){
echo $_ " equals "  $name1
$host.ui.rawui.WindowTitle = "Adding $name1*.txt to $name2.zip"
echo "Adding $name1*.txt to $name2.zip"
$host.ui.rawui.WindowTitle = "Compress-Archive $file $name2.zip -update"
Compress-Archive "$file" "$name2.zip" -update 
start-sleep 1 
echo '$file is: '$file
# cmd /c pause 
Remove-Item "$file"
# Remove-Item "$file" -confirm
} else {
$host.ui.rawui.WindowTitle = "$_  does not equal $name1"
echo $_ " does not equal "$name1" 
Not adding $name1*.txt to $name2.zip"}} | out-file logfile031422_030657692.txt -append} 
Clear-Variable -Name ("file", "name1", "name2")
# BEGIN DEBUG
Write-Host " 
Delete all FileGroup*.zip is next.
Press any key to delete all test files.
"
cmd /c pause>nul 
# END DEBUG 
Remove-Item filegroup*.zip 
# Remove-Item filegroup*.zip -confirm
echo "The time is: "((Get-Date).tostring("hh:mm:ss tt") -replace " : ", ":") | out-file logfile031422_030657692.txt -append
$ElapsedTime = (get-date) - $StartTimeGlobal
$ElapsedTime = "$($ElapsedTime.ToString("h\ \h\o\u\r\,\ m\ \m\i\n\u\t\e\s\ \a\n\d\ s\ \s\e\c\o\n\d\s"))"  
echo "The elapsed time is: $ElapsedTime"
echo "The elapsed time is: $ElapsedTime" | Out-File logfile031422_030657692.txt -append
popd 
#

生产:无评论或日志文件。

pushd "$env:userprofile\desktop"
$files = @()
$files +="1.1.txt"
$files +="1.txt"
$files +="11.txt"
$files +="2.1.3.6.txt"
$files +="2.1.txt"
$files +="2.txt"
foreach($file in $files){new-item -itemtype file -path $file}
$files=@() 
$files = gci *.txt | select -ExpandProperty Name 
foreach($file in $files){ 
0..11 | ForEach-Object { 
$name1=(Split-Path -Path $file -Leaf).Split(".")[0] 
$name2 = 'FileGroup{0:0000000000}' -f $_
If ("$_" -eq "$name1"){
Compress-Archive "$file" "$name2.zip" -update 
Remove-Item "$file"
}}} 
Clear-Variable -Name ("file", "name1", "name2")
Write-Host " 
Delete all FileGroup*.zip is next.
Press any key to delete all test files.
"
cmd /c pause>nul 
Remove-Item filegroup*.zip 
popd 
#

工作脚本。有没有更快的方法? 8秒将6个文本文件(24KB)压缩为3个.zip文件。

powershell archive powershell-v5.1
1个回答
0
投票

使用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 -ea Stop
    ri $_.Group
}

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

ri FileGroup*.zip
popd
© www.soinside.com 2019 - 2024. All rights reserved.