spaces at ends of filenames or directory names on Win32

R

rtilley

# Spaces are present before and after the XXX
filename = ' XXX '

new_filename = filename.strip()

if new_filename != filename:
print filename

Macs allow these spaces in file and folder names. Which is OK. The
problem arises when the file or folder is copied to a PC running Windows
from a Mac. Windows allows the Mac to copy the file to it, but when
Windows itself attempts to do anything with the file it strips the
spaces and then tries to move it, copy it, etc and complains that the
file isn't there!

I can rectify this by striping the spaces from the files and folders on
the Mac before they are copied to the PC or mounting the Windows share
from the Mac and running a recursive strip program I wrote, but Windows
will not allow the whitespace removal directly from within Windows....
how annoying!

For example... tell windows to move a file named ' XXX ' (one space
before and one space after the filename). Windows will complain that
file 'XXX' does not exist. It's correct of course, 'XXX' does not exist,
but ' XXX ' does indeed exist.

Can anyone rescue me from this madness :(

Many Thanks,
Brad
 
L

Larry Bates

rtilley said:
# Spaces are present before and after the XXX
filename = ' XXX '

new_filename = filename.strip()

if new_filename != filename:
print filename

Macs allow these spaces in file and folder names. Which is OK. The
problem arises when the file or folder is copied to a PC running Windows
from a Mac. Windows allows the Mac to copy the file to it, but when
Windows itself attempts to do anything with the file it strips the
spaces and then tries to move it, copy it, etc and complains that the
file isn't there!

I can rectify this by striping the spaces from the files and folders on
the Mac before they are copied to the PC or mounting the Windows share
from the Mac and running a recursive strip program I wrote, but Windows
will not allow the whitespace removal directly from within Windows....
how annoying!

For example... tell windows to move a file named ' XXX ' (one space
before and one space after the filename). Windows will complain that
file 'XXX' does not exist. It's correct of course, 'XXX' does not exist,
but ' XXX ' does indeed exist.

Can anyone rescue me from this madness :(

Many Thanks,
Brad

How about not naming files with leading and trailing spaces on
the Mac? Seems like a bad habit that needs breaking ;-).

-Larry Bates
 
R

rtilley

Larry said:
How about not naming files with leading and trailing spaces on
the Mac? Seems like a bad habit that needs breaking ;-).

-Larry Bates

Users will be users! Tell that to the guys and gals on Macs who like to
make a folder sort based on the number of spaces they've placed in the
front of the filenames :)
 
L

Larry Bates

rtilley said:
Users will be users! Tell that to the guys and gals on Macs who like to
make a folder sort based on the number of spaces they've placed in the
front of the filenames :)

Sounds like you should change leading/trailing spaces to something
like underlines instead of stripping them off the filename. That way
you preserve the character positions, visually the filenames are very
close. Problem is that underlines sort to bottom instead of to the
top of a list of files.

On second thought, if you replace spaces with dashes they will sort
correctly, but they will look odd.

Pick your poison.

-Larry
 
M

mensanator

rtilley said:
# Spaces are present before and after the XXX
filename = ' XXX '

new_filename = filename.strip()

if new_filename != filename:
print filename

Macs allow these spaces in file and folder names. Which is OK. The
problem arises when the file or folder is copied to a PC running Windows
from a Mac. Windows allows the Mac to copy the file to it, but when
Windows itself attempts to do anything with the file it strips the
spaces and then tries to move it, copy it, etc and complains that the
file isn't there!

I can rectify this by striping the spaces from the files and folders on
the Mac before they are copied to the PC or mounting the Windows share
from the Mac and running a recursive strip program I wrote, but Windows
will not allow the whitespace removal directly from within Windows....
how annoying!

For example... tell windows to move a file named ' XXX ' (one space
before and one space after the filename). Windows will complain that
file 'XXX' does not exist. It's correct of course, 'XXX' does not exist,
but ' XXX ' does indeed exist.

