Re: Validating a file name



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
.



Relevant Pages

  • Re: Validating a file name
    ... have no control over). ... Is there a package to check if the string is valid as a filename on the ... OS (my case Win32)? ...
    (comp.lang.perl.misc)
  • Validating a file name
    ... 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? ...
    (comp.lang.perl.misc)
  • Re: Why most run Microsoft, not RedHat
    ... I tried to give some examples of ease of use vs manual control. ... What happens when the package manager is compromised? ... But chattr works only as root and you can only run rpm -as root ... even if some files have the immutable flag set. ...
    (Fedora)
  • Re: [9fans] Help for home user discovering Plan 9
    ... The update/installation process in Ubuntu sucks. ... using BSD ports or Gentoo portage, you can fine tune things and have ... one can acquire a complete control over any common ... Linux distribution, can opt for tuning and, tweaking around any package ...
    (comp.os.plan9)
  • Re: Performance and Parallelism in SSIS
    ... In the system design docs, I had documented a Control table on similar ... 2)In the Package: Initiate two DateTime variables, ...
    (microsoft.public.sqlserver.dts)