Solved

VBscript or Batch Script Help needed!

Posted on 2012-04-03
15
448 Views
Last Modified: 2012-06-27
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
Comment
Question by:26fconsulting
  • 7
  • 4
  • 3
  • +1
15 Comments
 
LVL 5

Accepted Solution

by:
mvdeveloper earned 500 total points
ID: 37802264
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
 

Author Comment

by:26fconsulting
ID: 37802307
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37802308
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
 

Author Comment

by:26fconsulting
ID: 37802351
@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
 
LVL 5

Expert Comment

by:mvdeveloper
ID: 37802354
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37802376
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
 

Author Comment

by:26fconsulting
ID: 37802383
@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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:26fconsulting
ID: 37802448
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37802456
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
 

Author Comment

by:26fconsulting
ID: 37802469
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
 
LVL 5

Expert Comment

by:mvdeveloper
ID: 37802499
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
 

Author Comment

by:26fconsulting
ID: 37802530
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
 
LVL 5

Expert Comment

by:mvdeveloper
ID: 37802703
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
 
LVL 51

Expert Comment

by:Bill Prew
ID: 37802722
@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
 

Author Closing Comment

by:26fconsulting
ID: 37832506
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now