1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Append a date to a filename in DOS

Discussion in 'DOS/Other' started by cgjoker, Jan 26, 2005.

Thread Status:
Not open for further replies.
Advertisement
  1. cgjoker

    cgjoker Thread Starter

    Joined:
    Aug 13, 2003
    Messages:
    205
    Hi there. Me again.

    Thought id put out a new thread to see if anyone can help me.

    Id like to rename a file in DOS to include 'date /T' in the filename.

    Anyone?

    thanks, cg.
     
  2. squidboy

    squidboy

    Joined:
    Dec 29, 2004
    Messages:
    403
    Howdy,

    I remember your previous thread. It's somewhat tricky because those commands will output '/' and such which can't be used in file names.

    I would suggest a site but there are many with folks that live and breathe this stuff /giggle. Just do search in google for dos batch files or scripting and that should get you started.
     
  3. plejon

    plejon

    Joined:
    Jul 26, 2001
    Messages:
    665
    Try to rename the file using the date and time.

    ren file.txt file-%date%-%time%.txt

    I had some issues with the date not being displayed in the exact format I wanted, but you can use substrings and other techniques to get exactly what you want

    E.g. : ren file.txt file%date:~3,10%-%time%.txt

    For more info on the %date% and %time% variables and substringing, just open a Command Prompt box and type set /?
     
  4. cgjoker

    cgjoker Thread Starter

    Joined:
    Aug 13, 2003
    Messages:
    205
    i actually figured it out though. well, with some help. here is the syntax i ended up with..

    ren tmp.txt tmp%date:~4,2%%date:~7,2%%date:~10,4%
     
  5. fmgazette

    fmgazette

    Joined:
    Feb 22, 2005
    Messages:
    1
    Thanks for this guys - this was just what I was looking for too.
    I used...

    %DATE:/=%

    ...to remove the offending / characters.
     
  6. workingman

    workingman

    Joined:
    Mar 16, 2005
    Messages:
    6
    I got the idea from this site as to how to append date/time. I noticed that when you execute the ren dos command makes a difference. See bat file below.


    Echo Off
    IF Not EXIST n:\dbfs\doekasp.txt GOTO DoNothing

    copy n:\dbfs\doekasp.txt n:\history\doekasp.txt

    Rem rename command is done twice (2) to allow for 1 or 2 digit hour,
    Rem If before 10am (1digit) hour Rename starting at location (0) for (2) chars,
    Rem will error out, as location (0) will have a space
    Rem and space is invalid character for file name,
    Rem so second remame will be used.
    Rem
    Rem if equal 10am or later (2 digit hour) then first remame will work and second will not
    Rem as doekasp.txt will not be found (remamed)


    ren n:\history\doekasp.txt doekasp-%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~0,2%h%time:~3,2%m%time:~6,2%s%.txt
    ren n:\history\doekasp.txt doekasp-%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~1,1%h%time:~3,2%m%time:~6,2%s%.txt



    :DoNothing
     
  7. pdxbda

    pdxbda

    Joined:
    Jun 7, 2005
    Messages:
    2
    I've tried:

    ren h:\QMDB_Archive\QMDB_be.mdb QMDB_be-%date:~4,2%-%date:~7,2%-%date:~10,4%.mdb

    ...and I get a syntax error message. Where am I going wrong?
     
  8. cgjoker

    cgjoker Thread Starter

    Joined:
    Aug 13, 2003
    Messages:
    205
    try removing the " - " in front of the %date ... so...

    ren h:\QMDB_Archive\QMDB_be.mdb QMDB_be%date:~4,2%%date:~7,2%%date:~10,4%.mdb
     
  9. pdxbda

    pdxbda

    Joined:
    Jun 7, 2005
    Messages:
    2
    Thanks. But, I still get the error message. I'm using XP. Would that affect the dos commands?
     
  10. workingman

    workingman

    Joined:
    Mar 16, 2005
    Messages:
    6
     
  11. workingman

    workingman

    Joined:
    Mar 16, 2005
    Messages:
    6
    ren n:\history\doekasp.txt doekasp-%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~0,2%h%time:~3,2%m%time:~6,2%s%.txt

    ren n:\history\doekasp.txt doekasp-%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~1,1%h%time:~3,2%m%time:~6,2%s%.txt


    This is the output from commands above: doekasp-06-07-2005_@_8h01m09s.txt(note that "_" is in front of and behind "@" editor here displays all text as underlined)

    Note that if you use time in your file name.. be aware that you need to code for 1 and or 2 digit hour.

    I had to put some dos pause commands to view what was going on and make sure I was not using invalid char(s) for file names.

    Ps.. go to dos prompt and enter Date command and then Time command to see character strings your parsing in the rename command when your building your file name.

    For Date: Wed 06/08/2005 You start counting from 0 starting with Wed and the add 1 as you move to the right.. The 5th char would be the nbr 6 in this example.

    Do the same for time. just remember that you can not take first 2 chars. of time when time is displaying 1 digit hour as you are trying to rename your file with a space+number and space is invalid character for filename.
     
  12. Mark180

    Mark180

    Joined:
    Jul 7, 2005
    Messages:
    3
    Hi

    I have trying to use the above solution and have come up with this syntax

    REN E:\Sx3\FTP\pl.txt pl%date:~10,4%%date:~7,2%%date:~4,2%%time:~1,2%%time:~3,2%%.txt
    ren E:\Sx3\FTP\llpg.txt llpg%date:~10,4%%date:~7,2%%date:~4,2%%time:~1,2%%time:~3,2%%.txt
    pause

    the required output is plYYYYMMDDHHmm.txt and llpgYYYYMMDDHHmm.txt

    this runs fine from my machine - but the syntax is incorrect when i run from the server - I'm a complete novice in this area - can anyone help?

    (NB I have tried a simple REN E:\Sx3\FTP\pl.txt mark.txt which works fine)

    Thanks
     
  13. Mark180

    Mark180

    Joined:
    Jul 7, 2005
    Messages:
    3
    Finally got it sussed used the following for my rename.bat

    @echo off
    for /F "tokens=2-4 delims=/ " %%i in ('date /t') do set yyyymmdd=%%k%%i%%j
    echo Date: %yyyymmdd%
    for /F "tokens=1-2 delims=: " %%l in ('time /t') do set hhmm=%%l%%m
    echo Time: %hhmm%
    rem -- or use both
    echo %yyyymmdd%%hhmm%
    rem -- here is the copy command with just the date appended

    REN E:\Sx3\FTP\pl.txt pl%yyyymmdd%%hhmm%.txt
    REN E:\Sx3\FTP\llpg.txt llpg%yyyymmdd%%hhmm%.txt
     
  14. Mark180

    Mark180

    Joined:
    Jul 7, 2005
    Messages:
    3
    oh that isn't working!

    2 questions

    1) why is my month and day coming out in the wrong position in the file name (it comes out yyyyddmm - i want yyyymmdd)

    2) why if the scirpt is run at 0430 is the time hhmm coming out as 430 not 0430
     
  15. rch

    rch

    Joined:
    Aug 15, 2008
    Messages:
    1
    If I type in the following command line in the dos window:

    7z a -xr!*.tmp C:\backupfiles_%date:/=%.zip D:\backupfiles\

    I will get the file name correctly as ==> backupfiles_08152008.zip

    But if I run the same exactly command line in the schedule job in a bak.bat or bak.cmd file, the file name became ==> backupfile_Fri.7z

    Any idea why?
    I am using the freeware 7-zip in the command line. Suggestion is very welcome if something better than 7-zip and also free I can use as command line in the scheduled job.

    Thank you very much!
     
  16. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/323716