General discussion

Locked

Renaming a file via a batch file

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?

This conversation is currently closed to new comments.

8 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Renaming a file via a batch file

by Joseph Moore In reply to Renaming a file via a bat ...

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

Collapse -

Renaming a file via a batch file

by Joseph Moore In reply to Renaming a file via a bat ...

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

Collapse -

Renaming a file via a batch file

by CVN70_IT2 In reply to Renaming a file via a bat ...

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.

Collapse -

Renaming a file via a batch file

by CVN70_IT2 In reply to Renaming a file via a bat ...

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!

Collapse -

Renaming a file via a batch file

by CVN70_IT2 In reply to Renaming a file via a bat ...

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!

Collapse -

Renaming a file via a batch file

by CVN70_IT2 In reply to Renaming a file via a bat ...

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.

Collapse -

Renaming a file via a batch file

by CVN70_IT2 In reply to Renaming a file via a bat ...

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

Collapse -

by CVN70_IT2 In reply to Renaming a file via a bat ...

This question was closed by the author

Back to Windows Forum
8 total posts (Page 1 of 1)  

Related Discussions

Related Forums