# Use .bat and paramenter file to back up log files



## andiemac (Sep 26, 2013)

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.


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.
 
Appreciate any guidance or direction you can offer.


----------



## andiemac (Oct 15, 2013)

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

```
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

```
“C:\Test\EventLog.log”
“C:\Data\errorfileload.log”
“C:\Logs\IntlLoad.log”
“C:\Automation\Import.log”
```
 
LogBackUpRunner.bat – the actual work gets done here

```
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
```


----------

