Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 476
  • Last Modified:

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.
0
26fconsulting
Asked:
26fconsulting
  • 7
  • 4
  • 3
  • +1
1 Solution
 
mvdeveloperCommented:
Something along these lines - modify to suit.

Regards

-----

Dim FSO

Const START_DIR = "c:\temp"
Set FSO = CreateObject("Scripting.FileSystemObject")

set outFile = FSO.createTextFile( "c:\result.csv")

doFolder START_DIR

outFile.close



sub doFolder(path)
  ' WScript.echo "Doing folder " + path

  Dim folder, files, file, subfolders,subfolder

  set folder = FSO.getFolder(path)
  set files = folder.Files
  for each file in files
     doFile path, file.path
  next

  set subFolders = folder.subFolders
  For Each subfolder in subFolders
    doFolder subfolder.path
  Next

end sub

sub doFile( parent, path )
  ' WScript.echo "Found file " + path
  if right(lcase(path),4) = ".log" then
     checklog parent, path
  end if
end sub

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

  Do While fl.AtEndOfStream <> True
    line = fl.ReadLine
    if InStr(line, "Elapsed search time" ) > 0 then
        output parent, path, line
    end if
  Loop
  fl.close
   
end sub

sub output(parent, path, line)
  outFile.writeLine(parent + "," + path + "," + line)
end sub
0
 
26fconsultingAuthor Commented:
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?
0
 
Steve KnightIT ConsultancyCommented:
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.)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
26fconsultingAuthor Commented:
@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
0
 
mvdeveloperCommented:
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.
0
 
Steve KnightIT ConsultancyCommented:
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
0
 
26fconsultingAuthor Commented:
@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
0
 
26fconsultingAuthor Commented:
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
0
 
Steve KnightIT ConsultancyCommented:
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

0
 
26fconsultingAuthor Commented:
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
0
 
mvdeveloperCommented:
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
0
 
26fconsultingAuthor Commented:
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.
0
 
mvdeveloperCommented:
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.
0
 
Bill PrewCommented:
@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
0
 
26fconsultingAuthor Commented:
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 :)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 7
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now