473,385 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Adding PC Filename Extensions to Macintosh Filenames

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.
Jul 18 '05 #1
1 2350
On Tue, 05 Aug 2003 22:45:25 -0400, rumours say that hokiegal99
<ho********@vt.edu> 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...d0qh%404ax.com
--
TZOTZIOY, I speak England very best,
Microsoft Security Alert: the Matrix began as open source.
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ralph Freshour | last post by:
I have a PHP web app using MySQL - when I save a .jpg file named test's.jpg I see that the filename on the unix server is: test\'s.jpg - the filename I end up saving in my SQL table is named...
3
by: Martin Lucas-Smith | last post by:
Is there some way of using ereg to detect when certain filename extensions are supplied and to return false if so, WITHOUT using the ! operator before ereg () ? I have an API that allows as an...
11
by: hokiegal99 | last post by:
How would I determine if a filename is greater than a certain number of characters and then truncate it to that number? For example a file named XXXXXXXXX.txt would become XXXXXX fname = files...
27
by: gmtonyhoyt | last post by:
I need assistance coming up with a clean way to handle filename extensions with my application. While I can come up with several ways of doing so on my own, I felt perhaps it would be worth...
6
by: Maheshkumar.R | last post by:
How i can store images in array @ runtime..? What are the possbile ways.. Thankz.. - Mähésh Kumär. R
4
by: Ken Swanson | last post by:
Hi, In VB.NET, how do I determine what filename has been dropped into a ListView? In my ListView's DragEnter() event, I am trying this, which according to the Help, should work: MyFilename...
3
by: dick | last post by:
Hello, I want to encrypt a file with its filename. I can encrypt the file itself using the frame libraries. How can I now best hide the filename and extension? Within the file, ... ? I...
5
by: Amjad | last post by:
Hi, I want to write a For loop that will put file names of extensions (*.txt and *.csv) in an array. I had it work fine for one extension, and I need help making it work for two extensions. My...
12
by: IamIan | last post by:
I searched the archives but couldn't find anyone else with this problem. Basically I'm grabbing all ASCII files in a directory and doing geoprocessing on them. I need to calculate a z-factor based...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.