General discussion

  • Creator
    Topic
  • #2322879

    batch file to rename files daily

    Locked

    by donalconlon ·

    Hi,
    Can anyone give me an example of a batch file that will change the name of a file called db_base to the current date on a daily basis.i.e today this file will need to be renamed to 07-05-02 and tomorrow when it comes over again from another server it will need to be renamed to 08-05-02. Many thanks.

All Comments

  • Author
    Replies
    • #3405213

      batch file to rename files daily

      by fenaikh ·

      In reply to batch file to rename files daily

      Hi

      Pls visit this URl:
      A common question is how to give files names based on date or time.

      http://www.calweb.com/~webspace/batch/samples/namedate.txt

      Good luck

      Mohamed

      ***Very Important NOTE *********
      After you copy the URL aspace or a %20 maybe gets inserted in the URL when you paste it into your browser. Delete any spaces or such characters from the URL for the page to be loaded.

    • #3405184

      batch file to rename files daily

      by beermonster ·

      In reply to batch file to rename files daily

      Hi,
      try this, note that to run from a command line you need one % sign in front of the A(as below), if you put this in a batch file you’ll need to change it to two – %%A

      rem – get todays date
      for /F %A in (‘date /t’) do set today=%A

      rem – the output (uk) is dd/mm/yy but you want
      rem dd-mm-yy so change the \ to a –
      set today=%today:/=-%

      rem – now do the rename
      ren myfile.fil %today%.fil

      Hope this helps…..

    • #3405180

      batch file to rename files daily

      by joseph moore ·

      In reply to batch file to rename files daily

      It actually only takes 1 line in a BAT file:

      for /f “tokens=1-4 delims=/ ” %%i in (‘date /t’) do echo %%j-%%k-%%l

      That like will take the DATE command (which displays output like TUE 05/07/2002) and breaks up each piece between the spaces and the front slashes, and assigns each one to a separate variable.
      So in the example, %%j = the month (05)
      %%k = the day (07)
      and %%l = the year (2002)
      and %%i = the day in alpha (TUE)
      You could then arrange the variables in any order when you rename your file. For example, after you run the FOR /F command, do this:

      copy db_base %j%-%k%-%l%

      That would make a copy of your file and call it 05-07-2002 when run today; tomorrow, its 05-08-2002

      And yes, in the BAT file, use the double % signs for the FOR /F line, but %letter% in the copy line.

      hope this helps

      • #3405152

        batch file to rename files daily

        by donalconlon ·

        In reply to batch file to rename files daily

        Hi Joseph can you check the syntax again as the output I get when I run the batch file with both lines is a file call –. Thanks.

    • #3405014

      batch file to rename files daily

      by joseph moore ·

      In reply to batch file to rename files daily

      Dang it! Forgot, the variables set up on the FOR /F line are only good when you are still ON that line. I always forget that!

      Ok, the BAT file is down to 1 line. Try this:

      for /f “tokens=1-4 delims=/ ” %%i in (‘date /t’) do copy db_base %%j-%%k-%%l

      That will copy the file called db_base and call the copy 05-07-2002

      Now, if you need to just rename the existing db_base file and not make a copy of it, just replace the COPY command in the FOR /F line with RENAME. You get the point.

      Sorry for the confusion, but this one line is all you need.

    • #3416520

      batch file to rename files daily

      by donalconlon ·

      In reply to batch file to rename files daily

      This question was closed by the author

Viewing 4 reply threads