Re: Validating a file name
- From: Philipp <sicsicsic@xxxxxxxxxxx>
- Date: Fri, 20 Oct 2006 15:51:49 +0200
David Lee Lambert wrote:
On Fri, 20 Oct 2006 14:32:01 +0200, Philipp wrote:
I want to create a file with a filename coming from a string (which I have no control over).
Is there a package to check if the string is valid as a filename on the OS (my case Win32)?
The package would check for:
- invalid characters (: / \ ? etc)
- control characters
- length of string (max 215 chars on win32 I think)
etc.
I have not been able to find anything like that browsing the perl faq.
Well, the MS Tablet PC API accepts the following pattern for file-names:
* For file name, allows all IS_ONECHAR characters except: \ / : < > |
This is easy to test with regular expressions, e.g.
$fn_ok = ($fn =~ m!^[^\\/:<>|]+$! and $fn !~ m![\x00-\x1f\x7f-\x9f]!)
However, in input validation, it's generally better to test for
compliance with the rules rather than nondangerousness. Something like
one of these would probably be better, and more readable:
$fn_ok - ($fn =~ m!^[a-z0-9 _+-]+$!i); # ASCII alphanumeric, space,
# hyphen, underline, plus
$fn_ok = ($fn =~ m!^[0-9\pL-]$); # Unicode letter, ASCII number, hyphen
Thank you for your answer.
I tried this kind of validation (regexp), but it failed on my lack of knowledge (I didn't know exactly what characters are allowed in win filename).
Phil
.
- References:
- Validating a file name
- From: Philipp
- Re: Validating a file name
- From: David Lee Lambert
- Validating a file name
- Prev by Date: Re: Validating a file name
- Next by Date: How can i find source code like Data::Dumper in perl package
- Previous by thread: Re: Validating a file name
- Next by thread: perlembed - how to get 'use constant' values
- Index(es):
Relevant Pages
|