Add date to all files in folder

What would be a good dos command that will rename or append the current date to all files in a particular folder?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
Assuming your system date is in MM/DD/YYYY format then the simplest approach would be this BAT file.  This will add _YYYYMMDD to the name of each file in the c:\temp folder.

@echo off
set Today=%Date:~-4%%Date:~-10,2%%Date:~-7,2%
ren "c:\temp\*.*" "*_%Today%.*"

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
breauxlgAuthor Commented:
Thanks Billprew:
If it's not too much to ask would you explain what each line is doing?
Also I would like to add / in the date ie. mm/dd/yyyy
On another note:
It does rename the files in the folder adding the current date, but it strips out the extension. After slight modification. Here is the code I used to achieve the desired solution.

set Today=%Date:~-10,2%%Date:~-7,2%%Date:~-4%
REM Sets the date as MMDDYYYY
ren "c:\temp\*.*" "*_%Today%.txt"
REM renames all files in folder to today's date and adds extension txt
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Bill PrewIT / Software Engineering ConsultantCommented:
I don't think that filenames can contain slashes.

So I think what you might want is:

@echo off
set Today=%Date:~-4%%Date:~-10,2%%Date:~-7,2%
for %%A in ("c:\temp\*.*") do ren "%%A" "%%~nA_%Today%%%~xA"

Open in new window

If that works we can explain it in detail.

breauxlgAuthor Commented:
Definitely works sorry I knew that when I hit submit.
Anyway please explain.
I Bill's absence I hope I can explain what the batch file does.

Windows has a number of fixed System and User Variables that are used for a variety of purposes.  You will see them if you open a CMD window and just type the command    SET
You should see ones like COMPUTERNAME, USERNAME, TEMP, OS, WINDIR, etc.
Not shown are CD, DATE, and TIME.

You can ECHO any of these variables back to screen like this at the command prompt or from within a batch file:

echo It is currently %TIME% and today's date is %DATE%.
echo You are currently using the computer named %COMPUTERNAME%.
echo You are logged in as %USERNAME%.

The %DATE% and %TIME% variables are expanded when called, and instead of echoing the date and time to screen you can use the values that are expanded and do other things with them.

You can also SET something as a variable to use for the duration of the batch file or CMD window, for example:

SET TEXTSTRING=Some Random Text String
echo You have stored the localised variable named TEXTSTRING.
echo The variable contains the text string: %TEXTSTRING%

Be careful using the SET command on the DATE and TIME variables though, because that will prompt you to change the computer's Date and Time.  To see more about the SET command, just type     SET /?

One of the things you can do with variables is Modify them, or more correctly modify the data stored in the variable as it is expanded.  For example I could do this after setting the %TEXTSTRING% variable as above:

echo %TEXTSTRING:~0,-7%

and it would display "Some Random Text" instead of "Some Random Text String".

The  :~  symbols are used as the Variable Modifier, and the   0,-7   just means expand the string stored in the variable beginning at the leftmost character (begins at Zero, not 1) and cut off the last 7 characters.

If I wanted the variable to expand as "Random Text" I would modify %TEXTSTRING% like this:

echo %TEXTSTRING:~5,-7%

 You do not always have to specify the "Y" in the   :~X,Y   modifier.  If you just add do not specify the "Y" then the variable will be expanded beginning at the position set by X" and will include everything to the end of the string held in the variable.  If "X" is a negative digit, then it will begin from the rightmost position rather than the left.  Spaces are included in the counting.

If you type    FOR /?   and then start paging your way down through the help for the FOR command, you will see other methods of modifying variables.

In context with %DATE% variable, you simply take the results of your echoed %DATE% and work out where to start, and how many characters to include.  Because some peoples' Dates don't include leading Zeros and may include the Day, eg. Tue, it may be best to use negatives so that you cound inwards from the Right and then tell it how many characters to include.  The purpose is to miss out the / character which cannot be used in file names.

For example, in a Date format like this:
Tue 04/05/2011
You could replace the / with - and get your MM-DD-YYYY file name like this:
Remember, you may not need the "X" of the   :~X,Y   so for 2011 you could just use   %DATE:~-4%   and it will backspace in 4 characters from the right and then retain the 2 right to the end of the string.

The FOR command used by Bill (use % typed into a CMD window and %% in a batch file), does this:

for %%A in ("c:\temp\*.*") do ren "%%A" "%%~nA_%Today%%%~xA"

For every file of any type (*.*) found in "C:\Temp", the name of which will be temporarily stored as %%A each time the command loops, rename that file as follows:

Type   FOR /?   and page right to the end where you will see all the methods of modifying a variable n a FOR loop.

%%~nA   modifies   %%A   to the file name minuse the file extension.
%%~xA    modifies    %%A   to the dotted extension of the file.

The %%A can be any letter you wish, as long as you recall it using the same letter.  It is case-sensitive, so %%a is not the same as %%A.  When used with modifiers like ~n  ~x   it makes the variable letter more apparent if you use an uppercase letter.

I hope I have explained this sufficiently to make sense in Bill Prew's absence.

Bill PrewIT / Software Engineering ConsultantCommented:
I see BillDL has added a bit of explanation (while I was sleeping :-) ), let me know if anything is still unclear.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.