Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Regular Expression problems with long-filenames
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Regular Expression problems with long-filenames



Hi everyone,

I'm trying to create a script that will compare the contents of two directories and delete all of the
duplicated files in the first.

The problem comes with long filenames in OS X. The OS mangles filenames for FSSpec
representation (which I assume MPW is using) and changes names such as "This is a really long
filename that will cause OS name mangling.png" into "This is a really lon#33C91E.png"

My attempts to deal with this problem involves the following attempt (apologies if high-ASCII
characters don't make it through the list server):


echo "### DeleteEqualFiles {1} {2}"

for f in `files -o -s "{{1}}"`
    set ismatch 0

    if "{f}" =~ /(?«18,26»[¬#])®2?#[0-9A-F]«2,10»(.?«1,6»)*®3/
        # special-case processing for long file names

        for g in `files -o "{{2}}{{®2}}"?"{®3}`
            # compare data fork only
            equal -d -q "{{1}}{{f}}" "{{g}}"

            if {status} == 0
                # compare resource forks. if neither has an RF, succeeds
                equal -r -q "{{1}}{{f}}" "{{g}}"

                if {status} != 0
                    resequal -fFlags -skip 'ckid' -skip 'vers' "{{1}}{{f}}" "{{g}}"
                end

                if {status} == 0
                    set ismatch 1
                    break
                end
            end
        end
    else
        # compare data fork only
        equal -d -q "{{1}}{{f}}" "{{2}}{{f}}"

        if {status} == 0
            # compare resource forks. if neither has an RF, succeeds
            equal -r -q "{{1}}{{f}}" "{{2}}{{f}}"

            if {status} != 0
                resequal -fFlags -skip 'ckid' -skip 'vers' "{{1}}{{f}}" "{{2}}{{f}}"
            end

            if {status} == 0
                set ismatch 1
            end
        end
    end

    if {ismatch}
        echo "{{f}}"
    #   delete "{{1}}{{f}}"
    end
end

As you can see in line 4, I'm trying to detect a mangled name and do a recursive check of matches in
the target directory. If no mangling is detected, I fall through to equivalent code to do duplication
checks on the complete filename.

My test folder consists of the following files, a folder from my daughter's iBook:

files 'Macintosh HD:Users:petersb:Desktop:Stuff1:'
'"If you would make p#33C809.rtf'        # problem file 1
'"Son," the man said,#33C80A.rtf'
'*How will understand#33C80B.rtf'
.DS_Store
'Ah, well, after the #33C80D.rtf'
'And wiping the dust #33C811.rtf'
'BLUE interrupted, "Y#33C813.rtf'            # problem file 2
'God'?''s word is clear #33C816.rtf'
'I shout it to you in#33C817.rtf'
Journals....rtf
'Letter To Toni (Day 2) .rtf'
'Little Lines.rtf'
'Little Secret Sister Notes.rtf'
'My dreams for my Writing.rtf'
'Quote Book.rtf'
'The Piano'
'Value Journals.rtf'

Apparently she likes to be liberal in her use of spaces and single/double quote characters in her
filenames, and tends to make them very descriptive.

Unfortunately I am having problems in my mangled-name detector. For example, the filenames
noted above as "problem files" both generate an error in the Shell. (### MPW Shell - "s must occur in
pairs).

Also, the pattern doesn't seem to catch the mangled names that have extensions. I have been able to
catch non-extension mangles with this pattern

    if "{f}" =~ /(?«18,26»[¬#])®2?#[0-9A-F]«2,10»/

but catching all cases and getting the proper quoting is proving to be quite elusive.

Does anyone have a good mangled-name filename detector that you would be willing to share?

(And before anyone says to "just use diff in the Terminal", AFAIK diff doesn't do any checks with
resource forks, even in 10.4.x . :-)

Thanks!

Bill


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
MPW-Dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.