Page 1 of 1

Need help creating bat for network AV program, add hostname

Posted: 29 Aug 2008 08:43
by mgrahek
I am having some issues creating this batch file. This is really the first one I have written and need some help.

Here is what it looks like so far
*************************************
REM Change to working directory
cd\program files\sophos\sophos anti-virus\

REM Print Computer Information
hostname>>c:\SophosAV.log
whoami>>c:\SophosAV.log
date /T>>c:\SophosAV.log
time /T>>c:\SophosAV.log

REM Sophos Scan WILL NOT DISINFECT!!
sav32cli z: -f -sc -nc -dn -mbr -bs=c: -all -rec -ndi -archive -p=z:\SophosAV-s.log -exclude c:\boot\
type z:\SophosAV-S.log>>c:\SophosAV.log

REM Print time of completion
time /T>>c:\SophosAV.log

hostname>>c:\SophosAV.log
whoami>>c:\SophosAV.log
date /T>>c:\SophosAV.log
time /T>>c:\SophosAV.log

REM Sophos Scan WILL NOT DISINFECT!!
sav32cli c: -f -sc -nc -dn -mbr -bs=c: -all -rec -ndi -archive -p=c:\SophosAV-c.log -exclude c:\boot\
type c:\SophosAV-c.log>>c:\SophosAV.log

REM Print time of completion
time /T>>c:\SophosAV.log

REM Map network drive
net use v: \\vserver\l

copy c:\SophosAV.log v:\log\

net use v: /delete
********************************************

So far it is working pretty well but there are a few things that I would like to improve.

First, I would like to incorporate the hostname (computer name that the scan runs on) into the file name, as well as the date. I tried this and it didn't work

set log=c:\%hostname%-Sophos-AV-Scan.log

When i use the above, i get a file named: -Sophos-AV-Scan.log

How do I do this to properly add the hostname, and it needs to be automatic for whatever computer it is run on.

Finally is there a way to add the date the Scan is run?

Posted: 02 Sep 2008 11:36
by greenfinch
Try %computername% - see http://en.wikipedia.org/wiki/Environmen ... ft_Windows

As for the date - where do you want it? How do you want it to look (e.g. did you want something like computername-Sophos-AV-Scan-YYYY-MM-DD.log ?)

Posted: 04 Sep 2008 06:30
by mgrahek
greenfinch wrote:As for the date - where do you want it? How do you want it to look (e.g. did you want something like computername-Sophos-AV-Scan-YYYY-MM-DD.log ?)


Yes, computername-Sophos-AV-Scan-YYYY-MM-DD.log. Any suggestions?

Posted: 08 Sep 2008 18:54
by greenfinch
It depends what your date format is (try running date /t) but start the whole thing with something like:

Code: Select all

FOR /F "usebackq tokens=1,2,3,* delims=/-" %%a IN ('%date%') DO (set datestring=%%c-%%b-%%a)

set logfile=c:\%computername%-Sophos-AV-Scan-%datestring%.log


Then direct things to %logfile% , e.g.

Code: Select all

whoami>>%logfile% 

Posted: 09 Sep 2008 16:34
by mgrahek
Thanks for the tips!!

I checked my date format and it looks like this:

Tue 09/14/2008

So that is why it wasn't a valid use, I think. I figure that it is the /'s..

When I enter your
FOR /F "usebackq tokens=1,2,3,* delims=/-" %%a IN ('%date%') DO (set datestring=%%c-%%b-%%a)


I get this in response:
%%a was unexpected at this time


What does this mean and how do I get around it?

Posted: 10 Sep 2008 08:17
by greenfinch
Not sure - could you post the whole of your current script, with that line in it?

Posted: 10 Sep 2008 09:48
by mgrahek
Well, I don't have the date included yet because it doesn't work.

When I type exactly what you had in quotes:
FOR /F "usebackq tokens=1,2,3,* delims=/-" %%a IN ('%date%') DO (set datestring=%%c-%%b-%%a)


I get the error I quoted above:
%%a was unexpected at this time


What does my script have to do with changing my date. I'm looking for the code just to re-format the date appearance. If I type in what you had quoted (verbatim) should that work? I'm using Vista BTW.. I could try on XP if needed.

I really don't understand the first line that you wrote. the "usebackq tokens=1,2,3,* delims=/-"

I just need to to know how to re-format the date. I do not have anything in yet as to how to do it w/o getting the error

Posted: 10 Sep 2008 10:06
by greenfinch
In a batch file you need %%x for tokens. If you're just typing into the comand line you need %x. So try this in the command line:

Code: Select all

FOR /F "usebackq tokens=1,2,3,* delims=/-" %a IN ('%date%') DO (set datestring=%c-%b-%a) 


When you put it in the script, use the original with %%a %%b %%c

The line takes the date from the date /t command, uses / as a separator, and makes a new variable called datestring. Nothing changes your actual date.

See some of my earlier posts which mention the FOR command and the way it uses delims.

http://www.dostips.com/forum/search.php ... greenfinch

Posted: 10 Sep 2008 10:43
by mgrahek
Thanks!! I didn't understand that it would work differently when it was in a script vs when it was run from the CLI. I put your original suggestion into the script and it spit out a nicely formatted date!


This is what my code looks like now:

Code: Select all

FOR /F "usebackq tokens=1,2,3,4,* delims=/-" %%a IN ('%date%') DO (set datestring=%%d-%%c-%

%b-%%a)
pause
cd\
mkdir log
pause
hostname>>c:\log\%computername%-SophosAV-%datestring%.log
whoami>>c:\log\%computername%-SophosAV-%datestring%.log
date /T>>c:\log\%computername%-SophosAV-%datestring%.log
time /T>>c:\log\%computername%-SophosAV-%datestring%.log
pause

The output is a file named: GATEWAY-SophosAV-2008-10-Wed
and only had date /T; and time /T in the file. Here is the screen shot from when it ran.

Why was hostname and whoami ommited?
Image

Also, the month (09) was ommitted from the filename and it shows up before the >> of the output call.

Posted: 10 Sep 2008 20:05
by greenfinch
Ah, your date format is a bit different to mine - this will reformat it to YYYY-MM-DD:

Code: Select all

FOR /F "usebackq tokens=1-4,* delims=/ " %%a IN ('%date%') DO (set datestring=%%d-%%b-%%c)


The tokens in your date format here are Wed 09 10 and 2008 - I ignore the first and rearrange the rest into ISO date format.

Don't understand why that 09.log appears - make sure that this line break is not in the actual batch:

Code: Select all

FOR /F "usebackq tokens=1,2,3,4,* delims=/-" %%a IN ('%date%') DO (set datestring=%%d-%%c-%

%b-%%a)


It should be all one line.

Posted: 12 Sep 2008 17:09
by mgrahek
Thank you sir! That did the trick perfectly!!



Finally, this may seem really trivial, but how do I print a blank line into a file, followed by a line of ************************'s and another blank line. I want this to be a divider on my log files.

Thanks again for your help, I really appreciate it!!

Posted: 12 Sep 2008 20:03
by greenfinch

Code: Select all

echo.>>filename
echo *********>>filename
echo.>>filename



Remember, > will create a file (overwriting anything you had before) and >> appends to the file.

Posted: 12 Sep 2008 20:05
by mgrahek
Thank you. I didn't know the echo would write to a file!