Rename files to add date modified to filename with Windows CMD or simple .TXT

43,959

Solution 1

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

Solution 2

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
Share:
43,959

Related videos on Youtube

newbie
Author by

newbie

Updated on September 18, 2022

Comments

  • newbie
    newbie over 1 year

    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!

    • user5870571
      user5870571 almost 8 years
      Have you looked at adding the command dir /T:W pathtofile to your batch file? windows-commandline.com/get-file-modified-date-time
    • user5870571
      user5870571 almost 8 years
      If you want more information read the link I included. The command I provided you will give you the modified date of a file.
    • newbie
      newbie almost 8 years
      @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!
  • user5870571
    user5870571 almost 8 years
    You are welcome. Please let me know if you need anything else.
  • Swizzler
    Swizzler about 5 years
    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?
  • EmSixTeen
    EmSixTeen over 3 years
    This was perfect, exactly what I'm looking for - Didn't even need to adjust it. Thanks.
  • wolfram77
    wolfram77 almost 3 years
    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.
  • Yogwhatup
    Yogwhatup about 2 years
    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?