I am attempting to rename a large batch of files to incorporate the dates in the "Date Modified" column from File Explorer into each filename in YYYYMMDD format. I was able to generate a report from the desired file folder of all the files inside using the following steps:
- Click on the Tips and Tricks folder
- Hold down the Shift key and right-click the Tips and Tricks folder
- Click “Open Command Window Here”
- Type
dir>filename.txt
- Click Enter
- Open the Tips and Tricks folder and look for a text file with the filename you created
I then pulled the report into Excel as a delimited text file and manipulated the content so I now have the "last modified" date in one column and the filename in another:
Date Modified | File Name
1/9/2009 | ArcGIS_TT_Projections_Transformations.doc
2/18/2014 | ArcGIS_TT_Re-Projection_WMAS.docx
1/9/2009 | ArcGIS_TT_Set_Selectable_Layers.doc
1/9/2009 | ArcGIS_TT_Spatial_Join.doc
4/21/2010 | ArcGIS_TT_View_CLU_History_Layer.doc
I have only had success using rename scripts like the following:
@echo off
setlocal enableDelayedExpansion
for %%F in ("ArcGIS_TT_Projections_Transformations*.txt") do (
set "name=%%F"
ren "!name!" "!name:ArcGIS_TT_Projections_Transformations=ArcGIS_TT_Projections_Transformations_20090109!"
)
pause
But I am trying to avoid having to repeat that process for 61 files. Does anyone out there have a suggestion of a Windows CMD prompt or a simple .txt that I can use to pull in the date modified information into the filename more automatically?
Thanks for your help!
In Powershell you can do this with a One-Liner:
Explanation:
-Recurse
to get files from sub directories tooThe method
.ToString()
allows you to format as you need itHere is the code you need to use to iterate the text files, obtain the modified date of each file, copy each file using the modified date as part of the file name, and delete the original files. You were very close, but you were missing a few things.
To get the modify date of a file we can use
set "MDate=%%~tF"
.To parse the date that is provided by
set "MDate=%%~tF"
you will need to specify which parts of the value stored by %MDate% you want to use in the output. You can use the commandset "ParsedDate=!MDate:~6,4!!MDate:~0,2!!MDate:~3,2!"
to convert the output ofset "MDate=%%~tF"
from MM/DD/YYYY hh:mm to YYYYMMDD.After that we can copy the files to new files using the value of %ParsedDate% in the filename.
You should see two copies of the files. If you want to delete the original files we just need to run the command
for %%F in ("C:\test\*.txt") do ( del %%F )
. That will leave you with the renamed text files. To convert the .txt.new to .txt use the commandren "C:\test\*.new" *.
.Because we are in a for loop we need to change how we address varables (which based on what you have written so far you already know). We change % to ! for variable names inside a loop. So if you have the variable name
example
you would reference the variable using!example!
inside the loop instead of%example%
.For variables inside the loop to work we also need to add the command
setlocal enabledelayedexpansion
before the loop.That should cover everything. Please feel free to upvote or mark the answer as correct if this solves your problem.
The full batch file is provided below.