add at the end of the log the number of files treat, remaining and errors

bibi92
bibi92 used Ask the Experts™
on
Hello,


How can I add at the end of the log the number of files treat, remaining and errors, please ?

ForEach-Object {
		$Directory = $_.Group
		$PrefixList | ForEach-Object {
			$Prefix = $_
			$Directory | Where-Object {$_.Name -like $Prefix} | Sort-Object -Property Name | Select-Object -First 1 | ForEach-Object {
				$NewName = $_.BaseName.SubString(1) -replace '_\d{8}_\d{6}\Z'
				Try {
					Rename-Item -Path $_.FullName -NewName $NewName -ErrorAction Stop -WhatIf
					$Result = 'OK'
				} Catch {
					$Result = $_.Exception.Message
				}
				$_ | Select-Object 'DirectoryName', 'Name', @{n='NewName'; e={$NewName}}, @{n='Result'; e={$Result}}
			}
		}
	} | Format-Table -AutoSize | Out-String -Width 1024 | Out-File E:\log.txt -Encoding Default -ErrorVariable ERR1 -Append

Open in new window

     
Thanks

Regards
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
You should post the complete script; the first two lines were missing, which is kind of confusing for Experts who don't know the background.
That said, since I know the background, try this:
$PrefixList = 'ZCAMER*', 'ZTOGO*'
$Log = 'D:\Files\log.txt'
$Total = $Processed = $Errors = 0
Get-ChildItem -Path D:\Files -Include $PrefixList -Recurse |
	Group-Object -Property DirectoryName |
	ForEach-Object {
		$Directory = $_.Group
		$Total += $Directory.Count
		$PrefixList | ForEach-Object {
			$Prefix = $_
			$Directory | Where-Object {$_.Name -like $Prefix} | Sort-Object -Property Name | Select-Object -First 1 | ForEach-Object {
				$NewName = $_.BaseName.SubString(1) -replace '_\d{8}_\d{6}\Z'
				Try {
					Rename-Item -Path $_.FullName -NewName $NewName -ErrorAction Stop -WhatIf
					$Processed += 1
					$Result = 'OK'
				} Catch {
					$Result = $_.Exception.Message
					$Errors += 1
				}
				$_ | Select-Object 'DirectoryName', 'Name', @{n='NewName'; e={$NewName}}, @{n='Result'; e={$Result}}
			}
		}
	} | Format-Table -AutoSize | Out-String -Width 1024 | Out-File -Path $Log -Encoding Default -Append

$(
"Total:     $($Total)"
"Processed: $($Processed)"
"Errors:    $($Errors)"
"Remaining: $($Total - $Processed)"
) | Out-File -Path $Log -Encoding Default -Append

Open in new window

Author

Commented:
Thanks regards

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial