27 July 2009

Don't ask questions in Windows filenames.

Windows doesn't support the ? character in filenames, so Cygwin doesn't, either.

I have a bunch of files in a mercurial repository with question marks in the name. (They're dumps of web pages for testing a bunch of scripts that process web pages, so they're of the form foo.cgi?bar=3&foo=someargument. I noticed after untarring the archive on Windows (I had to move it from the Linux half of my laptop on a MicroSD card; don't ask.) that tar was choking on all of the filenames with ? marks.

You can fix that quite easily with a script like this. (You'll have to run it on a Linux machine or something else that doesn't fail on ? marks, of course.)
import subprocess, os
names = subprocess.Popen(r"find . -name '*\?*'", shell=True, \
for name in names.split("\n"):
name = name.strip()
if not name:
print name
os.system(r"hg mv '%s' '%s'" % (name, name.replace("?", "@")))
I would have done this in bash, but I couldn't figure out how to iterate over filenames with spaces in them.

No comments:

Post a Comment

Blog Archive

About Me

blog at barillari dot org Older posts at http://barillari.org/blog