Rename and Copy Logs Recursively

August 19, 2009 at 10:08:55
Specs: Windows XP

I run the following script on my servers xcopy g:\Logs\iis\*.log \\servername\logs\iis\*.log_%computername% /D /Y /S. It works ok except the logs get copied over in this format ex090728.log_servername and I would really like them in this format ex090728_IHS-S-SPWB-204.log

Thanks in advance.

See More: Rename and Copy Logs Recursively

Report •

August 20, 2009 at 05:40:39
Okay, so this is actually kind of tricky if you want to use the "Xcopy" command, becuase when you use Xcopy to change a filename it isn't so smart and it will either just error out or most likely you will get a prompt asking if the destination is a file or directory, and you can't suppress it from asking unless you use the /i switch which will make it a directory and not a file. So here is how I would suggest doing it. We make each filename ending with .log in the source directory into a variable called %%A then we use the For /f command to use the xcopy command for each name in %%A but it kept prompting me to either hit F for a file or D for a directory, now I had two ways to have the batch answer automatically, the first was to create a text file that just contained the letter F and then use the port command < to enter the text (xcopy source destination < text.txt) which to me didn't seem that pretty, so I chose to port the xcopy command through the echo command with the value of "f"
echo f| xcopy source destination. I hope this is making sense so that you understand what is going on, then we use some variable modifiers to remove the file extenstion "~n", then add the computer name and then again use the modifier to add the file extension back on "~x"

I hope this is what you are looking for.

@echo off
for /f "tokens=*" %%A in ('dir /b "g:\logs\iis\*.log"') do (
echo f| xcopy /d /y /s %%A "\\servername\logs\iis\%%~NA_%computername%%%~XA")

Report •

August 20, 2009 at 10:30:53
Hello! Thanks for the reply. I tried running your syntax both in a .bat file and directly from the command line. This looks very promising, but from the bat file I get:

The following usage of the path operator in batch-parameter sustitution is invalid: %~NA_%computername%%%~XA")

For valid formats type CALL /? or FOR /?
copyrenamelogs.bat was unexpected at this time

And from the command line I get:

%%A was unexpected at this time.

I really appreciate you looking into this.

Report •

August 20, 2009 at 12:13:08
See if this works, the "%" that was in front of \\servername shouldn't have been there which is probably what gave you the Formating error, also I added the path to %%A becuase obviously if the batch isn't in the same folder as the logs it wouldn't know where to look.

@echo off
for /f "tokens=*" %%A in ('dir /b "g:\logs\iis\*.log"') do (
echo f| xcopy /d /y /s "g:\logs\iis\%%A" "\\servername\logs\iis\%%~NA_%computername%%%~XA")

Report •

Related Solutions

August 20, 2009 at 14:11:22
Closer! I've altered the paths just a little to test since g:\logs\iis has gigs of logs. I placed two site ID folders from g:\logs\iis under "g:\test1 with say 10 .log files in each. I'm running it from a batch file called g:\utilities\test.bat. Here are the results. I must be missing something. What's weird is I get different results if I run from the command line vs. a batch from the same g:\utilities directory. Notice the File Not Found percent difference.

Running from batch produces:

G:\Utilities>echo off for /f "tokens=*" %A in ('dir /b "G:\test1\*.log"') do (echo f| xcopy /d /y /s "G:\test1\%A" "g:\test2\%~NA_IHS-T-SPWB-302%~XA")
File not found - %A
0 File(s) copied

Pasting into command line produces:

G:\Utilities>echo off for /f "tokens=*" %%A in ('dir /b "G:\test1\*.log"') do (echo f| xcopy /d /y /s "G:\test1\%%A" "G:\test2\%%~NA_%computername%%%~XA")
File not found - %%A
0 File(s) copied

So for clarity, I need to store the .bat file in g:\utilities on each server I'm transferring the logs from, look in g:\logs\iis on the same server, gather up all the .log files in each site id folder, copy the .log files from the source server where the .bat file is on g:\utilities to \\destinationserver\logs\iis and wind up with the correct ex090720_sourceservername.log format on the destination server we're using for analytics. Shewwwww..... Ain't technology great!

Once we get this working, all I will need is a send to address so I know where to send your new Porche.

Thanks again, and please stick with me.

Report •

August 20, 2009 at 14:52:38
The only way that I could get it figured out to return the results that you have was by putting it in a .bat and then putting all commands on one line. When you copy and paste all this text out of the forum it should paste in to your .bat with three lines, the first line is simply "@echo off" the second line starts with "For /f..." and the third line should start with "echo f|..."

The %%A variable won't work if you paste it straight into the command line, if you want to put this command into the command line directly, you are correct, it all needs to be on one line, but you have to change the %%A variable to only have one % sign also remove the @echo off

So the following oneliner would work directly in the commandline

for /f "tokens=*" %A in ('dir /b "g:\test1\*.log"') do ( echo f| xcopy /d /y /s "g:\test1\%A" "g:\test2\%~NA_%computername%%~XA")

The above wouldn't work in a .bat becuase when the .bat run's it strips one of the % from the variable. The following should do what you want, make sure that when you paste it into your .bat that it is formatted into the three lines just like how it is posted, The lines start with @echo..., for /f... and xcopy /....

@echo off
for /f "tokens=*" %%A in ('dir /b "g:\test1\*.log"') do echo f| (
xcopy /d /y /s "g:\test1\%%A" "g:\test2\%%~NA_%computername%%%~XA")

Report •

August 21, 2009 at 08:40:29
Hi gtaion,

I'm getting a File Not Found when I run copyrenamelogs.bat from the g:\utilities directory. I updated the .bat file making sure the 3 lines were formatted as you mention above

If I run for /f "tokens=*" %A in ('dir /b "g:\test1\*.log"') do ( echo f| xcopy /d /y /s "g:\test1\%A" "g:\test2\%~NA_%computername%%~XA") from the command line I receive the same File Not Found message. g:\test1 and g:\test2 are correct paths.


Report •

August 21, 2009 at 11:51:48
Well... hmm... That to me is saying one of two things,
1: The file extension on your log files is not .log
2: g:\test1 doesn't contain any files with .log file extension.

For the first one, it is simple if the actually file extension is not .log i.e. the logfile is named (date.log.txt) all you do is add another * to the end so in the batch it would look like *.log* which would mean that the batch is looking for any file that contains .log in the filename but has an unknown file extension.
But then we will also have to look at another way to format the name.

For the second, add some file's with the .log file extension to the g:\test1 folder.

If neither of them are the case I'm at a loss

Report •

August 21, 2009 at 12:54:39
Maybe I wasn't clear about the sub directories under g:\test1. There are multiple sub directories under g:\test1 which do contain files all ending in .log. I would need the script to go through each of the subs recursively under g:\test1 as it did with my original xcopy syntax. Is that possible using your script?

Report •

August 21, 2009 at 13:12:13
@echo off
for /f "tokens=*" %%A in ('dir /b /s "g:\test1\*.log"') do echo f| (
xcopy /d /y "%%A" "g:\test2\%%~PA%%~NA_%computername%%%~XA" > nul)

So your final production code should look like:

@echo off
for /f "tokens=*" %%A in ('dir /b /s "g:\logs\iis\*.log"') do echo f| (
xcopy /d /y "%%A" "\\SERVERNAME\%%~PA%%~NA_%computername%%%~XA" > nul)

Notice that on the server the rest of the path was removed, that is becuase it's going to mimic the directory structure from the origanal computer, the only thing it is doing is replacing the "G" with your servername and adding the computername where you wanted it to.

Report •

August 25, 2009 at 10:00:38
That did the trick! Thanks very very much for all of your assistance on this request. I really appreciate it.

Report •

Ask Question