We help IT Professionals succeed at work.

format log file

bibi92
bibi92 asked
on
80 Views
Last Modified: 2017-03-07
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
			}
		}
	} 

Open in new window


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
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks regards
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.