Greetings all. I’ve got a seemingly simple problem but being a PS newbie I’m running into some obsticals. Here is the business logic: In a folder there are pdf files with two different signatures. Half with underscores the other half with no underscores. All of the file names consist of dates. For example:
20160930063010.pdf
20160930_063010.pdf
I need to modify the files without underscores and convert them to look like the ones with underscores.
When the conversion is complete, there may be some files that share the same name. (not necessarialy duplicates, just same file name)
I need to identify which of the newly changed names is the same as the underscored files.
Flag them somehow and rename them to a different timestamp for that day.
The new timestamp will be in the past. Specifically the first seconds of the day.
For example the file name above would be renamed from 20160930_053010.pdf to 20160930_000001.pdf.
If there are more than one duplicate the timestamp rename would just increment by 1 second.
I’m able to create an array of the first two file signatures but I’m having trouble copying the Non-Underscore names and converting them to names with the underscore. Could someone tell me how to rename the non-underscored files properly?
Any help would be appreciated.
Also please let me know if I’m completely doing this wrong and should go a different direction.
Here is the code:
cls
$src = "D:\PDFs\MyFolderA"
$dest = "D:\PDFs\MyFolderB"
$a= New-Object System.Collections.ArrayList
$srcFiles=Get-ChildItem -Path $src -Filter *.pdf
foreach($file in $srcFiles) {
## only process files with correct signature. does not process files like 'yaddayadda.pdf'
if ( $file.Name -match "\d{14}.pdf" -OR $file.Name -match "\d{8}_\d{6}.pdf" ) {
#write-host $file
## process the acceptable files
$myThing1 = Get-ChildItem $src | Where-Object {$_.Name -match "\d{14}.pdf" } ## signatue A (all numbers. no underscore)
$myThing2 = Get-ChildItem $src | Where-Object {$_.Name -match "\d{8}_\d{6}.pdf" } ## signature b (with underscore)
$myThing3 = $myThing1 #copy the ones without underscore to mything3
}else{
## email the bad file name
#$PSEmailServer = "mailrelay.myDomain.com"
#Send-MailMessage -From "ed.stanton@mydomain.com" -To "ed.stanton@mydomain.com" -Subject "Bad TAAD File" -Body "This is a bad file name: $file"
} # end if ( $file.Name -match "\d{14}.pdf" -OR $file.Name -match "\d{8}_\d{6}.pdf" )
} # end foreach($file in $srcFiles)
### This is not workig correctly ###
foreach($item in $myThing3) {
$firstPart = $item.Name.toString().Substring(0,8)
$secondPart = $item.Name.toString().Substring(8,6)
$myThing3 = $firstPart + "_" + $secondPart + ".pdf"
$a.Add($myThing3)
write-host "howdy" : $item
}
write-host $myThing1
write-host $myThing2
write-host $myThing3
Here is the output :
0 howdy : 20160927063055.pdf 1 howdy : 20160930063000.pdf 2 howdy : 20160930063009.pdf 3 howdy : 20160930063010.pdf 20160927063055.pdf 20160930063000.pdf 20160930063009.pdf 20160930063010.pdf 20160928_061543.pdf 20160930_063000.pdf 20160930_063009.pdf 20160930_063010.pdf 20160930_063011.pdf 20160930_063010.pdf
It looks like only the last file is being renamed correctly. 20160930_063010.pdf is correct but none of the others got renamed.