Rename file using variables... in DOS!
Page 1 of 2 1 2 LastLast
Results 1 to 15 of 30

Thread: Rename file using variables... in DOS!

  1. #1
    Registered User techleet's Avatar
    Join Date
    Oct 2000
    Location
    San Jose, CA, USA
    Posts
    459

    Rename file using variables... in DOS!

    Hey guys

    Does anyone know if it's possible to rename a file in NT DOS using variables? I've tried but it gives an incorrect syntax error.
    Here's what I tried:

    - ren bob.txt bob%date%.txt
    - ren bob.txt bob%date%
    - ren bob.txt %date%
    - set bob %date%; ren bob.txt %bob%

    None worked. I was thinking of redirecting %date% to a txt file or something, but there's no command in dos to read from a file. Horrible!

    Any ideas?

    Thanks!!
    DON'T PANIC

  2. #2
    Registered User
    Join Date
    Oct 2000
    Posts
    1,569
    HERE is a good page that shows how to use DOS variables as well as reading text files. there are also links to some freeware progs that make variable manipulation easier.

  3. #3
    Registered User Chris_MacMahon's Avatar
    Join Date
    Nov 2001
    Location
    sebago, maine
    Posts
    568

    try this

    try rename instead of ren
    i love peta...and sars...
    and bin laden....and n. korea....and china...and p2p...spyware...

  4. #4
    Tech-To-Tech Mod kato2274's Avatar
    Join Date
    Sep 2001
    Location
    Bentleyville, Pa
    Posts
    2,317
    perhaps a small visual basic app.
    Nonsense prevails, modesty fails
    Grace and virtue turn into stupidity - E. Costello

  5. #5
    Registered User techleet's Avatar
    Join Date
    Oct 2000
    Location
    San Jose, CA, USA
    Posts
    459

    Re: try this

    Originally posted by Chris_MacMahon
    try rename instead of ren
    REN and RENAME are the same command, but thanks!
    DON'T PANIC

  6. #6
    Registered User Chris_MacMahon's Avatar
    Join Date
    Nov 2001
    Location
    sebago, maine
    Posts
    568
    on my machine when i type ren balh.bat hlab.bat i get bad command or file name...
    when i type rename blah.bat halb.bat it's succesfull

    win xp pro
    i love peta...and sars...
    and bin laden....and n. korea....and china...and p2p...spyware...

  7. #7
    Banned Ya_know's Avatar
    Join Date
    Jun 2001
    Posts
    10,692
    Originally posted by Chris_MacMahon
    on my machine when i type ren balh.bat hlab.bat i get bad command or file name...
    when i type rename blah.bat halb.bat it's succesfull

    win xp pro
    Techleet is correct, both are the exact same command.

  8. #8
    Registered User
    Join Date
    Aug 2002
    Location
    Colorado
    Posts
    26
    It often helps when using rename operations to change the extension at the same time. eg

    ren bob*.txt bob%date%.ttt
    ren bob*.ttt bob*.txt

    The Batch Masters over at alt.msdos.batch could get you going right off, although the question and answer is no doubt to be found just searching that awesome group.

    Batch is _not_ an unstructured language - its a cult

  9. #9
    Junior Member Taz-nc's Avatar
    Join Date
    Mar 2003
    Location
    Hendersonville, NC
    Posts
    1
    I don't think the %date% variable will work.
    You can't have a forward slash in a file name.

    I tried this in a win Xp Dos box
    echo %date%

    This was the output
    Wed 03/19/2003


    Try another variable.

    Good Luck !

  10. #10
    Driver Terrier NooNoo's Avatar
    Join Date
    Dec 2000
    Location
    UK
    Posts
    31,824
    You can, however, change the date separator in Regional and Language options to a period or a dash, the dash should get around it?
    Never, ever approach a computer saying or even thinking "I will just do this quickly."

  11. #11
    Registered User craigmodius's Avatar
    Join Date
    Sep 2001
    Location
    Hellmira, NY, USA
    Posts
    1,572
    Well this is for Windows 98.

    But he has some NT stuff for putting date into the environment variable available here

    I've set up a .bat at work under 98 that copies a logfile from one PC to another and renames it to the corresponding date.

    It looks something like this...

    @echo off
    echo.|date|find "Current" >cu##ent.bat
    echo set date=%%4> current.bat
    call cu##ent.bat
    del cu??ent.bat > nul

    copy /y \\workstation\share\nightly.txt \\otherpc\logfiles\"%date%-nightly.log"

    @echo off
    cls


    However, I've tried setting this up on a XP machine and an NT 4 machine so that the .bat file runs on the XP machine and copies down to itself however no luck, but I'm no DOShead, maybe you or someone else can shed some light from the above links.

    There are alotta good links from that guys site

    http://www.ericphelps.com

    ~good luck
    Last edited by craigmodius; March 20th, 2003 at 09:14 PM.
    "And just when I thought today couldn't get anymore poo-like." -Outcoded

  12. #12
    Junior Member grief's Avatar
    Join Date
    Mar 2003
    Location
    Wysox, PA
    Posts
    2
    SO....
    How bout that war.

  13. #13
    Registered User techleet's Avatar
    Join Date
    Oct 2000
    Location
    San Jose, CA, USA
    Posts
    459
    Originally posted by grief
    SO....
    How bout that war.
    Wow, that was random. Glad I had the Email Notification on.

    DON'T PANIC

  14. #14
    Registered User techleet's Avatar
    Join Date
    Oct 2000
    Location
    San Jose, CA, USA
    Posts
    459
    Originally posted by craigmodius
    But he has some NT stuff for putting date into the environment variable available here
    YOU ARE A GOD!!!!!

    Here's all the code you need:

    for /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set weekday=%%a& set day=%%b& set month=%%c& set year=%%d)
    set jdate=%day%-%month%-%year%
    ren bob.txt bob_%jdate%.txt


    Works like a charm!! Thanks!!
    DON'T PANIC

  15. #15
    Registered User craigmodius's Avatar
    Join Date
    Sep 2001
    Location
    Hellmira, NY, USA
    Posts
    1,572
    Originally posted by techleet
    YOU ARE A GOD!!!!!

    Here's all the code you need:

    for /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set weekday=%%a& set day=%%b& set month=%%c& set year=%%d)
    set jdate=%day%-%month%-%year%
    ren bob.txt bob_%jdate%.txt


    Works like a charm!! Thanks!!
    I'm not a god I just play one at work.

    I'd also like to give a shout out to Grief for warming up the thread. We were talking at work today about this very same issue of getting the variable to work in NT, and I told him how I posted here and it seems like I have the uncanny ability to kill a thread just by posting on it, so he's got my back
    "And just when I thought today couldn't get anymore poo-like." -Outcoded

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •