in reply to Re: How to validate windows path
in thread How to validate windows path

No, my program get path from the user as parameter and need to check if the path is valid - if yes then create the folder.

Replies are listed 'Best First'.
Re^3: How to validate windows path
by MidLifeXis (Monsignor) on Mar 13, 2012 at 12:19 UTC

    Define 'valid'. One approach would be to 'try it and see'. Attempt the create, if that succeeds, check to verify the directory exists1. In this case, 'valid' means being able to be created.

    If you define what you mean by 'valid', it may help with the responses.

    Footnotes:

    1. 1 - Winding back if it doesn't exist might be fun...

    Updates:

    1. +10 minutes - Update formatting and footnotes, and added a more generic response.

    --MidLifeXis

      I misunderstood the OP's question. In this case, I agree with MidLifeXis, try create a dir.
      A tool you can use can be Win32::CreateDirectory, check the return value and message the user afterward
      I didn't successes to use the following regular expressions
      my $dir = "c:\\temp\\%1111 323232?"; if ($dir =~ m/^[a-z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$/i) { print "Valid Path\n"; } else { print "Not Valid Path\n"; }
      Please advise - as i wrote i get Path from the user and i need to print error if isn't valid path.

        A regular expression to check whether a path is valid or not would be quite a substantial piece of work. For example...

        C:\BON - a valid path C:\CON - not valid

        (Aside: "CON" is a special device representing the console itself, so files and directories are not allowed to be called "CON". There are other devices with similar effect. Unixey operating systems have a similar feature, however absolute paths were always used, so while you cannot name a file "/dev/stdin" in Unix, you can create "stdin" files in other directories.)

        Do what everyone else has said - just go ahead and try to create the directory, then check that it exists (i.e. that creation worked).

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

        What did you get when you ran it? I received an error message that points out very well where I (well, the code) has an error in it.

        Unmatched [ in regex; marked by <-- HERE in m/^[a-z]:\\(?:[ <-- HERE ^\\/ at foo.pl line 3.

        You need to escape the 'end of regexp' marker to move beyond this.

        --MidLifeXis