I'm a complete noob when it comes to powershell but I've been struggling with this one all day and not sure why.
Each day, I will have a set of files in a directory. The files will be different names and different extesions so they are all unique. I'm trying to rename all of those files to contain the same initial file name and then put the current date time at the end and not include an extension. Below is what I have. I browse to the directory my files reside in, create my variable for getting the current date time and formatted the way I want. I then issue my command to rename and it always errors out after doing the first file successfully as it tries to edit the first file again which was already renamed. Any help would be greatly appreciated. Thanks in advance.
Example File Names to be renamed
test123.dat
341test.txt
983asdf.doc
Desired End Result which each file being unique with the millisecond that you can see as the end 4 numbers in the file name below:
abcabcp123456789d202004271552001023
abcabcp123456789d202004271552001024
abcabcp123456789d202004271552001025
What I tried:
$curDateTime = Get-Date -Format yyyyMMddTHHmmssffff
Get-ChildItem *.* | Rename-Item -NewName {'abcabcp123456789d' + $curDateTime}
If I run a -WhatIf, it says it works fine with no errors. When I actually run it, it says: Rename-Item : Cannot create a file when that file already exists.
Is it too quick even for the milliseconds? In the -WhatIf, even though it says there aren't errors, all three files show up with the same name. Any way around that. Thanks again.
Updated Variation I tried running:
Get-ChildItem *.* | ForEach {
Rename-Item -Path $_.FullName -NewName "$($_.DirectoryName)\$('abcabcp123456789d')$(Get-Date -F yyyyMMddTHHmmssffff)"
}
Tried the code with parenthsis:
(Get-ChildItem *.*) | ForEach { Rename-Item -Path $_.FullName -NewName "$($_.DirectoryName)\$('abcabcp123456789d')$(Get-Date -F yyyyMMddTHHmmssffff)"}
New code with parenthesis that didn't work. I received an error of:
Rename-Item : Cannot create a file when that file already exists. At line:1 char:33 + (Get-ChildItem .) | ForEach { Rename-Item -Path $.FullName -NewName "$($.Dir ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (F:\abc\Test\Upl...00415-b-dbo:String) [Rename-Item], IOException + FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand