Solved

# Rename all files in a folder with extension *.log with servername and date

Posted on 2012-09-19
374 Views
Hi I have a script that I could use some help on. I have a couple of operations within the script but one piece is throwing me for a loop. I need a windows batch file, that will perform the following function

Take all files in a folder that have an extension *.LOG and rename them to

%Servername%_%OriginalFileName%_%DateTimeStamp%.log

how can I accomplish this? I have a script that will do this for a single file but not for all files in a directory.
0
Question by:jbla9028

LVL 51

Expert Comment

What's your single file script look like?

You can use the FOR command to loop over all files in a folder, for example:

for %%A in ("c:\temp\*.log") do (
echo %%A
)

Then you just adapt your current rename logic to work on each file one by one inside the FOR loop, where the file name will be assigned to the %%A loop variable.  Once I see what you have I can help adapt it.

~bp
0

LVL 1

Author Comment

This is what I got so far. I think you may have helped me previously!

I'm a little confused how I could modify this. I'm guessing if the basefile could be a variable that would work?

#Rename the log log.log file to CFMLogs_%Servername%_%DateTime%.log
set BaseFile=C:\temp\log.log
set DestName=CFMLogs_
set DestExt=log
ECHO Date=[%DATE%]
ECHO Time=[%TIME%]
set Stamp=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
ECHO Stamp=[%STAMP%]
ECHO copy "%BaseFile%" "%DestDir%\%DestName%_%COMPUTERNAME%_%Stamp%.%DestExt%"
copy "%BaseFile%" "%DestDir%\%DestName%_%COMPUTERNAME%_%Stamp%.%DestExt%"

0

LVL 51

Accepted Solution

This should get the job done, if I understand it right.

Inside the FOR loop, %%~A will be the full path of the current file being processed, and then we can use %%~nA to refer to it's base file name, and %%~xA to refer to its extension (including the period).

@echo off

REM Set directory where files to be renamed exist, and the destination directory
set BaseDir=c:\temp

REM Build a date / time stamp in the format YYYYMMDD_hhmmss
set Stamp=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
ECHO Date=[%DATE%]
ECHO Time=[%TIME%]
ECHO Stamp=[%STAMP%]

REM Process all files in the base folder
for %%A in ("%BaseDir%\*.log") do (
REM Rename to Servername_OriginalFileName_DateTimeStamp.OriginalFileExtension
ECHO copy "%%~A" "%DestDir%\%COMPUTERNAME%_%%~nA_%Stamp%%%~xA"
copy "%%~A" "%DestDir%\%COMPUTERNAME%_%%~nA_%Stamp%%%~xA"
)

~bp
0

LVL 11

Expert Comment

@echo off

set source=c:\temp

set ds=%date:~-4%%date:~-10,2%%date:~-7,2%
set ts=%time:~0,2%%time:~3,2%%time:~6,2%

for %%a in ("%source%\*.log") do (
copy "%%~a" "%destination%\%computername%_%%~na_%ds%_%ts%.log"
)

0

LVL 51

Expert Comment

Paul,

I'm confused, why did you post essentially the same code I had posted, without the comments and diagnostic displays?  That's not like you.

~bp
0

LVL 11

Expert Comment

bill

Apologies. As you say, it does look compellingly similar.

Surely this proves two experts can't be wrong!
0

LVL 51

Expert Comment

Paul,

No worries, it happens, just didn't know if I was missing one of your pearls of wisdom.

~bp
0

LVL 1

Author Closing Comment

Thank you! this worked perfectly. I apologize for the delays in responding.
0

LVL 51

Expert Comment

Welcome, thanks for the feedback.

~bp
0

## Featured Post

### Suggested Solutions

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…