Append datestamp to all files in a directory

Posted on 2013-09-19
Medium Priority
Last Modified: 2014-10-29
I need a script that will allow me to append the datestamp of each file in a directory to each filename.  For example;

Name                                                         Date Modified
test1.txt                                                      6/18/2013
Joeresume.doc                                            7/12/2013

Would become;

Name                                                         Date Modified
test1_20130618.txt                                                6/18/2013
Joeresume_20130712.doc                                      7/12/2013
Question by:KMKeller

Expert Comment

ID: 39506215
Function GetDateTimeStamp
  Dim strNow
  strNow = Now()
  GetDateTimeStamp = Year(strNow) & Pad2(Month(strNow)) _
        & Pad2(Day(StrNow)) & Pad2(Hour(strNow)) _
        & Pad2(Minute(strNow)) & Pad2(Second(strNow))
End Function

Function Pad2(strIn)
  Do While Len(strIn) < 2
    strIn = "0" & strIn
  Pad2 = strIn
End Function
LVL 60

Accepted Solution

Bill Prew earned 2000 total points
ID: 39506424
Here's a simple small BAT script that should do the job.  It assumes your date format is MM/DD/YYYY, if it isn't we can adjust for that.

Save as a BAT file and adjust the folder name near the top to be the location of the files to rename.  Currently I have the actual REN command with an ECHO in front of it.  SO when you run it for testing it won't actually rename the files, but will just display the rename command it would execute.  This will allow you to verify you are getting the naming you want.

Once it looks good in test mode, remove the word ECHO in front of the REN, and run for real.  Only run it once for real in the folder, or else you will get the date appended multiple times.

@echo off
setlocal EnableDelayedExpansion

set BaseDir=c:\EE\EE28244069\Files

for %%F in ("%BaseDir%\*.*") do (
  set Stamp=%%~tF
  set Stamp=!Stamp:~6,4!!Stamp:~0,2!!Stamp:~3,2!
  ECHO ren "%%~F" "%%~nF_!Stamp!%%~xF"

Open in new window


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…
Loops Section Overview

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question