# Solved: batch file that creates automatically a new log file each month



## abnranger67 (May 7, 2013)

Hi all I am looking for a batch file/CMD that will automatically create a new log file each month, but also gets appended each time the job/operation runs. Basically I want a file created for May, then when a job runs, it is appended with the log of the operation, then when May ends, new log file for June is created and jobs appended as well and so forth. I hope I have not created any confusion on what I am trying to accomplish. Thanks in advance. I have the script from this thread http://forums.techguy.org/dos-other/685747-write-log-output-batch-file.html, but I need one that does what I explained above.


----------



## foxidrive (Oct 20, 2012)

Have the log file created with the year and month in the filename. It will automatically change when the month changes.

Such as 

logfile-yyyy-mm.log


----------



## abnranger67 (May 7, 2013)

Hmm, thanks Foxdrive, so basically I would do something like this?:
somebatchcommand >>c:\mylog-yyyy-mm.log
Exit


----------



## foxidrive (Oct 20, 2012)

If you use the snippet below (Xp Pro and later has Wmic) to create the log file name and yes, just redirect the commands into the log.

>>"%log%" echo Run 200 at %date% %time%

or redirect output from an existing batch file:

call mybatch.bat >>"%log%"


```
:: timestamp YYYY_MM
@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set log=mylog_%dt:~0,4%_%dt:~4,2%.log
echo %log%
pause
```


----------

