Use .bat and paramenter file to back up log files

andiemac

Board Regular
Joined
Oct 8, 2009
Messages
228
I could easily figure out how to do this if I could use Excel and VBA, but my requirement is to create a batch file that will reference some kind of parameter or initialization file. I’m hoping one of my fellow MrExcel fans will have some serious .bat skills that I can learn from.
:biggrin:

Here is what I need to accomplish. I have a number of log files that occasionally get too large. I would like to take each of these and rename that file with some kind of date stamp indicator. Then I need to create or replace that log file with an empty file of the original name. This batch file would be triggered only when the services and processes have been stopped for system backups or patching, so nothing would be actively writing to the files. The idea is that the parameter file would be a list of all the files that need to be managed by this batch.

For instance:
Current file - C:\test\toofull.log
The renamed file - C:\test\toofull_20130926.log
Create an empty file called - C:\test\toofull.log

I’m thinking that the batch file will drive the action with code that says something like:

For %%A in (FileList.txt) DO
NAME strFileName AS Replace(strFileName, ".", Format(Now(), "_yyyymmdd") & ".")
NAME C:\blank.log AS strFileName

The parameter file might contain a list that looks like:
K:\Test\EventLog.log
D:\Data\errorfileload.log
S:\Logs\IntlLoad.log
M:\Automation\Import.log

I’m certainly open to suggestions! And this could probably benefit from error handling but not sure what that would look like.
:confused:
Appreciate any guidance or direction you can offer.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I’m sure there is a more refined method of doing this, but as my first effort I'm happy with it. For anybody that searches this topic in the future, this is how I accomplished my task:

Ended up with 3 files to support the process.

LogBackUpMaster.bat – the file that gets triggered during server maintenance
Code:
for /f %%a IN (LogBackUpList.txt) do call LogBackUpRunner.bat %%~da %%~pa %%~na %%~xa

LogBackUpList.txt – a text file containing the path, filename and extention of the files I want backed up this is just an example
Code:
 “C:\Test\EventLog.log”
“C:\Data\errorfileload.log”
“C:\Logs\IntlLoad.log”
“C:\Automation\Import.log”

LogBackUpRunner.bat – the actual work gets done here
Code:
echo off
 
REM establish the system date in the yyyymmdd format
for /F "tokens=1* delims= " %%A in ('date /T') do set CDATE=%%B
for /F "tokens=1,2 eol=/ delims=/ " %%A in ('date /T') do set mm=%%B
for /F "tokens=1,2 delims=/ eol=/" %%A in ('echo %CDATE%') do set dd=%%B
for /F "tokens=2,3 delims=/ " %%A in ('echo %CDATE%') do set yyyy=%%B
set date=%yyyy%%mm%%dd%
 
REM find the file to be backed up and rename it
cd %1%2
ren %3%4 %3_%date%%4
 
REM copy a blank file in to replace the file that was renamed in the back up
COPY D:\Automation\TEST_VBA\Back_Up_Test\blank.log %1%2%3%4
cd D:\Automation\TEST_VBA\Back_Up_Test
 
Upvote 0

Forum statistics

Threads
1,225,656
Messages
6,186,245
Members
453,343
Latest member
hacigultekin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top