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

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.
LVL 1
jbla9028Asked:
Who is Participating?
 
Bill PrewConnect With a Mentor Commented:
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
set DestDir=\\int\logs\adhoc

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"
)

Open in new window

~bp
0
 
Bill PrewCommented:
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
 
jbla9028Author Commented:
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 DestDir=\\int\logs\adhoc
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%"

Open in new window

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Paul TomasiCommented:
@echo off

set source=c:\temp
set destination=\\int\logs\adhoc

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"
)

Open in new window

0
 
Bill PrewCommented:
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
 
Paul TomasiCommented:
bill

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

Surely this proves two experts can't be wrong!
0
 
Bill PrewCommented:
Paul,

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

~bp
0
 
jbla9028Author Commented:
Thank you! this worked perfectly. I apologize for the delays in responding.
0
 
Bill PrewCommented:
Welcome, thanks for the feedback.

~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.