Adding PC Filename Extensions to Macintosh Filenames

Discussion in 'Python' started by hokiegal99, Aug 6, 2003.

  1. hokiegal99

    hokiegal99 Guest

    I've written a small script (with much help from this list) that
    searches through a directory for files without a PC filename extension
    like .doc .xls and then adds them. The reason for writing the script is
    that I'm migrating several dozen Mac users to PCs over the next few
    months and many of the Mac files do not have PC filename extensions.
    There are thousands of these files. The script runs on a Linux ftp
    server where the files are upload to.

    In testing, the script works OK. It needs work, for example I need to
    make certain that the strings I'm searching for to identify file types
    are correct. This would be easier if I could assign more than one string
    to string.find. I'm considering doing this:

    setpath = raw_input("Enter the path: ")
    for root, dirs, files in os.walk(setpath):
    old_fname = files
    for old_fname in files:
    new_fname = old_fname + '.xls'
    exclude = string.find(old_fname,'.xls')
    x = string.find(old_fname,'Excel.Sheet.')
    y = string.find(old_fname,'Microsoft Excel')
    z = string.find(old_fname,'Worksheet')
    p = x, y, z
    if p >= 1 and exclude == -1:
    newpath = os.path.join(root,new_fname1)
    oldpath = os.path.join(root,old_fname)
    os.rename(oldpath,newpath)
    print oldpath
    print newpath

    The script as it exists now is attached to this email. Please critique
    it and let me know how I can make it better or more accurate. All advice
    is welcome both positive and negative. I'd especially like to hear for
    people who could show me how I might make the script smaller and more
    efficent and ways in which I might ID file types more accurately.

    Thanks!!!

    #---- Use with latest Python 2.3 stable
    import os, string
    print " "
    setpath = raw_input("Enter the path: ")
    for root, dirs, files in os.walk(setpath):
    old_fname = files
    for old_fname in files:
    #---- New filename is the old filename with extension appended.
    new_fname1 = old_fname + '.xls'
    new_fname2 = old_fname + '.doc'
    new_fname3 = old_fname + '.wpd'
    new_fname4 = old_fname + '.rtf'
    #---- If Filename has an extension already, then exclude it from the rename.
    exclude1 = string.find(old_fname,'.xls')
    exclude2 = string.find(old_fname,'.doc')
    exclude3 = string.find(old_fname,'.wpd')
    exclude4 = string.find(old_fname,'.rtf')
    #---- The content to search through the files for to identify what type of file it is.
    content1 = string.find(file(os.path.join(root,old_fname), 'rb').read(), 'Excel.Sheet.')
    content2 = string.find(file(os.path.join(root,old_fname), 'rb').read(), 'Word.Document.')
    content3 = string.find(file(os.path.join(root,old_fname), 'rb').read(), 'Created with WordPerfect')
    content4 = string.find(file(os.path.join(root,old_fname), 'rb').read(), 'rtf1')
    #---- If content is found in files and if files don't have extension, then do the rename.
    if content1 >= 1 and exclude1 == -1:
    newpath = os.path.join(root,new_fname1)
    oldpath = os.path.join(root,old_fname)
    os.rename(oldpath,newpath)
    print oldpath
    print newpath
    if content2 >= 1 and exclude2 == -1:
    newpath = os.path.join(root,new_fname2)
    oldpath = os.path.join(root,old_fname)
    os.rename(oldpath,newpath)
    print oldpath
    print newpath
    if content3 >= 1 and exclude3 == -1:
    newpath = os.path.join(root,new_fname3)
    oldpath = os.path.join(root,old_fname)
    os.rename(oldpath,newpath)
    print oldpath
    print newpath
    if content4 >= 1 and exclude4 == -1:
    newpath = os.path.join(root,new_fname4)
    oldpath = os.path.join(root,old_fname)
    os.rename(oldpath,newpath)
    print oldpath
    print newpath
    print " "
    print "--- Done ---"
    print " "
    #---- End of Script.
    #
    # Possible strings to search for:
    # 'Microsoft Excel Worksheet' 'Microsoft Excel 5.0 Worksheet' 'Excel.Sheet.' Excel
    # 'Microsoft Word Document' 'Microsoft Word 6.0 Document' 'Word.Document.' Word
    # 'WordPerfect' 'Created with WordPerfect' '{WP}' WordPerfect
    # '{\rtf1' RTF
    #
    #string.find( open('my_binary_file','rb').read(), 'string_you're_looking_for' )
    # lookfor2 = re.compile('Microsoft Excel Worksheet')
    #
    # Need to verify that the strings we're searching for are
    # accurate identifying material. And, need to add power point files and others
    # to the list of files that need extensions appended. Then need to clean up code,
    # test heavily, condense code and rewrite as a function.
    hokiegal99, Aug 6, 2003
    #1
    1. Advertising

  2. On Tue, 05 Aug 2003 22:45:25 -0400, rumours say that hokiegal99
    <> might have written:

    [snip]
    >and more
    >efficent and ways in which I might ID file types more accurately.


    You might want to give this a shot (single line URL):

    http://groups.google.com/groups?selm=
    --
    TZOTZIOY, I speak England very best,
    Microsoft Security Alert: the Matrix began as open source.
    Christos TZOTZIOY Georgiou, Aug 7, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lauchlan M

    filenames / extensions with file uploads

    Lauchlan M, Mar 2, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    357
    Lauchlan M
    Mar 3, 2004
  2. =?Utf-8?B?Sm9l?=

    Extract filename from a filename typed by user

    =?Utf-8?B?Sm9l?=, Aug 23, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,002
    Travis Murray
    Aug 24, 2004
  3. B.J.
    Replies:
    4
    Views:
    725
    Toby Inkster
    Apr 23, 2005
  4. Replies:
    1
    Views:
    1,427
    Roland de Ruiter
    Jun 15, 2006
  5. Ed
    Replies:
    10
    Views:
    45,718
    alok000707
    Jul 13, 2010
Loading...

Share This Page