PDA

View Full Version : Need silly cscript



IronBits
06-23-2007, 01:35 AM
finder.vbs "Text to Find" "Filename to look in.txt" or .xml but still TEXT.

Open File {
read one line at a time and look for "Text to Find"
If found - stop reading file ; exit (1)
}
else
{ exit (0) }

Close file

If the String is there, stop reading the file and give me a %errorlevel% 1, else %errorlevel% 0 is good if not found. ;)
Filename could be on a local HDD or "\\Servername\Sharename\Folder\Folder\Filename to look in.txt" (under 255 chars)
"String to find" and "PATH" will be inside double quotes (a given)

If file doesn't exist - exit 0 is fine - if you want to print Found String, that's fine, but I'm only looking for the EL.
If no command line params are provided, print Example: finder.vbs "string to find" "filename to read.txt" ;)

I can do this in Perl, but need something to run in a DOS/Windows batchfile.
Find and Findstr read through the whole file before returning which slows down the whole process as sometimes the files are several megs and over very slow network connections. :(
TIA! :)

Darkness Productions
06-23-2007, 01:14 PM
Your best bet for something like this is grep for windows... It'll make things much easier.

IronBits
06-24-2007, 09:16 AM
Can't do that, and, they also read the whole file.
If someone can't come up with the simple code for cscript, I may have to write it in Perl and compile it.

IronBits
06-24-2007, 11:59 AM
Something like this maybe?
I need to specify the "filename.ext" and "StRiNg" on the command line.

$hFileRead = FileOpen("Filename.txt", 0)

; Check if FileOpen failed.
If $hFileRead = -1 Then Exit (0)
EndIf

; Read in lines of text until the EOF is reached
While 1
$sLine = FileReadLine($hFileRead)
If @error = -1 Then ExitLoop
If StringInStr($sLine, 'text to Find', 1) Then Exit (1)
EndIf
Wend

FileClose($hFileRead)

IronBits
06-28-2007, 10:56 PM
I can do this with Perl, anyone able to convert this to a .vbs script for me?



#!/perl/bin/perl -w
#finder.pl
# * Make sure we have a file to parse
if (!$ARGV[0]) {die "You must provide an input filename.ext to read!\n";}
# ***** Open Input and Output File Handles
open (FILENAME, $ARGV[0]) or die "Couldn't open $ARGV[0] : $!";
while ($line = <FILENAME>) {
if ($line =~ /Job\stype:\sBackup/) {
exit (1);
}
}
close(FILENAME);

Darkness Productions
06-29-2007, 09:51 PM
IB -

You should be able to follow the code on this page fairly easily. I'm on a Linux box, otherwise, I'd write it up for you...

http://www.devguru.com/technologies/vbscript/quickref/textstream_readline.html

IronBits
07-02-2007, 01:56 AM
I'll wait until you have time to write it up, if you don't mind.
You can send it to me via email, or post here.
Thanks! :smoking:

Darkness Productions
07-02-2007, 08:19 AM
This should work. Give it a try, and let me know. Obviously, modify filename and rxpattern variables to suit.



Const ForReading = 1, ForWriting = 2, ForAppending = 8

Dim filename, rxpattern

filename = "D:\src\freedc\file.txt"
rxpattern = "Some.other.text"

Dim fso, rfile, fcontents, oRX
Set fso = CreateObject("Scripting.FileSystemObject")
Set rfile = fso.OpenTextFile(filename, ForReading)

Set oRX = New RegExp

oRX.Global = True
oRX.IgnoreCase = True
oRX.Pattern = rxpattern

Do While rfile.AtEndOfStream <> True
fcontents = rfile.ReadLine
Set Matches = oRX.Execute(fcontents)

If (NOT IsNull(Matches)) Then
WScript.StdOut.Write "Match found. Exiting"
Exit Do
End If
Loop
rfile.close
Set rfile = Nothing

IronBits
07-02-2007, 06:52 PM
I'll give it a whirl. If it works out gooder, I'll modify it to accept input from the command line.
Thanks! :)