6

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:

  1. Click on the Tips and Tricks folder
  2. Hold down the Shift key and right-click the Tips and Tricks folder
  3. Click “Open Command Window Here”
  4. Type dir>filename.txt
  5. Click Enter
  6. 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!

newbie
  • 63
  • 1
  • 1
  • 5
  • Have you looked at adding the command `dir /T:W pathtofile` to your batch file? http://www.windows-commandline.com/get-file-modified-date-time/ – user5870571 May 13 '16 at 19:02
  • @user5870571, Sorry, I'm a newbie - what is "/T:W"? and where would I incorporate that? – newbie May 13 '16 at 19:14
  • If you want more information read the link I included. The command I provided you will give you the modified date of a file. – user5870571 May 13 '16 at 19:15
  • @user5870571, I read the link, but I am not sure where I would add `dir /T:W pathtofile` into a rename script. I have all the last modified dates already - I'm just trying to add them into the filename. Please demo in code sample if you can - I really am a newbie. Thanks! – newbie May 13 '16 at 19:31
  • You've requested a CMD solution "or a simple .txt," which I don't understand. But in any case, I would use Excel for this. FIND the first slash, FIND the first space (separating date from filename), etc. This link illustrates the basic ideas: https://raywoodcockslatest.wordpress.com/2021/07/02/excel-batch-move/ – Ray Woodcock May 28 '22 at 01:39

2 Answers2

8

In Powershell you can do this with a One-Liner:

Get-ChildItem |Foreach-Object { Rename-Item $_ -NewName ("{0}-{1}{2}" -f $_.BaseName,$_.LastWriteTime.ToString('yyyyMMdd'),$_.Extension)  }

Explanation:

  • Get-ChildItem: Gets all items in the directory. You could add -Recurse to get files from sub directories too
  • Foreach-Object: Run the following code block with each file
  • $_: The current iterated file as an object
  • $_.BaseName: The filename without extension
  • $_.LastWriteTime: The last write time as a DateTime object
    The method .ToString() allows you to format as you need it
  • $_.Extension: The extension of the file
Gerald Schneider
  • 19,757
  • 8
  • 52
  • 79
  • This was perfect, exactly what I'm looking for - Didn't even need to adjust it. Thanks. – EmSixTeen Dec 30 '20 at 17:41
  • 1
    For sorting files by time try `Get-ChildItem |Foreach-Object { Rename-Item $_ -NewName ("{0}{1}" -f $_.LastWriteTime.ToString('hhmmss'),$_.Extension) }`. I needed to sort images for creating GIF. – wolfram77 May 31 '21 at 13:47
3

Here 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 command set "ParsedDate=!MDate:~6,4!!MDate:~0,2!!MDate:~3,2!" to convert the output of set "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 command ren "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.

setlocal enabledelayedexpansion

for %%F in ("C:\test\*.txt") do ( 
    set "MDate=%%~tF"
    set "ParsedDate=!MDate:~6,4!!MDate:~0,2!!MDate:~3,2!"
    REM To add time
    set "ParsedDate=!MDate:~6,4!!MDate:~3,2!!MDate:~0,2!!MDate:~11,2!!MDate:~14,2!"
    copy %%F %%~dpnF_!ParsedDate!%%~xF.new )

for %%F in ("C:\test\*.txt") do ( 
    del %%F )

ren "C:\test\*.new" *.

exit
user5870571
  • 2,900
  • 2
  • 11
  • 33
  • 1
    You are welcome. Please let me know if you need anything else. – user5870571 May 16 '16 at 15:56
  • is there a way to also display the hour/time (wont work on my machine because of the : ) and put the date before the filename? – Swizzler Feb 17 '19 at 11:47
  • This script seems to work very well, however with file names that include spaces, it does not proceed with the renames. How can we correct this script to account for spaces in file names? – Yogwhatup Feb 16 '22 at 12:28