Outaspace has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I just wondered if there is a way to check if a path is correct (on current Operating Systems). The path dont has to be exist, but only if the spelling is correct.

Example:

c:\winmoff\folderforvirus32 => is correct path (on Win Sys, but not under Unixes)
c:\winmoff\\folderforvirus32 => is incorrect (everyware, I think)

Andre
  • Comment on Is there a way for checking if a Path is correct

Replies are listed 'Best First'.
Re: Is there a way for checking if a Path is correct
by Perl Mouse (Chaplain) on Nov 23, 2005 at 13:43 UTC
    c:\winmoff\folderforvirus32 => is correct path (on Win Sys, but not under Unixes)
    c:\winmoff\\folderforvirus32 => is incorrect (everyware, I think)
    $ mkdir /tmp/test $ cd /tmp/test $ mkdir 'c:\winmoff\folderforvirus32' $ mkdir 'c:\winmoff\\folderforvirus32' $ ls -1 c:\winmoff\\folderforvirus32 c:\winmoff\folderforvirus32 $ uname Linux $
    Testing whether a path is valid under Unix is easy: as long as it doesn't contain "\x00" (NUL character in C) it's a valid path. Any other character is valid, and only / is special: it separates the path components. Hence, in a component (file- or directoryname), all characters, except the NUL character and the slash are allowed.
    Perl --((8:>*
Re: Is there a way for checking if a Path is correct
by svenXY (Deacon) on Nov 23, 2005 at 13:47 UTC
    Hi,
    you probably mean if a path is syntactically correct. Well, that's sometimes hard to tell as on Windows you have the backslash as a separator and certain other characters are not allowed whereas under Unix you use the forward slash (aka slash ;-)) as the delimiter while at the same time the backslash is a widely used character used to mask the next character (e.g. a space).

    This leaves you with the problem of how to know if a backslash is used to mask the next character or if it is a Windows path.
    But there are ways to translate Windows paths to Unix and vv.
    Regards,
    svenXY
      The backslash is only used to prevent the shell from processing special characters. There's no reason to deal with backslashes yourself - if you write a regex, you aren't dealing with any backslashes that might be in the query string as well, are you?
      Perl --((8:>*
Re: Is there a way for checking if a Path is correct
by Moron (Curate) on Nov 23, 2005 at 13:52 UTC
    Just look at the bottom left of your screen. As long as there's no windows symbol, the spelling is correct. Otherwise, who cares ;)

    Update: Actually I am lying - the spelling can also be incorrect on other legacy systems than windows, such as VMS, OS400 etc.

    More to the point, Perl will translate unix/linux paths in filename context into whatever is correct for the actual platform.

    -M

    Free your mind