Need help creating bat for network AV program, add hostname

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mgrahek
Posts: 13
Joined: 29 Aug 2008 08:24

Need help creating bat for network AV program, add hostname

#1 Post by mgrahek » 29 Aug 2008 08:43

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?

greenfinch
Posts: 36
Joined: 17 Jul 2008 07:37

#2 Post by greenfinch » 02 Sep 2008 11:36

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 ?)

mgrahek
Posts: 13
Joined: 29 Aug 2008 08:24

#3 Post by mgrahek » 04 Sep 2008 06:30

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?

greenfinch
Posts: 36
Joined: 17 Jul 2008 07:37

#4 Post by greenfinch » 08 Sep 2008 18:54

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% 

mgrahek
Posts: 13
Joined: 29 Aug 2008 08:24

#5 Post by mgrahek » 09 Sep 2008 16:34

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?

greenfinch
Posts: 36
Joined: 17 Jul 2008 07:37

#6 Post by greenfinch » 10 Sep 2008 08:17

Not sure - could you post the whole of your current script, with that line in it?

mgrahek
Posts: 13
Joined: 29 Aug 2008 08:24

#7 Post by mgrahek » 10 Sep 2008 09:48

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

greenfinch
Posts: 36
Joined: 17 Jul 2008 07:37

#8 Post by greenfinch » 10 Sep 2008 10:06

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

mgrahek
Posts: 13
Joined: 29 Aug 2008 08:24

#9 Post by mgrahek » 10 Sep 2008 10:43

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.

greenfinch
Posts: 36
Joined: 17 Jul 2008 07:37

#10 Post by greenfinch » 10 Sep 2008 20:05

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.

mgrahek
Posts: 13
Joined: 29 Aug 2008 08:24

#11 Post by mgrahek » 12 Sep 2008 17:09

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

greenfinch
Posts: 36
Joined: 17 Jul 2008 07:37

#12 Post by greenfinch » 12 Sep 2008 20:03

Code: Select all

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



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

mgrahek
Posts: 13
Joined: 29 Aug 2008 08:24

#13 Post by mgrahek » 12 Sep 2008 20:05

Thank you. I didn't know the echo would write to a file!

Post Reply