Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using A Batch File to Append The Date to a Filename

Posted on 2004-10-29
7
Medium Priority
?
6,739 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

618 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