Can anyone rescue me from this madness :(

Use quatation marks.
copy tt1.txt " XXX "
1 file(s) copied.
Volume in drive C is XXXXPXXABA
Volume Serial Number is 90C0-740E

Directory of C:\Documents and Settings\Desktop

File Not Found
dir " XXX
Volume in drive C is XXXXPXXABA
Volume Serial Number is 90C0-740E

Directory of C:\Documents and Settings\Desktop

02/06/2006 07:05p 10 XXX
1 File(s) 10 bytes
0 Dir(s) 2,063,822,848 bytes free
Volume in drive C is XXXXPXXABA
Volume Serial Number is 90C0-740E

Directory of C:\Documents and Settings\Desktop

02/23/2006 03:20p <DIR> .
02/23/2006 03:20p <DIR> ..
02/06/2006 07:05p 10 XXX
01/20/2006 04:53p <DIR> AGMP
01/20/2006 04:53p <DIR> BPWR Fe-Mn
01/20/2006 04:53p <DIR> GM
01/20/2006 04:53p <DIR> IDEM response
01/20/2006 04:53p <DIR> msgs
01/20/2006 04:53p <DIR> New Folder
02/08/2006 05:29p <DIR> New Folder (2)
01/20/2006 04:53p <DIR> OCAML
01/20/2006 04:53p <DIR> review05
01/20/2006 04:53p <DIR> scanned docs
02/17/2006 03:07p <DIR> USS repository
1 File(s) 10 bytes
13 Dir(s) 2,063,822,848 bytes free

Note that the trailing quote isn't always necessary.
Note from the last listing that the XXX file is indented one space.
 
S

Steven D'Aprano

How about not naming files with leading and trailing spaces on
the Mac? Seems like a bad habit that needs breaking ;-).

Why is it a bad habit? Because *Windows* is primitive enough that it can't
cope with leading and trailing spaces? I don't see why Windows' lack is
Mac users' problem.
 
L

Larry Bates

Steven said:
Why is it a bad habit? Because *Windows* is primitive enough that it can't
cope with leading and trailing spaces? I don't see why Windows' lack is
Mac users' problem.
It is a problem because the poster says it is a problem for him.
If they want to leave the files on a Mac, there is no problem.

IMHO leading and/or trailing spaces in filenames is asking for
incompatibilities with cross-platform file access. Much like
using single-quote in filenames which are perfectly legal in
DOS/Windows, but Linux doesn't like much.

-Larry Bates
 
J

Jeffrey Schwab

Larry said:
IMHO leading and/or trailing spaces in filenames is asking for
incompatibilities with cross-platform file access.

With what platforms specifically?
Much like
using single-quote in filenames which are perfectly legal in
DOS/Windows, but Linux doesn't like much.

Uh... What Linux are you using? And what FS?

$ touch "'" && ls
'
$ rm "'"
$
 
S

Steven D'Aprano

It is a problem because the poster says it is a problem for him.

Absolutely.

Now read my statement again. Why is it a problem for the Mac _users_?

I use Linux, Windows and Mac, in varying amounts. I'm fully aware of the
problems of transferring files from one platform to another. When it
affects _me_, I may choose to dumb down to the lowest common denominator
so as to save _me_ problems. But wearing my user hat, if a developer came
to me telling me I had to avoid using features on my platform of choice in
order to make his life easier, I'd say to him "So why exactly are we
paying you the big bucks? That's your problem, you solve it."
 
T

Tim Roberts

rtilley said:
For example... tell windows to move a file named ' XXX ' (one space
before and one space after the filename). Windows will complain that
file 'XXX' does not exist. It's correct of course, 'XXX' does not exist,
but ' XXX ' does indeed exist.

Are you sure you're doing it right? It works for me. Notice that I have
"x.c" and " x.c " in the same directory at the same time.

C:\tmp\x>copy ..\x.c .
1 file(s) copied.

C:\tmp\x>dir
Volume in drive C has no label.
Volume Serial Number is 70CF-E8F4

Directory of C:\tmp\x

02/24/2006 11:48 PM <DIR> .
02/24/2006 11:48 PM <DIR> ..
02/22/2006 10:49 PM 539 x.c
1 File(s) 539 bytes
2 Dir(s) 50,937,999,360 bytes free

