Batch File - Renaming File with DATE

Discussion in 'Computer Support' started by Tom, Mar 3, 2007.

  1. Tom

    Tom Guest

    Hey guys,

    I have a batch file which does the following:

    copy LOG.LOG LOG.txt. This creates a copy of the original with a txt
    extension. But to prevent it from being overwritten the next time the
    file executes, I would like it to be copied with a new name which has
    the current date. So the end result will be "LOG20070303.txt"

    Is there an easy way to do this?


    Tom, Mar 3, 2007
    1. Advertisements

  2. Tom

    Pennywise Guest

    Start here:

    @echo off&SETLOCAL

    :: This will return date into environment vars
    :: Works on any NT/2K/XP machine independent of regional date
    :: 20 March 2002

    FOR /f "tokens=1-4 delims=/-. " %%G IN ('date /t') DO (call
    :s_fixdate %%G %%H %%I %%J)
    goto :s_print_the_date

    if "%1:~0,1%" GTR "9" shift
    FOR /f "skip=1 tokens=2-4 delims=(-)" %%G IN ('echo.^|date') DO (
    set %%G=%1&set %%H=%2&set %%I=%3)
    goto :eof

    MD %mm%%dd%%yy%
    ENDLOCAL&SET mm=%mm%&SET dd=%dd%&SET yy=%yy%

    :: see also
    :: from
    Pennywise, Mar 3, 2007
    1. Advertisements

  3. Tom

    why? Guest

    This will be for a MSDOS/Windows OS based system?
    Many, see past posts in 24HSHD
    about batch files, many contain links to batch file sites and


    Also* , 3 groups

    not forgetting or even

    It's easy in a batch file, although for the sake of neatness I used to
    use a freeware exe program years ago that did the hard work of parsing
    out the date / time to h:m:s d:m:d variables for you.

    why?, Mar 4, 2007
  4. Tom

    thanatoid Guest

    "Easy way to do this" is NOT what the two prior replies provide,
    IMO. I am not absolutely certain what you exactly want to do
    (yes, I'm stoopid) but I am pretty sure the file manager from can do it. I am NOT spamming, I own a copy and
    it is the best. I weep for you Windows Explorer lusers.

    Or you could try one of the many renaming utilities out there.

    Not that there's anything wrong with simple batch files, I love
    them, but they are not particularly easy to write or use for
    most people.
    thanatoid, Mar 4, 2007
  5. Tom

    why? Guest

    Exactly required is to add ymd to a filename.

    It's very simple, this is the easy way -

    newlogfile_name = filename%y%m%d

    That's all that's needed.

    Rather than making the batch file longer with the working solution given
    by pennywise, I said I prefer to use a free exe program, which I setup 4
    years ago and can't remember the name just now.
    If it has a command line for use in a batch file, great, Then again the
    OP could most likely find a DOS/Win cms line version of touch.exe.
    Rename is 1 thing, getting the date into the rename is another.
    So your point is? With a cut / paste example it's easy

    OP asking for help and getting it, mind you OP didn't have to ask, just
    searchiung old posts in 24hshd for my nick as the author will find
    several posts with lots of links to batch scripting sites and ready to
    use examples for exactly the request asked.

    why?, Mar 4, 2007
  6. Tom

    thanatoid Guest

    I didn't read every character, but 35 lines of code is not
    "easy" for most people - myself included. I SAID I'm stupid.
    Not sure what you mean by "for use in a batch file" but it does
    give you a command line with one key press. And remembers what
    you did before - up to a limit you can set, IIRC.

    It also has a batch-rename tool which includes the date option
    and is generally better than quite a few of the specific
    renamers I have tried.
    There are so many renamers I am sure it must have occurred to
    ONE of the authors to include the date as an option.
    My point is I'm stoopid and I should get off the usenet.

    AFA batches, using - yes, writing - no. Only Microsoft writes
    their "programs" by cutting and pasting 10-20 year old code.
    That is very true - the fact that almost every question has
    already been asked and answered and yet most people would rather
    post again and wait than spend a few moments searching continues
    to amaze me.
    thanatoid, Mar 5, 2007
  7. Tom

    Rolf Guest

    I don't use 24h. very often, but I am impressed by the amount of expertise
    available to me at 'just a mouse click'.

    But WRT searching, I am not quite sure how I could search for a solution to
    a particular problem. Maybe I am ignorant, but stupid I am not. Besides, at
    76, I may be a bit lazier than most of you....

    But seriously, Would it be possible to create instructions on how one may
    search the forum for a particular problem solution? I don't think it is
    possible to create a 'sticky' message like used on web based forums, but
    what about posting the instructions on a regular basis twice a month?

    Rolf, Mar 5, 2007
  8. Tom

    Pennywise Guest

    Google archives all the text groups, this group:

    Say you have a question like oh maybe
    "OE removed access to the following unsafe attachments"
    (note quotes) would bring up

    Main Google Group page

    You use the same search operators as regular Google

    Some groups -not this one- supply FAQ's to answer the most common

    No Sticky notes, just bookmark the ones of interest.
    Pennywise, Mar 5, 2007
  9. Tom

    why? Guest

    This forum? It isn't it's a newsgroup. If you are accessing it via

    There is a help page
    with a section on searching

    The ultra basics are ,

    - enter some appropriate words

    - if too many matches prefix a wored with - , as in

    dogs -hound

    - If needing to search for a specific word use a +

    dogs +cats

    Or and search for

    google search

    to see the various,

    Google Image Search
    Google Desktop Search
    Google Blog Search
    Google Book Search

    Add another keyword,

    google search usenet
    Make your own, text file on desktop is simple enough.
    why?, Mar 5, 2007
  10. Tom

    why? Guest

    If it's a copy/past of a working example, problem solved. Many of the
    examples from batch file sites are annotated showing how it works.

    Part of the OP request, say the filemanger you mentioned above is called


    The op would put in the batch file

    ghisler.exe /rename LOG.txt LOGyyyymmdd.txt

    The filemanger ghisler would inset the values yyyymmdd automatically.

    Opening up a filemanager and renaming the file manually would work but
    it wouldn't be automated.
    You just answered your question, however the tool must run with out
    launching the tool and waiting for user input.

    That's more in line with the example I use,

    commandline-utility puts the date / time into environment varaibles
    next line in batch file renames file using %yyyy %mm %dd

    Not needing 35 lines of code.


    why?, Mar 5, 2007
  11. Tom

    Pennywise Guest

    My post was a bat file I download a long time ago, Yes it was a
    working copy, posted mainly for the Links.

    I was going to make a brain dead way for my Mom to connect her camera
    to the computer, run like copy.bat and have it move each pix from the
    camera to a directory of the days date; appending each file with the
    date as well. - how I came across the batch file.

    She found a GUI program she could understand (to an extent) and wanted
    to use that instead (just a copy from camera to a computer directory)
    so the example just sits in my INFO directory...
    Pennywise, Mar 5, 2007
  12. Tom

    why? Guest

    :) often the way of things

    I wouldn't have liked to think what the thread turned into if I posted
    the perl script I use many times to do the samething,

    Oh and found the app, I used before,


    why?, Mar 5, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.