Address
304 North Cardinal St.
Dorchester Center, MA 02124
Work Hours
Monday to Friday: 7AM - 7PM
Weekend: 10AM - 5PM
Address
304 North Cardinal St.
Dorchester Center, MA 02124
Work Hours
Monday to Friday: 7AM - 7PM
Weekend: 10AM - 5PM
I wrote (modified) a folder date rename script recently, and I wanted to share it.
First, I was trying to organize the files and folders on my new NAS.
Unfortunately, some of my folders were in the format of M/D/YYYY. If you are familiar with sorting, you know that this wouldn’t put them in chronological order.
In this case, I needed to change this format to YYYY/M/D.
During my research, I found a great PowerShell script that I could easily change.
As you can see, my original folder structure was not in chronological order.
PS F:\GameScreenshots\_Dated> dir Directory: F:\GameScreenshots\_Dated Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 4/7/2012 2:01 PM 10-1-2008 d----- 4/7/2012 2:01 PM 10-10-2008 d----- 4/7/2012 2:01 PM 10-16-2008 d----- 2/17/2011 6:25 PM 10-2-2009 d----- 2/17/2011 6:25 PM 10-23-2009 d----- 4/7/2012 2:01 PM 10-24-2008 ... <snip> ... d----- 4/20/2014 2:58 PM 2-23-2012 d----- 4/20/2014 2:58 PM 2-24-2012 d----- 4/20/2014 2:58 PM 2-25-2012 d----- 4/20/2014 2:58 PM 2-26-2012 d----- 2/17/2011 6:24 PM 3-26-2008 d----- 2/17/2011 6:24 PM 6-27-2008 d----- 4/7/2012 3:26 PM 6-28-2006 d----- 2/17/2011 6:24 PM 6-3-2008 d----- 2/17/2011 6:25 PM 8-13-2009
First, I executed my modified script, and it properly renamed my folders.
PS F:\GameScreenshots\_Dated> .\rename.ps1 Processing 10-1-2008 New folder name: 2008-10-1 ================= Processing 10-10-2008 New folder name: 2008-10-10 ================= Processing 10-16-2008 New folder name: 2008-10-16 ================= Processing 10-2-2009 New folder name: 2009-10-2 ================= ... <snip> ... Processing 4-6-2012 New folder name: 2012-4-6 ================= Processing 4-7-2012 New folder name: 2012-4-7 ================= Processing 4-8-2012 New folder name: 2012-4-8 ================= Processing 5-1-2008 New folder name: 2008-5-1 ================= PS F:\GameScreenshots\_Dated> dir
Finally, when it finished, my folder organization was far better.
Directory: F:\GameScreenshots\_Dated Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 4/7/2012 3:26 PM 2006-6-28 d----- 4/7/2012 2:01 PM 2008-10-1 d----- 4/7/2012 2:01 PM 2008-10-10 d----- 4/7/2012 2:01 PM 2008-10-16 ... <snip> ... d----- 2/17/2011 6:25 PM 2009-9-9 d----- 4/20/2014 2:58 PM 2012-2-22 d----- 4/20/2014 2:58 PM 2012-2-23 d----- 4/20/2014 2:58 PM 2012-2-24 d----- 4/20/2014 2:58 PM 2012-2-25 d----- 4/20/2014 2:58 PM 2012-2-26 d----- 4/20/2014 2:58 PM 2012-3-31 d----- 4/20/2014 2:58 PM 2012-4-1 d----- 4/20/2014 2:58 PM 2012-4-2 d----- 4/20/2014 2:58 PM 2012-4-6 d----- 4/20/2014 2:58 PM 2012-4-7 d----- 4/20/2014 2:58 PM 2012-4-8 -a---- 4/20/2020 5:29 PM 1235 rename.ps1
First, I changed the original regex, as my files were different from the original blog post.
My new strRegEx was as follows.
^\d{1,2}-\d{1,2}-\d{4}
Other than that, I removed any unnecessary lines and the logging from the original script.
You can find my modified version below, but all credit goes to the original author.
# Initialize Parameters/Variables # Root of target folder $strDirectoryPath = split-path -parent $MyInvocation.MyCommand.Definition # Delimiter to split date by (e.g. "/") $strDelimiter = "-" # RegEx for date format ("##-##-####" i.e.: "6-24-2019") $strRegEx = "^\d{1,2}"+$strDelimiter+"\d{1,2}"+$strDelimiter+"\d{4}" $aFolderList = $null $aSplitFileName = @() $strNewDate = $null $strNewFolderName = $null # Get folder list $aFolderList = Get-ChildItem -Path $strDirectoryPath -Directory FOREACH ($objFolder in $aFolderList){ Write-Host "Processing "$objFolder.Name IF ($objFolder.Name -match $strRegEx){ $aSplitFileName = $objFolder.Name.Split($strDelimiter) $strNewFolderName = $objFolder.Name -replace $strRegEx,($aSplitFileName[2] + $strDelimiter + $aSplitFileName[0] + $strDelimiter + $aSplitFileName[1]) Write-Host "New folder name: "$strNewFolderName Rename-Item -path $strDirectoryPath\$objFolder -newName $strNewFolderName } ELSE { Write-Host $objFolder.Name" doesn't match the RegEx and will be skipped" -BackgroundColor Red } Write-Host "=================" # Clear variables for next pass $objFolder = @() $aSplitFileName = @() $strNewFolderName = $null }
This was a simple script, but it made organizing some screenshot folders much easier on my NAS.
I’m still using under 10% of my new storage, which is awesome. Stay tuned for even more lab upgrades and updates though!
I don’t plan on sharing this to my GitHub, as it is someone else’s script that I barely changed.
That said, if I use it for more tasks, then I may make it slightly more generic and share it then.
Ray Doyle is an avid pentester/security enthusiast/beer connoisseur who has worked in IT for almost 16 years now. From building machines and the software on them, to breaking into them and tearing it all down; he’s done it all. To show for it, he has obtained an OSCE, OSCP, eCPPT, GXPN, eWPT, eWPTX, SLAE, eMAPT, Security+, ICAgile CP, ITIL v3 Foundation, and even a sabermetrics certification!
He currently serves as a Senior Staff Adversarial Engineer for Avalara, and his previous position was a Principal Penetration Testing Consultant for Secureworks.
This page contains links to products that I may receive compensation from at no additional cost to you. View my Affiliate Disclosure page here. As an Amazon Associate, I earn from qualifying purchases.