C:\tmp\x>copy x.c " x.c "
1 file(s) copied.

C:\tmp\x>dir
Volume in drive C has no label.
Volume Serial Number is 70CF-E8F4

Directory of C:\tmp\x

02/24/2006 11:48 PM <DIR> .
02/24/2006 11:48 PM <DIR> ..
02/22/2006 10:49 PM 539 x.c
02/22/2006 10:49 PM 539 x.c
2 File(s) 1,078 bytes
2 Dir(s) 50,937,999,360 bytes free

C:\tmp\x>erase " x.c "

C:\tmp\x>dir
Volume in drive C has no label.
Volume Serial Number is 70CF-E8F4

Directory of C:\tmp\x

02/24/2006 11:49 PM <DIR> .
02/24/2006 11:49 PM <DIR> ..
02/22/2006 10:49 PM 539 x.c
1 File(s) 539 bytes
2 Dir(s) 50,937,999,360 bytes free

C:\tmp\x>
 
D

drobinow

For example... tell windows to move a file named ' XXX ' (one space
before and one space after the filename). Windows will complain that
file 'XXX' does not exist. It's correct of course, 'XXX' does not
exist,
but ' XXX ' does indeed exist.

Can anyone rescue me from this madness :(
 
L

Larry Bates

Jeffrey said:
With what platforms specifically?


Uh... What Linux are you using? And what FS?

$ touch "'" && ls
'
$ rm "'"
$

I stand corrected if you put double quotes around filenames
it does work. That to me means that single quotes
in filenames are somehow "different" than other characters.
You must handle these filenames differently (at least from
the command line). Thanks for pointing this out.

-Larry Bates
 
L

Larry Bates

Steven said:
Absolutely.

Now read my statement again. Why is it a problem for the Mac _users_?

I use Linux, Windows and Mac, in varying amounts. I'm fully aware of the
problems of transferring files from one platform to another. When it
affects _me_, I may choose to dumb down to the lowest common denominator
so as to save _me_ problems. But wearing my user hat, if a developer came
to me telling me I had to avoid using features on my platform of choice in
order to make his life easier, I'd say to him "So why exactly are we
paying you the big bucks? That's your problem, you solve it."
I understand you completely, but its a problem for the Mac users because
their company needs to do something with their files that is making it
hard. I see no difference than if some user chose to use some obscure
spreadsheet or wordprocessing program that works extremely well for them
but is completely incompatible with all other users in the organization.
Why is it their problem? Because it makes it difficult for others to
work with their data. I don't believe for a minute you would tell them
"It is ok to keep using your wordprocessing software, if we can't read
the files that's our problem, we will write something to convert them
to a more usable format. It will only take about 500/1000/2000 hours
to fix for you Mr. _user_."

Funny how we got here. I just made a suggestion as to how the user can
make the problem go away altogether and my suggestion that it was a bad
habit did have a smiley ;-) as you will note.

-Larry Bates
 
J

Jeffrey Schwab

Larry said:
I stand corrected if you put double quotes around filenames
it does work. That to me means that single quotes
in filenames are somehow "different" than other characters.
You must handle these filenames differently (at least from
the command line). Thanks for pointing this out.

Sure, no problem. FYI, the quotes are to keep my shell, which happens
to be bash, from trying to interpret the quote. If I were renaming a
file by clicking the icon in a Windows-like GUI, or using a file
manager, there would be no need for the quote.
 
T

Tim Roberts

For example... tell windows to move a file named ' XXX ' (one space
before and one space after the filename). Windows will complain that
file 'XXX' does not exist. It's correct of course, 'XXX' does not
exist,
but ' XXX ' does indeed exist.

