Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have used a script from a previous post and modified it slightly:

Get-ChildItem "c:\test\*.txt" | ForEach-Object {          
Rename-Item $_.FullName "$BackupFolder$($_.BaseName -replace " ", "_" -replace '\..*?$')-$(Get-Date -Format "ddMMyyyy").txt"
}

However:

Current renamed file: test-ddmmyy.txt
Required Format: testddmmyy.txt

share|improve this question
add comment

1 Answer

This ought to work.

Get-ChildItem "c:\test\*.txt" | ForEach-Object {          
Rename-Item $_.FullName "$BackupFolder$($_.BaseName -replace " ", "_" -replace '\..*?$')$(Get-Date -Format "ddMMyyyy").txt"

Your dash was coming in from just before $(Get-Date. Remember, when passing things in double-quotes as a parameter, anything that's not a variable or is not within parenthesis and preceded by $ will be treated as a string character.

In the below script:

$test='test';"$test-$(Get-Date -F 'ddMMyyyy')"

$test will be expanded into its value 'test', - will be left alone since it is neither a variable nor a part of an expression, and Get-Date -F 'ddMMyyyy' will be processed and return its output since it is set apart as an expression with $(...). This will result in:

test-24112013

Get rid of the dash, and...

$test='test';"$test-$(Get-Date -F 'ddMMyyyy')"

....outputs...

test24112013
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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