mack
mack

Reputation: 2965

Powershell File Rename Date/Time

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"
}

Upvotes: 0

Views: 9825

Answers (3)

Doberon
Doberon

Reputation: 648

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"
}

Upvotes: 0

mjolinor
mjolinor

Reputation: 68273

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"
}

Upvotes: 0

Frode F.
Frode F.

Reputation: 54881

Try this:

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

Upvotes: 4

Related Questions