?
Solved

Using A Batch File to Append The Date to a Filename

Posted on 2004-10-29
7
Medium Priority
?
6,754 Views
Last Modified: 2012-08-13
I am using Windows 2000 and need to create a batch file to append the date to a filename. The filename is "order.txt" so for example I need to make it "order20041029.txt" - the date format being YYYYMMDD.

Through another example on Experts Exchnage I manage to do the following:

ren d:\orders\order.txt order%date:~7,2%%date:~4,2%.csv

This however gives the result "order1029.txt". Can you a) let me know what I need to do to amend my line of code and b) explain what it means? I would like the answer urgently if possible.

Many thanks
0
Comment
Question by:rabelle
  • 4
  • 2
7 Comments
 
LVL 5

Assisted Solution

by:brianadkins
brianadkins earned 800 total points
ID: 12445075

at a command prompt type:

echo %date%

Then access portions of this resulting string in your batch file like this:

%date:~10,4%  (this yields '2004' for me)

This grabs the substring starting at position 10 and grabs four characters
(first character is zero)

This is how I would do it here, but your %DATE% variable might be ordered differently if not in USA:

ren d:\orders\order.txt order%date:~10,4%%date:~4,2%%date:~7,2%.csv

-Brian


0
 

Author Comment

by:rabelle
ID: 12445311
Brian

Many thanks for that - I've amended the order of the %dates%'s and it works fine. Can you explain what the different numbers mean though?

Thanks
Richard
0
 
LVL 9

Accepted Solution

by:
DrWarezz earned 800 total points
ID: 12445827
Basically, if you have this:

set variable=1234567890

and then you had: echo %variable:~0,2%
that would 'echo' (output):  12

That basically processes everything from 0 to 2.

Equally, if you put:
echo %variable:~3,5%

that would 'echo' (output): 45678

So, the first number means how many to SKIP (from left to right) and then the second number is how many characters (or integers in this case) to process from there on..

Does that make sense? :o)

Best of luck,
[r.D]
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
LVL 9

Expert Comment

by:DrWarezz
ID: 12445846
And then you have more options, such as:

set variable=1234567890

set var=%variable:2=7%

that would replace '2' with '7', so if you then put:

echo %var%

you'd get:

1734567890

That's basically all there is to it ;-)

By the way; I'm just informing you of this, I'm not trying to nick the points.. so, all the points to go to biran ;-)

[r.D]
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12445855
Sorry! I mean Brian :)
0
 

Author Comment

by:rabelle
ID: 12445863
Thanks to both of you for that - apprecaited :o)

Regards
Richard
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12445875
Oh. 8-) Thanks.
[r.D]
0

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.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…

601 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