check lines of a text file

Deploying to Windows? Utilizing VB Script execution? This is the place to ask Windows-specific questions.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
alemrantareq
Posts: 203
Joined: Wed Jul 23, 2008 8:46 am

check lines of a text file

Post by alemrantareq » Mon Nov 10, 2008 8:46 am

hi everybody, i need to check two lines i.e. "open=test.exe" and "Go to my folder" written into a text file located in desktop folder. If these lines exist, the application will open else exit showing a message. what will be script to do it in simple? pls, help me...Thanks

SparkOut
Posts: 2945
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Mon Nov 10, 2008 9:25 am

Check the "offset" variants in the dictionary.

lineOffset sounds like it's appropriate for your use.

If you read the text file into a variable before testing, you could use

Code: Select all

If (lineOffset ("open=test.exe", theFile) is zero) or (lineOffset ("Go to my folder", theFile) is zero) then
  --exit
else
  --launch
end if
Alternatively you could "open" the file for reading and "read from file" line by line and test each line that's read, but it's probably quicker and simpler with less overall overhead to read the entire file into a variable first.
Last edited by SparkOut on Tue Nov 18, 2008 10:17 am, edited 1 time in total.

alemrantareq
Posts: 203
Joined: Wed Jul 23, 2008 8:46 am

Post by alemrantareq » Mon Nov 17, 2008 8:00 am

Dear SparkOut,

Thanks for your reply. I've tried it with lineoffset function. But it doesn't work. For example, I've a text file "test.txt" in my desktop. now, i made the script:

put specialfolderpath("desktop") & "/test.txt" into theFile
if lineOffset("open=test.txt", theFile) is zero then
answer "Line not found"
else
answer "Line was found"
end if

Is the script wrong? I've also tried wordOffset function in leau of lineOffset function, but failed. pls, help me...

SparkOut
Posts: 2945
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Mon Nov 17, 2008 9:00 am

To get the file into the tFile variable in the first place you need to get it from a url (which can be a file location on your computer). What you have so far is just testing the path/name of the file for the string. To read the file into a variable, try

Code: Select all

put specialfolderpath("desktop") & "/test.txt" into thePath
put url ("file:" & thePath) into theFile
if lineOffset("open=test.txt", theFile) is zero then 
answer "Line not found" 
else 
answer "Line was found" 
end if 

Post Reply