Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Defining a path with only a partial filename.

Defining a path with only a partial filename.
Thread Tools
Mac Enthusiast
Join Date: Oct 2001
Status: Offline
Reply With Quote
Jul 18, 2002, 05:51 AM
 
I need to point an NSTask at a file that always begins with 'logfile_' but the exact ending changes. Like these examples:

logfile_02.txt
logfile_03.txt
logfile_04.txt
etc…

There is always one and only one of these files in the parent directory at any given time. So in pseudo code, I want to tell my NSTask, 'Hey NSTask! The path to the file you need, is the only file in the directory that contains the string @"logfile_". I dont care what the ending is." What would be the easiest way to accomplish this?
3R1C
     
Forum Regular
Join Date: Oct 2001
Location: Sweden
Status: Offline
Reply With Quote
Jul 18, 2002, 06:38 AM
 
What about "logfile_*", just as you would do it at the command line? Make sure you first set the correct directory path by using setCurrentDirectoryPath:.

/Tobias
     
3R1C  (op)
Mac Enthusiast
Join Date: Oct 2001
Status: Offline
Reply With Quote
Jul 18, 2002, 07:11 AM
 
That doesn't appear to work here. Perhaps if I give more detail about what exactly Im doing, a solution will be more clear. Inside a known directory, exists a file that has the string "logfile_" in it. Inside this file, there is one line that contains the word "Completed in it. I need to get this line from this text file. What I was doing previously was using NSTask to do a grep with the word to search for and the file path to logfile_01.txt as arguments. this worked fine until I realized that the file name changed after a time. Here is my present code that only works until the file name changes.

-(void)grepForProgBarStart
{
NSTask *initializeProgressBar=[[NSTask alloc] init];
NSPipe *pipe=[[NSPipe alloc] init];
NSFileHandle *handle;
NSString *FAHOneLog = @"/Users/xaos01/Library/Application\ Support/Origami/FAH1/work/logfile_01.txt:";
NSString *string;

[initializeProgressBar setLaunchPath:@"/usr/bin/grep"];
[initializeProgressBar setArguments:[NSArray arrayWithObjects:@"Completed", FAHOneLog, nil]];
[initializeProgressBar setStandardOutputipe];
handle=[pipe fileHandleForReading];
[initializeProgressBar launch];
string=[[NSString alloc] initWithData:[handle readDataToEndOfFile] encoding:NSASCIIStringEncoding];
string = [[string componentsSeparatedByString:@": "] objectAtIndex:1];
string = [[string componentsSeparatedByString:@","] objectAtIndex:0];
[progressBar setDoubleValue:[string intValue]];
[pipe release];
[initializeProgressBar release];
}

Would it help if I wasnt using grep to do this?

Other ways to determine the right file:

Its always the second file in the directory when the directory is sorted alphabeticaly.
Its always the only file that ends in ".txt"

could i some how make an array of strings that contain the name of each file in the known directory and then use objectAtIndex to give me the string at object 1? I believe the answer lies in making an array of the file names and doing something with it but Im not sure.
3R1C
     
3R1C  (op)
Mac Enthusiast
Join Date: Oct 2001
Status: Offline
Reply With Quote
Jul 18, 2002, 08:47 AM
 
OK. I have a solution, but I'd like your opinions on its efficiency. This method is called evry 5 seconds:

-(void)grepForProgBarStart
{
NSString *logfile = [NSString alloc];
NSString *string;
NSString *file;

NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:FAHOneLog];
if ([cartman getPID:@"FAH2Console-v219-OSX.bin"] != 0) {
while (file = [enumerator nextObject]) {
if ([[file pathExtension] isEqualToString:@"txt"]) {
logfile = [logfile initWithContentsOfFile:[FAHOneLog stringByAppendingString:file]];
string = [[logfile componentsSeparatedByString:@"- Frames Completed: "] objectAtIndex:1];
string = [[string componentsSeparatedByString:@","] objectAtIndex:0];
[progressBar setDoubleValue:[string intValue]];
}
}
}
[logfile release];
}

Do any of you see a way to make this better? Is this method better than using grep to get the line?
3R1C
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 10:04 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2