Using A Batch File to Append The Date to a Filename

Posted on 2004-10-29
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
Question by:rabelle
    LVL 5

    Assisted Solution


    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



    Author Comment


    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?

    LVL 9

    Accepted Solution

    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,
    LVL 9

    Expert Comment

    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:


    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 ;-)

    LVL 9

    Expert Comment

    Sorry! I mean Brian :)

    Author Comment

    Thanks to both of you for that - apprecaited :o)

    LVL 9

    Expert Comment

    Oh. 8-) Thanks.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Course: MongoDB Object-Document Mapper for NodeJS

    NodeJS (JavaScript on the server) is awesome, but some developers get confused about NoSQL when it comes to working in Node with MongoDB (NoSQL database). Do you need a better explanation of how to use Node.js with MongoDB? The most popular choice is the Mongoose library.

    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…
    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…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video discusses moving either the default database or any database to a new volume.

    877 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now