Forum Discussion
pspBugs
May 14, 2022Copper Contributor
Rename files to get the year, month and day of the creation in front of the file?
Hi all, I am a trainee and need to make some changes to the file naming convention. There are tens of thousands of files in the filesystem and it would be a long day to do it all manually. So my qu...
- May 14, 2022
Here's a three-line example on how you can do this. I've gone with the assumptions that:
- The prefix date format should be "yyyyMMdd_";
- You do indeed want the creation date used in the prefix, not the last modified date.
$ParsedDate = [datetime]::MinValue; $Path = "D:\Data\SomePath"; Get-ChildItem -File -Path $Path -Recurse | Where-Object { (-not [regex]::IsMatch($_.Name, "^\d{8}_", [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)) -or (-not [datetime]::TryParseExact($_.Name.Substring(0, 8), "yyyyMMdd", [cultureinfo]::CurrentCulture, [System.Globalization.DateTimeStyles]::None, [ref] $ParsedDate)) } | ForEach-Object { Rename-Item -Path ($_.FullName) -NewName "$($_.CreationTime.ToString("yyyyMMdd"))_$($_.Name)"; }Cheers,
Lain
LainRobertson
May 15, 2022Silver Contributor
I just noticed your second comment about existing prefixes not being ignored.
I'm not seeing that reflected when I run it multiple times over the test files. It's putting the prefix in the first time then ignoring it on subsequent runs.
Maybe you can drop an example into this thread so we can get an idea of what's going on behind the scenes?
If you could drop in the following for one of the repeatedly-changing files, that might help:
- File name;
- File creation date;
- Which version of PowerShell you're running (I'm running under Windows PowerShell 5.1.)
You can get points 1 and 2 via:
Get-Item -Path ".\SomeFileName" | Select-Object -Property Name, CreationTime
You can get point 3 from the following:
$PSVersionTable
Cheers,
Lain
pspBugs
May 16, 2022Copper Contributor
First of all sorry for the late answer.
I tried the script at home first and found this strange behavior I discribed above. But at work, and now at home, I could not reproduce it again.
To answer the question, here are the answers from Powershell for questions 1 to 3.
"PS C:\WINDOWS\system32> Get-Item -Path "C:\Users\user\Downloads\jhjk\Test\20220514_yyyyMMdd_rename.ps1" | Select-Object -Property Name, CreationTime
$PSVersionTable"
Name CreationTime
---- ------------
20220514_yyyyMMdd_rename.ps1 05/16/2022 21:04:34
Name Value
---- -----
PSVersion 5.1.19041.1645
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.19041.1645
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
So Thank you for the Help.
pspBugs
I tried the script at home first and found this strange behavior I discribed above. But at work, and now at home, I could not reproduce it again.
To answer the question, here are the answers from Powershell for questions 1 to 3.
"PS C:\WINDOWS\system32> Get-Item -Path "C:\Users\user\Downloads\jhjk\Test\20220514_yyyyMMdd_rename.ps1" | Select-Object -Property Name, CreationTime
$PSVersionTable"
Name CreationTime
---- ------------
20220514_yyyyMMdd_rename.ps1 05/16/2022 21:04:34
Name Value
---- -----
PSVersion 5.1.19041.1645
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.19041.1645
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
So Thank you for the Help.
pspBugs