Batch for log gathering + timestamping
December 5th, 2006When gathering logs, or performing backups, it's useful to append date suffix to the result file.
But that's all not so trivial calculating such suffix in pure bat-files.
We'll use system variables %date% and %time%.
Try executing "echo %date%" and "echo %time%" from command line and watch the output.
We'll append date suffix, and will show final result for time+date.
Suppose we need to rename the file test.txt to test_currdate.txt.
On my laptop "echo %date%" gives "05.12.2006".
Be careful, output depends on regional settings (I've got Russian), so you'll need to modify this script.
"test_05.12.2006.txt" doesn't look that good, I'd prefer "test_05122006.txt", so we'll need to calculate 05122006 from 05.12.2006.
This can be done via :~ operator, allowing to cut substrings. Syntax is str:~start_position,length.
So the following line will calculate the variable currdate, holding "05122006".
set curdate=%date:~6,4%%date:~3,2%%date:~0,2%
rename command allows to change name of file, therefore you just run
set curdate=%date:~6,4%%date:~3,2%%date:~0,2%
rename test.txt test_%currdate%.txt
Script for grabbing specified files from servers below.
This bat file accepts servernames as arguments and iterates through them to rename&gather all PlanningErrorLog.csv logs in one place (namely, C:\Logs).
@echo off rem Gathering the Logs from command line arguments=servernames rem directory to write set destination="C:Logs\" set user=Admin set pwd=~~~~~~~~ set log_directory="WindowsTemp\" set log_name=PlanningErrorLog echo Monitoring %date% %time%>>c:logsmonitor_log.txt :LOOP IF "%1" == "" GOTO END net use \\%1\c$ %pwd% /USER:%1\%user%set curtime=%date:~6,4%%date:~3,2%%date:~0,2%_%time:~0,2%%time:~3,2%%time:~6,2% if EXIST \\%1\c$%log_directory%%log_name%.csv rename \\%1\c$\%log_directory%%log_name%.csv %1_%log_name%%curtime%.csv & xcopy \\%1\c$\%log_directory%%1_%log_name%%curtime%.csv %destination% /DY net use /delete \\%1\c$ /Y SHIFT GOTO LOOP :END
No comments yet.