Back to Blog

Save Unique Log Files With a Windows Batch File

This may help you if you have to run a batch file and save off file with a unique name every time. I find the easiest thing is to save based on the exact time. For this file, I wanted to gather all of the processes in task manager and save them into a spreadsheet. I did this at the beginning of a load test, and the ending of a load test. I then compared the two files to determine memory utilization of each process. In XP there is a command line executable build in called “tasklist.exe” and it allows you to save your data in csv format. In this case I only chose to look at the dllhost.exe processes. The main thing I wanted to save in here was the code for setting up the timestr variable, which is the current date and time.

If you run this against another machine (not localhost) you will need to make sure you have appropriate permissions. You might want to do a “NET USE” with your credentials before running this against the remote machine to ensure you have an open connection.

-----Begin Code------
echo off
REM SET UP UNIQUE LOG FILE NAMING CONVENTION
for /f "tokens=1,2" %%u in ('date /t') do set d=%%v
for /f "tokens=1" %%u in ('time /t') do set t=%%u
if "%t:~1,1%"==":" set t=0%t%
set timestr=%d:~6,4%%d:~3,2%%d:~0,2%%t:~0,2%%t:~3,2%

REM RUN TASKLIST.EXE WITH OPTIONS TO SAVE AS A CSV FILE
tasklist /FI "IMAGENAME eq dllhost.exe" /S SERVER1 /U DOMAIN\USERNAME /FO CSV > tasklistlog%timestr%.csv
-----End Code----

01/08/2010 UPDATE:

My brother from another mother, Richard Bishop has expanded upon this. Check it out:

http://www.bish.co.uk/index.php?option=com_content&view=article&id=82:tasklist-log-files&catid=34:recent&Itemid=1

Enjoy! Got a cool batch file program? Send it to me at scott-at-loadtester.com

Scott

Back to Blog