General discussion

  • Creator
    Topic
  • #2299425

    Renaming a file via a batch file

    Locked

    by cvn70_it2 ·

    I run a system that creates a log file after the nightly back ups run. I want to use a batch file in DOS that runs via an AT job. I want the batch to move the log file (which I know how to do), and I need to rename the file as well. If possible, I want it named whatever the current date is. Is there some way to do this, or is there a way to create a list for the file to pick from, I.e. an “if/then” kinda thing?

All Comments

  • Author
    Replies
    • #3395464

      Renaming a file via a batch file

      by joseph moore ·

      In reply to Renaming a file via a batch file

      Yes, you can use a FOR command to parse out the DATE command, then issue a REN using the output! It took me a while to figure this out a year ago (I had to archive pictures from a webcam) but I was successful.

      Ok, say your file is called FILE.LOGand you moved it into C:\FILES

      Now, to rename this file, put the following lines in your BAT file after the file has been moved:

      C:
      CD \
      CD FILES
      FOR /F “tokens=1-4 delims=/ ” %%i in (‘date /t’) do set filerename=%%i%%j%%k%%l
      RENAME file.log %filerename%.log

      There, that’s it! That will CD into the C:\FILES directory, and take the file FILE.LOG and rename it to Thu03062003.log. The FOR line parses out the DATE /T command, assigning “Thu” to %%i, “03”
      to %%j, “06” to %%k, and “2003” to %%l. After that, the whole mess is pressed together into the new variable “filerename”. You then just have to rename your original LOG file to be %filerename%.log.

      Now, if you don’t want the day of the week in the file name (the “Thu” part inthe example), just change the FOR /F file at the end to read, “do set filerename=%%j%%k%%l”

      You can also shuffle the variables around if you want the year set first. Just have fun with this.

      hope this helps

      • #3394408

        Renaming a file via a batch file

        by joseph moore ·

        In reply to Renaming a file via a batch file

        Here is a clarification on the FOR /F command.

        IF you want to run this line from a Command Prompt, you have to use only a single “%” for each variable.
        But, if you want to run this in a BAT file, you have to use “%%”.
        This is a difference in using the FOR command.
        So, the Command line version would be:

        FOR /F “tokens=1-4 delims=/ ” %i in (‘date /t’) do set filerename=%i%j%k%l

        And the BAT file version is like I posted above.

        I don’t know why it works this way. It just does, to my chagrin!

        hope this clears things up.

        Joe

      • #3396076

        Renaming a file via a batch file

        by cvn70_it2 ·

        In reply to Renaming a file via a batch file

        The script works when I type it in manually, but it won’t work in the batch file, even after I’ve made the necessary adjustments.

    • #3395339

      Renaming a file via a batch file

      by cvn70_it2 ·

      In reply to Renaming a file via a batch file

      Thanks Joseph, but it doesn’t seem to be working. Every time I type the “For /f” line, I get a response that says ‘%%i was unexpected at this time”. Any ideas? Thanks!

    • #3395324

      Renaming a file via a batch file

      by cvn70_it2 ·

      In reply to Renaming a file via a batch file

      Joseph, thanks for the help. However, every time I type in the ‘for /f’ line, I get a response that says ‘%%i was unexpected at this time’. When I type the rename line, it just names it ‘filerename’. Am I mistyping the syntax? Thanks!

    • #3395212

      Renaming a file via a batch file

      by cvn70_it2 ·

      In reply to Renaming a file via a batch file

      Nevermind my last two comments. I figured it out. What I don’t get though, is why it works when I type it in manually, but it won’t work in the batch file. I’m gonna jack up the points if someone can figure it out. Thanks.

    • #3394252

      Renaming a file via a batch file

      by cvn70_it2 ·

      In reply to Renaming a file via a batch file

      Joe, I made the appropriate changes like you had pointed out, but it still isn’t working. Here is what my batch looks like. Am I doing something wrong?
      c:
      copy c:\folder1\test.log c:\folder2
      cd c:\folder2
      FOR /F “tokens=1-4 delims=/ ” %%i in (‘date /t’) do set filerename=%%i%%j%%k%%l
      RENAME file.log %filerename%.log

      It copies it, but it won’t rename. I feel like I’m so close! Thanks again. AJ

    • #2730196

      Reply To: Renaming a file via a batch file

      by cvn70_it2 ·

      In reply to Renaming a file via a batch file

      This question was closed by the author

Viewing 5 reply threads