[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 386
  • Last Modified:

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.
0
jbla9028
Asked:
jbla9028
  • 5
  • 2
  • 2
1 Solution
 
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
 
Bill PrewCommented:
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
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 
paultomasiCommented:
@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
 
paultomasiCommented:
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now