On Tue, 17 Jun 2008, Eric Sosman wrote:
> Tom Anderson wrote:
>> On Tue, 17 Jun 2008, Eric Sosman wrote:
>>
>>> wrote:
>>>> I was wondering if there was a quick way to ensure that a filename is
>>>> a safe.
>>>>
>>>> What I mean is that if I am creating a file from a string variable, I
>>>> want to ensure that the file will actually be able to be created. So
>>>> if it contains a "?", then clearly I would want to eliminate it.
>>>
>>> The "alphabets" for file names vary from system to system,
>>> and there are systems on which '?' is perfectly legal. So your
>>> "clearly" isn't really all that clear ...
>>
>> Oh come on, this is ridiculous.
>
> I surmise you've never needed to write code for multiple
> file systems.
>
>> The only safe and sane thing to do is to target the common set of valid
>> filenames - so exclude ?, /, \, , *, ", etc. Surely this is blindingly
>> obvious? This is not a complicated question, it's quite clear what the OP
>> wants to know, and you're not helping anyone by making a mountain out of a
>> molehill.
>
> Very well, then: "All portable file names shall consist
> of one to six decimal digits or upper-case English letters, one
> period, and zero to three decimal digits or upper-case English
> letters." If you're content with this as a least common denominator, you're
> all set.
Ah, i had indeed forgotten that there were filesystems like that!
Okay, lowest common denominator of filesystems in widespread use on
computers at present. Pre-LFN FAT32 and non-Joliet ISO 9660 don't qualify.
Although, is LFNless FAT32 used on memory cards for cameras?
>>> In general, though, you can't guarantee that a file will be creatable
>>> just by examining its name. On one widespread system,
>>> "D:\\README.TXT" is a perfectly valid file name but you are unlikely
>>> to succeed in creating a new file on a CD-ROM ... Or you may lack
>>> permission to create files in some folders, or the file system may be
>>> full, or ...
>>
>> True. And completely unconnected to what the OP asked.
>
> He asked for a file name that would quote ensure that the file will
> actually be able to be created end quote.
It was pretty clear to me from his post that that wasn't what he was
asking.
tom
--
The future is still out there, somewhere.