Searching for a string in an external file

Find out why the . goes before the /

Moderator: Paul Tuersley

philspitler
Posts: 39
Joined: November 2nd, 2005, 10:20 am
Contact:

I am trying to figure out how to search an external file for a string then display the string and everything after it until it finds an "$"

I can open the file for reading and through power of the Google, I found I search for a string in the file but am at a loss on how to then display the text.

Any help would be great.

Thanks.

Phil
Phil Spitler
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:

Hey Phil,

If you post the code up to where it is working and then it will be easier to help you. You will need to use Regular Expressions to find the string and everything after it until the $. The regular expression for that would be this:

Code: Select all

var a = "czxdfsdjStringaskdjkjneni$asdijoaua";

alert(a.match(/String.+\$/));
The forward slashes indicate that it is a regular expression. String says look for a literal string called String. Keep in mind this is case sensitive so it would not find string or STRING for example. Then the . says look for any character and the + says keep repeating that match until you find a $ you need to escape the $ with a backslash before it because $ is a special character in regular expressions to indicate the end of a line which you don't want in this case.

Regular expressions are kind of hard to get your head wrapped around but they are super powerful.

-Lloyd
philspitler
Posts: 39
Joined: November 2nd, 2005, 10:20 am
Contact:

Thanks Llloyd.

I tried it but the match is returning NULL which I presume means it cannot find the string. Extended script also hangs after running the script.

I'm wondering if it is an issue because I am loading the entire file into a variable (this file is about 60k). Does it mater that most of the file is just hex garbage with some human readable text at the bottom?

Thanks for all your help.

This is what I have.

var QT = File.openDialog ("Please Select file");
var readOK = QT.open("r");

if (readOK) {
var contents = QT.read();
alert(contents.match(/renderTimeStamp.+\$/));

}

else {altert ("error opening file")}
Phil Spitler
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:

Is it hex or binary data? One thing you can try is reading one line at a time and trying the match and see if that works better. If it is hanging maybe it is too much text to load at once.

Lloyd
philspitler
Posts: 39
Joined: November 2nd, 2005, 10:20 am
Contact:

It's binary data so I'm unsure how I would read a line at a time, isn't a line determined by a carriage return?

I just did a test and cut most of the "jink" out of my file and ran the script and it worked so it must be a problem reading the whole file at once.


Thanks.

Phil
Last edited by philspitler on December 27th, 2010, 5:03 pm, edited 1 time in total.
Phil Spitler
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:

If it's binary data then I am not sure you'll be able to do it with extendscript but I would try the line by line trick. To read a line at a time use readln() instead of read().

Lloyd
philspitler
Posts: 39
Joined: November 2nd, 2005, 10:20 am
Contact:

Thanks Llloyd, I presume I would need set up a loop to loop through the file line by line?

I'll give it a go.

Phil
Phil Spitler
philspitler
Posts: 39
Joined: November 2nd, 2005, 10:20 am
Contact:

It tried the readln function and it worked great on my stripped down file but the file that still has the junk at the front still seems to crash.

The file was created from QT Player, basically it's a reference movie.

This is the code I cam up with.

Any other ideas?



var QT = File.openDialog ("Please Select file");
var readOK = QT.open("r");

if (readOK) {

do{
var contents = QT.readln();
var filtered = (contents.match(/renderTimeStamp.+\$/));
if (filtered != null) {
alert (filtered);
}


}

while (QT.eof == false)}

else {alert ("error opening file")}
Phil Spitler
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:

Like I mentioned before, not sure you can read in a binary file with extendscript so this is most likely what is stopping you. What are you trying to find out from the reference movie? Is there any other way to get this info?
philspitler
Posts: 39
Joined: November 2nd, 2005, 10:20 am
Contact:

The info is only available in a QT ref movie.

One thought I just had was this...

WhenI strip the file down in textedit then my script works fine, so I'm guessing extendedScript fails while reading all the binary stuff.

I just tried using the seek function to seek backwards from the eof, I was hopeful but it still crashes.

I'll try and do some debugging when I get chance to see exactly at what point the script crashes.

Thanks Llloyd.
Phil Spitler
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:

Try setting the encoding of the file before reading through it to see if that helps:

Code: Select all

var readOK = QT.open("r");
QT.encoding = "UTF-8";
Otherwise you can try duplicating the file then using applescript open and save the file with TextEdit or TextWrangler to see if it converts it to a text file.

-Lloyd
Dan Ebberts
Posts: 320
Joined: June 26th, 2004, 10:01 am
Location: Folsom, CA
Contact:

Did you try the binary encoder?

QT.encoding = "binary";


Dan
philspitler
Posts: 39
Joined: November 2nd, 2005, 10:20 am
Contact:

Great, I have it working.

I tried the utf-8 encoding and it matched the string but still hung ExtendScript.

I figured it must be something after my string that was crashing it so I simply put a break in the loop after it found my string and that solved the problem.

I got the same result using "binary".

So, it seems like I have a workaround.

Thanks again.

Phil
Phil Spitler
philspitler
Posts: 39
Joined: November 2nd, 2005, 10:20 am
Contact:

Bummer.... although it works if it can find the string, if it cannot find the string it hangs Extended Script.

This is what I have, I have also attached 2 files, one that works and one that crashes the system (because it doesn't have the match string).

Any other thoughts?

Thanks.

Phil


var QT = File.openDialog ("Please Select file");
var readOK = QT.open("r");
QT.encoding = "UTF-8";

if (readOK) {
do{
var contents = QT.readln();
var filtered = (contents.match(/fullPath.+\"/)); //look for the creator atom
if (filtered != null) {
alert (filtered);
break;
}
}

while (QT.eof == false)
}

else {alert ("error opening file")}

if (filtered == null) {alert ("project link not found");} // cannot find the string
Attachments
QT-refs.zip
(10.53 KiB) Downloaded 681 times
Phil Spitler
philspitler
Posts: 39
Joined: November 2nd, 2005, 10:20 am
Contact:

Interestingly, while ExtendedScript crashes when it reaches the eof. AE doesn't (if the script is ran from the AE scripts menu).

So I think that all is good (for now).

Thanks.

Phil
Phil Spitler
Post Reply