Link to home
Start Free TrialLog in
Avatar of 26fconsulting
26fconsulting

asked on

VBscript or Batch Script Help needed!

Hi Everyone,

I'm under a tight deadline and my vbscript and batch scripting skills are pretty rusty. I need to set the script to point at a high level directory (that contains multiple sub directories, and those subdirectories contain subdirectories), and it needs to open up each *.log file and search for the following text -- "Elapsed search time:" . The script will create a CSV file that will have 3 columns -- Parent Folder (the 1st level folder name of the high level directory) File Path, and Elapsed Search Time. For each hit that it finds, dump that into the CSV file.

Could someone please help me get started? Someone might have a script laying around that might do most of the heavy lifting.

Thanks! Need this ASAP.
ASKER CERTIFIED SOLUTION
Avatar of mvdeveloper
mvdeveloper

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 26fconsulting
26fconsulting

ASKER

Thank you so much! It looks like it is looping through all the files, and the result.csv is getting created... but it is blank?

A sample line that I am looking for from the log files is below

[16:08:54 Feb-03] Elapsed search time: 29 Minutes 17 Seconds


maybe this is why it isn't working?
Avatar of Steve Knight
Ok to iterate down the files, then run a for command over the output of a find command it runs against every log file it finds looking for that (exact case sensitive) string and when it finds one it adds that (or multiple if more than one entry in the log) to the CSV file.  At the moment that shows "path", "filename", "bit after the : on those lines":

@echo off
set log="c:\logfile.csv"
cd /d "c:\startdir"

for /r %%F in (*.log) do (
  for /f "tokens=4 delims=:" %%T in ('find "Elapsed search time:" ^< %%F") do (
    echo Entry %%T found in %%~nxF
    echo "%%~dpF","%%~nxF","%%T">>%log%
  )
)

Now I haven't got much time at the mo. so untested.  Please give it a go on some test files and see what happens

(just seen other posts and edited this.  Changed it from tokens=2 to tokens=4 to split the line now we know there are time with two colons before it.)
@dragon-it

here is my code:
@echo off
set log="c:\logfile.csv"
cd /d "c:\Projects\ScriptTest"

for /r %%F in (*.log) do (
  for /f "tokens=4 delims=:" %%T in ('find "Elapsed search time:" ^< %%F") do (
    echo Entry %%T found in %%~nxF
    echo "%%~dpF","%%~nxF","%%T">>%log%
  )
)


I saved the file as a filesearch.bat in C:\Projects. When I open up a CMD prompt and go to C:\Projects and type filesearch.bat, I get an error message saying ">> was unexpected at this time." and I notice the working directory was changed to C:\Projects\ScriptTest
My results file (hacked a line into one of my log files) reads:

c:\temp,C:\Temp\wsmain_2508.log,[16:08:54 Feb-03] Elapsed search time: 29 Minutes 17 Seconds

How are you running my script? and have you let it run to completion? the log file will appear empty until it has finished.
Sorry " missed off by the looks of it on this line so it had a mismatch of quotes:

  for /f "tokens=4 delims=:" %%T in ('find "Elapsed search time:" ^< "%%F") do (

Steve
@mvdeveloper

I have the file saved as eescript.vbs in C:\TestAsdf folder. I'm running CMD prompt as an admin (on a work computer with locked down regular user accounts). I have Const START_DIR = "c:\testasdf\scripttest". In the CMD prompt I am entering eescript.vbs and also trying cscript eescript.vbs

Thanks for the help :D

[edit] -- i'm running it against a small set of files, so it is running fairly quickly... i'm logged in as an admin now just to see what happens... I see the "log file xxx" entries in CMD prompt when I run the script, for some reason the CSV doesn't get populated though... it gets created but no text
I just changed this Sub  to echo the line -- and when it found something... there were some funky characters... do you think it could be a problem with the encoding of the log files of some sort?

sub checklog(parent, path)
  Wscript.echo "log file " + path
  Set fl = FSO.opentextfile(path, 1)

  Do While fl.AtEndOfStream <> True
    line = fl.ReadLine

      Wscript.echo "parent: " & parent & " --- Line: " & line

    if InStr(line, "Elapsed search time:" ) > 0 then
       
      output parent, path, line
    end if
  Loop
  fl.close
   
end sub
I think this is it.... got to go for a bit sorry.

@echo off
set log="d:\logfile.csv"
cd /d "d:\log"

for /r %%F in (*.log) do (
  for /f "tokens=6* delims=:" %%T in ('find "Elapsed search time:" ^< "%%~F"') do (
    echo Entry %%T found in %%~nxF
    echo "%%~dpF","%%~nxF","%%T">>%log%
  )
)

Open in new window

THAT WAS IT! It looks like all the log files are saved in a unicode text format... VBScript couldn't open up and read the text files... I resaved one of the text files at UTF8 and it was able to extract the text...


Now... any ideas how I can convert all these guys to UTF8 or use the script to read unicode?

Thanks :D
Well in this case, all you're looking for are ansi characters, so you can just strip off the zero byte.
Hack, but it should work for you ..

Do While fl.AtEndOfStream <> True
    line = fl.ReadLine
   
    line = replace(line, chr(0), "")
   
    if InStr(line, "Elapsed search time" ) > 0 then
        output parent, path, line
    end if
  Loop
That's worked!! YOU ROCK!!! One quick last thing... when I open in excel there is a blank line between every row... any ideas on how to get rid of this? When I open the CSV in notepad there is no blank line.
better option may be to change the opentextfile:

opentextfile(path, 1,false, -1)

The last argument should open it as unicode.. but I'm traveling now so can't check.
@mvdeveloper,

You are correct, it is -1 for UNICODE, here are the values for that parm:

Const TRISTATE_USEDEFAULT             = -2 'Opens the file using the system default.
Const TRISTATE_TRUE                   = -1 'Opens the file as Unicode.
Const TRISTATE_FALSE                  = 0  'Opens the file as ASCII.

(just to help a little as you are mobile, not mooching any points ...)

~bp
Thanks a ton! Sorry just now getting back to accept this. I was able to modify this a good bit to do what I needed it to do.

Thanks :)