Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rate Thread Display Modes
Old 06-16-2003, 07:48 PM   #1
vonleigh
All Star
 
Join Date: Jan 2002
Posts: 579
Finding files by creator code

Hello,

I'm trying to find all files with a certain creator code. I tried googling for a while and since I didn't come up with anything interesting, decided to try and script it.

The problem I'm running into is one of quoting, the find I'm using to generate a list of files gets broken up at the space between files, for example it fails with "Address Book" or the like. I tried looking at find's man page and the only option similar was -X but it's not quite what I need.

Any help is appreciated, here's the script:


Code:
#!/bin/sh

# Find by creator from current dir
list=`find . -type f`

for file in $list
do
code=`GetFileInfo -c "${file}"`

        if [ "$code" == "\"CODE\"" ]
        then
                echo $file
        fi
done

exit 0
Replace CODE with creator code. I'll make it more interactive later with options and submit it, right now I just want it to work.


thanks,
v


Edit:

Here's usage and errors:

Code:
% touch file
% touch "some file"
% SetFile -c CODE file
% SetFile -c CODE some\ file
% ./script
./file
GetFileInfo: could not refer to file (-43)
file
./file is the path to "file", which is the intended output. However "some file" is failing and getting evaluated as "some" and "file" (the second one succeeding btw)

Last edited by vonleigh; 06-16-2003 at 07:53 PM.
vonleigh is offline   Reply With Quote
Old 06-16-2003, 10:14 PM   #2
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 31,939
Perl version

The problem arises because the Bourne shell's lists are space-delimited. I don't know how to avoid this problem in the Bourne shell.
But here's a Perl version that works:
Code:
#!/usr/bin/perl -w

die "Must supply creator code as argument\n" unless $ARGV[0];
my $target = $ARGV[0]; # creator code to be looked for
$target = "\"$target\"";  # add quotes before and after code

my @list = `find . -type f`;
foreach my $file (@list)
{
    chomp($file);
    my $code = `/Developer/Tools/GetFileInfo -c "$file"`;
    chomp($code);          
    if ($code eq $target)      
    {
        print "$file\n";
    }
}
Note: the 'chomp()' function removes the newlines that come from the backticks commands.
hayne is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting 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

Forum Jump



All times are GMT -5. The time now is 01:46 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.