Posted by SpywareDr on August 29, 2009 at 11:03:18:
In Reply to: inserting a date into a filename using Dos posted by Rina120 on August 27, 2009 at 14:17:21:
: Hello,
: I hopefully have a quick question.
: I need to create a batch file to obvioulsy
: execute a few commands.
: One of the commands is to move a file to a
: specified location.
: Is there anyway I can get the line to name the
: file and automatically insert the date at the
: end of the file?
: For example:
: filename0827.nbk
: Help is MUCh appreciated!
: Thanks in advance!
--
The following *-simple-*, (i.e., no error checking), "DATEFN.BAT" example will do what you want as long as:
1) The filename you want renamed must be specified on the command line
2) The filename you specify must exist in your current directory
3) The filename you specify cannot contain any spaces
4) The filename that the file will ultimately be renamed to cannot already exist in the current directory
--- Cut Here ---
@echo off
for /F "tokens=1-3 delims=/" %%A in ('echo %date%') do (
set mm=%%A
set dd=%%B
set yr=%%C
)
for %%I in (%1) do ren %1 %%~nI%yr%%mm%%dd%%%~xI
for %%x in (mm dd yr) do set %%x=
---- Cut Here ----
For example, if today (August 29, 2009), you had a file named "filename.nbk" in your current directory, issuing the command:
datefn filename.nbk
would rename the file to "filename20090829.nbk".
(The year is first, then the month and day for sorting purposes).
Oh, almost forgot. This is also assuming that if you were to type the command:
date /t
today (August 29, 2009), you would see:
08/29/2009
(E.g., A two-digit Month first, a slash ["/'], then a two-digit Day, another slash ["/"], and finally the four-digit year).