Can anyone rescue me from this madness :(

Use double-quotes on Windows, not single-quotes. Single-quotes are taken
as just another filename character.
 
R

rtilley

Please post your Python code. I don't see the problem you're
describing.

OK, here's a copy. This works on Mac/Unix/Linux yet has no effect on
Windows:

-----------------------------------------------------------------
import os
import os.path

for root, dirs, files in os.walk(os.getcwd()):
for d in dirs:
new_d = d.strip()

## Uncomment the next 4 lines just to print dirs with end spaces.
## if new_d != d:
## print d
## else:
## print "No end spaces."

## Uncomment the next 7 lines to try a rename of dirs with end spaces.
## if new_d != d:
## try:
## new_path = os.path.join(root, new_d)
## old_path = os.path.join(root, d)
## os.renames(old_path, new_path)
## except Exception, e:
## print e
------------------------------------------------------------------
 
R

rtilley

Please post your Python code. I don't see the problem you're
describing.

OK, here's a copy. This works on Mac/Unix/Linux yet has no effect on
Windows:

-----------------------------------------------------------------
import os
import os.path

for root, dirs, files in os.walk(os.getcwd()):
for d in dirs:
new_d = d.strip()

## Uncomment the next 4 lines just to print dirs with end spaces.
## if new_d != d:
## print d
## else:
## print "No end spaces."

## Uncomment the next 7 lines to try a rename of dirs with end spaces.
## if new_d != d:
## try:
## new_path = os.path.join(root, new_d)
## old_path = os.path.join(root, d)
## os.renames(old_path, new_path)
## except Exception, e:
## print e
------------------------------------------------------------------
 
R

rtilley

This will at least allow me to ID folders that start with whitespace...
from within Windows too :) yet I still cannot rename the folders after
stripping the whitespace... attempting to gives an [Errno 2] No such
file or directory. The strip seems to work right too according to the
prints before and after.

import os
import os.path
import string

dirs = os.listdir(os.getcwd())
path = os.getcwd()
##print path

for d in dirs:
# If the first space in a folder name is whitespace.
if d[0] in string.whitespace:
## print d
try:
new_path = os.path.join(path, d.strip())
old_path = os.path.join(path, d)
print new_path
print old_path
os.renames(old_path, new_path)
except Exception, e:
print e
else:
pass
 
R

Roel Schroeven

rtilley schreef:
This will at least allow me to ID folders that start with whitespace...
from within Windows too :) yet I still cannot rename the folders after
stripping the whitespace... attempting to gives an [Errno 2] No such
file or directory. The strip seems to work right too according to the
prints before and after.

Does the rename work if try with other names? Maybe the problem is not
the whitespace; maybe Windows can't rename the folder because some
process, possibly your program, has an open handle to a file or
directory under your folder? Unix-systems are able to handle that
easily, but Windows doesn't.
 
R

rtilley

Roel said:
rtilley schreef:
This will at least allow me to ID folders that start with
whitespace... from within Windows too :) yet I still cannot rename the
folders after stripping the whitespace... attempting to gives an
[Errno 2] No such file or directory. The strip seems to work right too
according to the prints before and after.


Does the rename work if try with other names?

Yes, the script can rename files that have no end whitespace. Also, I
should note the the Windows GUI cannot rename the files. Same error...
cannot stat the file. The only way I've been able to rename is through
the cmd prompt using quotes like this:

ren " bad file with end spaces " good_file_no_end_spaces

I should also note that the code I posted earlier is misleading as
os.listdir() gets dirs and files... not just dirs as I implied. Here's a
better example that should get whitespace at either end (and does indeed
on Mac and Unix... but not Windows)

--------------------------------------------------------

import os
import os.path
import string

# dirs is actually files and folders, not just folders.
dirs = os.listdir(os.getcwd())
path = os.getcwd()
print path

for d in dirs:

# If folder name begins with whitespace.
if d[0] in string.whitespace:
print d
try:
new_path = os.path.join(path, d.strip())
old_path = os.path.join(path, d)
print new_path
print old_path
os.renames(old_path, new_path)
except Exception, e:
print e

# If folder name ends with whitespace.
elif d[-1] in string.whitespace:
print d
try:
new_path = os.path.join(path, d.strip())
old_path = os.path.join(path, d)
print new_path
print old_path
os.renames(old_path, new_path)
except Exception, e:
print e

# Folder name is OK, so skip it.
else:
pass
 

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

Staff online

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top