Adding PC Filename Extensions to Macintosh Filenames

H

hokiegal99

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top