troubleshooting Question

format log file

Avatar of bibi92
bibi92Flag for France asked on
Powershell
2 Comments1 Solution86 ViewsLast Modified:
Hello,
When I executed the following script
Get-ChildItem -Path e:\test -Include $PrefixList -Recurse |
	Group-Object -Property DirectoryName |
	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'
				Rename-Item -Path $_.FullName -NewName $NewName 
				$_ | Select-Object 'DirectoryName', 'Name', @{n='NewName'; e={$NewName}} | out-file e:\log.txt -ErrorVariable ERR1 -encoding "default" -append
			}
		}
	} 

The content of the log is :
DirectoryName                           Name                                    NewName                                
-------------                           ----                                    -------                                
E:\Files\CAMER                          ZCAMER_20170214_032034.txt              CAMER                              



DirectoryName                           Name                                    NewName                                
-------------                           ----                                    -------                                
E:\Files\TOGO                           ZTOGO_20170216_124903.txt               TOGO      

How can I generate :

DirectoryName                           Name                                    NewName                                
-------------                           ----                                    -------                                
E:\Files\CAMER                          ZCAMER_20170214_032034.txt              CAMER                              
                             
E:\Files\TOGO                           ZTOGO_20170216_124903.txt               TOGO    

Thanks

Regards
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros