0

I have what should be a simple problem, but I just can't seem to get it right. I have a file that has two file extensions. We retrieve the file, decrypt it and save the encrypted file to a backup folder with a date/time stamp showing when the file was processed. All I want to do is to have the date/time stamp put before the two extensions instead of between them. There has to be a simple one line answer to this, but I can't find it. Any suggestions?

Original File Name - DAILY AP FILES.ZIP.pgp

Current Rename File Name - DAILY_AP_FILES.ZIP-02182013-155123.pgp

Desired Rename File Name - DAILY_AP_FILES-02182013-155123.pgp

Get-ChildItem "$dlpath\*.pgp" | ForEach-Object {          
    Move-Item $_.FullName "$BackupFolder$($_.BaseName.Replace(" ", "_"))-$(Get-Date -Format "MMddyyyy-HHmmss").pgp"
}

3 Answers 3

4

Try this:

Get-ChildItem "$dlpath\*.pgp" | ForEach-Object {          
    Move-Item $_.FullName "$BackupFolder$($_.BaseName -replace " ", "_" -replace '\.([^\.]+)$')-$(Get-Date -Format "MMddyyyy-HHmmss").pgp"
}
3
  • Beat me to it. I'd use \..*?$ for replacing the second extension, though. Easier on the eye. ;) Feb 18, 2013 at 21:24
  • true. I just wanted to ensure that it didn't remove parts of the name if it also included a . . After I became more active here on SO, I've realised that people use a lot of weird filenames :P
    – Frode F.
    Feb 18, 2013 at 21:30
  • Worked perfectly! Thank you! I knew there had to be a simple way to do this.
    – mack
    Feb 19, 2013 at 15:11
0

Does this work for you?

Get-ChildItem "$dlpath\*.pgp" | ForEach-Object {
$NewBaseName = ($_.BaseName.Replace(" ", "_")) -replace '^(.+\.).+','$1'           
Move-Item $_.FullName "$BackupFolder$NewBaseName-$(Get-Date -Format "MMddyyyy-HHmmss").pgp"
}
0
0

Try this modification, improve because sort by date

$source_path="D:\Transferencia"
$backup_folder="D:\Transferencia_Backup"

Get-ChildItem "$source_path\*.pgp" | ForEach-Object {          
   Move-Item $_.FullName "$backup_folder\$($_.BaseName -replace " ", "_")-$(Get-Date -Format "yyyyyMMdd_HHmmss").pgp"
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.