Batch file that renames files uniquely
Results 1 to 9 of 9

Thread: Batch file that renames files uniquely

  1. #1
    Junior Member
    Join Date
    Apr 2006
    Posts
    5

    Batch file that renames files uniquely

    Running WinXP Pro. I have a batch file running once a day that downloads a single file via cmd prompt ftp to a local directory, however, the name of the source file never changes. I need a way of having the batch file rename the currently downloaded file so it is not overwritten the next day when the batch file runs again and downloads the latest version of the original file once again. Appending Date/Time would be ideal but not necessarily necessary.

    In the very least, just need the downloaded file to be renamed with a unique name each time, even if itís just appending ascending digits to the file name (I can always just check the file properties to see the date and time the file was created to distinguish them apart).

    I am in a work environment, so installing additional third party applications like Perl are not an option for me. Just would like to know if anyone knows of a way to do this through standard batch file commands.

    Any suggestions? My apologize, but I am not a programmer, so specifying it in laymanís terms is greatly appreciated. Thank you folks.

    Jim

  2. #2
    AOs Resident Troll
    Join Date
    Nov 2003
    Posts
    3,152
    Well I am no pro on this...

    why dont your rename it before you download it

    or...copy it somewhere else...and then rename it\download it

    I pretty sure you can do all this via the batch

    Googles got lotso info

    http://www.computerhope.com/batch.htm

    MLF
    How people treat you is their karma- how you react is yours-Wayne Dyer

  3. #3
    Right turn Clyde Nokia's Avatar
    Join Date
    Aug 2003
    Location
    Button Moon
    Posts
    1,696
    The REN commands renames files.

    So "ren 1.txt 2.txt"

    Will rename 1.txt to 2.txt

    If you know the name of the files it is downloading, which you do you will just need to increment the new file name by 1 each time.

    This could help with your date issue I.E if you have the first number for the day of the month you download it and then increment by one each time, to tie in with the date...etc

    So have 1.txt to be the first of the month, then 2.txt the second of the month etc etc.....
    The at the end of the month just put them in a folder for whatever month it is?
    maybe...


    take a look at this - I didnt read through it all..but you have more of a motive to than me!
    Drugs have taught an entire generation of kids the metric system.

    http://tazforum.**********.com/

  4. #4
    Banned
    Join Date
    Aug 2001
    Location
    Yes
    Posts
    4,429
    Here's something that should help you get started to rename files with the data attached. These are all Win9x examples, so you might want to check out his NT examples. I've been messing with it for a while, and I can't get it to work, though... my date has slashes in it, which aren't allowed in file names; his method to get rid of the slashes doesn't seem to work... but it's a start

  5. #5
    Banned
    Join Date
    Aug 2001
    Location
    Yes
    Posts
    4,429
    Here's something that works:

    Have your batch file download the file with filename "file.txt" (or whatever you want to name it), then add this to your batch file (after the part that downloads your file):

    Code:
    @echo off
    for /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set weekday=%%a& set day=%%b& set month=%%c& set year=%%d)
    ren file.txt %weekday%_%day%_%month%_%year%-file.txt
    The first line will put the weekday part of the date (Mon, Tue,...) in an environment variable called weekday, put the day in a variable called day, the month in a variable called month, and year in a variable called year.
    The second line renames the file.txt file into something like "Fri_04_07_2006-file.txt"

    You can check those environment variables by typing "set" in a command prompt, btw...

    Here's a screen output from my test:
    Code:
    C:\test>dir
     Volume in drive C has no label.
     Volume Serial Number is 5C82-9050
    
     Directory of C:\test
    
    04/07/2006  10:03 AM    <DIR>          .
    04/07/2006  10:03 AM    <DIR>          ..
    04/07/2006  10:02 AM                 0 file.txt
    04/07/2006  10:01 AM               178 test.bat
                   2 File(s)            178 bytes
                   2 Dir(s)   5,043,789,824 bytes free
    
    C:\test>test
    
    C:\test>dir
     Volume in drive C has no label.
     Volume Serial Number is 5C82-9050
    
     Directory of C:\test
    
    04/07/2006  10:03 AM    <DIR>          .
    04/07/2006  10:03 AM    <DIR>          ..
    04/07/2006  10:02 AM                 0 Fri_04_07_2006-file.txt
    04/07/2006  10:01 AM               178 test.bat
                   2 File(s)            178 bytes
                   2 Dir(s)   5,043,789,824 bytes free
    
    C:\test>

  6. #6
    Right turn Clyde Nokia's Avatar
    Join Date
    Aug 2003
    Location
    Button Moon
    Posts
    1,696
    10:01 AM ! Dood! You've just started work...and I'm just about to go home
    Drugs have taught an entire generation of kids the metric system.

    http://tazforum.**********.com/

  7. #7
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    Follow negative's example (which looks nice ) or just use the following line
    at the end of your batch-script:

    Code:
    ren c:\path\filename.exe filename_%date%.exe
    and change "c:\path" and "filename.exe" accordingly.

    Cheers.
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  8. #8
    Junior Member
    Join Date
    Apr 2006
    Posts
    5
    Originally posted here by Negative
    Here's something that works:

    Have your batch file download the file with filename "file.txt" (or whatever you want to name it), then add this to your batch file (after the part that downloads your file):


    BAM!!! Nailed it Negative, thanks so much. I tried your simple one liner sec_ware, but it wasn't renaming the file for some reason, though I was careful to make sure I had the correct path and file name. Thanks so much everyone for the responses, I appreciate it. Have a great weekend.

    Jim

  9. #9
    Junior Member
    Join Date
    Apr 2006
    Posts
    5
    Originally posted here by Negative
    Here's something that should help you get started to rename files with the data attached. These are all Win9x examples, so you might want to check out his NT examples. I've been messing with it for a while, and I can't get it to work, though... my date has slashes in it, which aren't allowed in file names; his method to get rid of the slashes doesn't seem to work... but it's a start

    Btw Negative, I actually found that same site yesterday after googling and spent like 2 hours trying to follow the directions and get it to work, but it just wouldn't work for me.

    Your second reply 'Here's something that works' worked for me on the first try. Thanks again.

    Jim

Posting Permissions

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

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides