in reply to Re: Regular expression to check for invalid file name characters
in thread Regular expression to check for invalid file name characters

Why not just let the OS tell you if it's invalid?

How would you do that reliably?

If you tried creating a file, it might fail because it already existed. If it succeeds, then you'd have to delete it again--assuming the idea is to just check, not actually use. And if it contained a backslash, you might end up creating a file in a subdirectory. You can't stat unless it already exists.

So what call would you use?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"I'd rather go naked than blow up my ass"
  • Comment on Re^2: Regular expression to check for invalid file name characters

Replies are listed 'Best First'.
Re^3: Regular expression to check for invalid file name characters
by ikegami (Patriarch) on Feb 22, 2010 at 16:24 UTC

    If it succeeds, then you'd have to delete it again--assuming the idea is to just check, not actually use.

    I assumed the opposite. When we get questions like this, the check is followed by actually opening or creating the file.

    How would you do that reliably?

    Errno provides codes against which you can check $!.

      I assumed the opposite.

      Even so, you could still succeed in creating a file within a subdirectory by accident.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        I was trying to make the application as user friendly as possible. Therefore I thought it worthwhile checking that all the characters supplied could be used (rather than getting a system error message which may not be that good to work with).
        Also, I should have said that I am working on Windows - hence the list of invalid characters I gave in the subroutine.