Reputation: 139
I have a requirement to copy files from a network drive to local drive based on minutes. Once copied over then I need to modify the filename as per below requirements:
Sample filename:
Ser1-PRD-CL4$Entreprise_AG2_MyCompany-NA_DB_Name_02_LOG_20191217_094501.trn
Once copied modify the name of all copied only files as following:
a. Remove all characters appearing before word "MyCompany"
b. Remove underscore along with word LOG "_LOG"
c. Remove underscore (_) between date and transaction number: 20191217_094501
I have achieved the copy and removal of "_LOG" from the filenames but having trouble with the remaining requirement.
Any input will be appreciated:
Important Note: There will be other files in the destination that were moved name and have their names modified in the destination directory and those file names should not be modified.
$Sourcefolder= "D:\Temp"
$Targetfolder= "D:\Temp2"
Get-ChildItem -Path $Sourcefolder -Recurse|
Where-Object {
$_.LastWriteTime -gt [datetime]::Now.AddMinutes(-60)
}| Copy-Item -Destination $Targetfolder
Get-ChildItem -Path $Targetfolder | Rename-Item -NewName {$_.Name -replace "_LOG", ""}
Get-ChildItem -Path $Targetfolder | Rename-Item -NewName {$_.Name -replace '^MyCompany'} #This is not working
Upvotes: 1
Views: 4145
Reputation: 27606
I'm trying it here with regex lookahead and lookbehind https://www.regular-expressions.info/lookaround.html, so -replace
only gets rid of the part that actually matches.
dir |
copy-item -destination {
$_.name -replace '.*(?=MyCompany)' -replace '_LOG' -replace '(?<=\d)_(?=\d)' } -whatif
What if: Performing the operation "Copy File" on target
Item: /Users/js/foo/MyServer$Entreprise_AG2_MyCompany-NA_DB_Name_LOG_20191217_094501.trn
Destination: /Users/js/foo/MyCompany-NA_DB_Name_20191217094501.trn".
Upvotes: 1
Reputation: 1216
You can use Copy-Item to specify a new name while copying (so no need to rename afterwards). Look at the code below to get the idea:
$Sourcefolder= "D:\Temp"
$Targetfolder= "D:\Temp2"
Get-ChildItem -Path $Sourcefolder -Recurse |
Where-Object {
$_.LastWriteTime -gt [datetime]::Now.AddMinutes(-60)
} | ForEach-Object {
$newname = $_.Name.Replace('_LOG','') # removes _LOG
$newname = $newname -Replace "(\d)_(\d)","$1$2" # replace underscore between the digits
$newname = $newname -Replace "(.*)Mycompany","MyCompany$1" # gets rid of anything before the word "MyCompany"
Copy-Item -Path $_ -Destination (Join-Path $Targetfolder $newname)
}
Upvotes: 1
Reputation: 15518
You can try this:
get-childitem-path "\\server\share\path" -recurse | foreach {
if ($_.LastWriteTime -gt [DateTime]::NowAddMinutes(-60)) {
$TheName = $_.Name -replace "_LOG", ""
$TheName = $TheName -replace "\d_\d", "$1$2"
$TheName = $TheName -replace ".*MyCompany", "MyCompany"
Copy-Item -path $_ -destination {join-path "TargetPath" $TheName}
}
}
Upvotes: 1
Reputation: 1020
For your first question on the repeated copying of the same files see: Powershell script that can log already scanned files and ignore them on next run?
For 2a)
Get-ChildItem -Path $Targetfolder | Rename-Item -NewName {$_.Name.substring($_.Name.indexof("MyCompany"))}
Please notice that the string is case sensitive.
Upvotes: 1