Sign in to follow this  
Followers 0
stuppie

Legal Path Name

9 posts in this topic

#1 ·  Posted (edited)

Is there a quick way to check if an inputted string is a legal file/path name ? As in, does not contain slashes, asterisks, question marks, etc, but still can contain dashes and underscores (so, isAlphaNumeric() won't work).

Edited by stuppie

Share this post


Link to post
Share on other sites

Search the forums a bit, I'm sure someone has posted an applicable StringRegExp() pattern for that.

:mellow:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

I did search, but I didn't find anything because I looked for "legal" path name instead of "valid".

So here it is as a reference for anyone else who is looking for this:

http://www.autoitscript.com/forum/index.php?showtopic=113403

Share this post


Link to post
Share on other sites

I did search, but I didn't find anything because I looked for "legal" path name instead of "valid".

So here it is as a reference for anyone else who is looking for this:

http://www.autoitscript.com/forum/index.php?showtopic=113403

Looks like that first function would consider a UNC path to be invalid. I'm also not sure if that is a cross platform from NTFS and FAT systems.

Anyway, If I were going to personally write a func for it, I'd be sure to read the "File and Directory Names" part of this link: http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx

And of course, as Psalty said, create a regex patttern for it based on the link info.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Is there a quick way to check if an inputted string is a legal file/path name ? As in, does not contain slashes, asterisks, question marks, etc, but still can contain dashes and underscores (so, isAlphaNumeric() won't work).

Hope this helps.

$String = 'C:\Users\SomeUser\SomeFolder\Some_File.txt' ;Full path.
$File = StringSplit($String,"\",1)
$File = $File[UBound($File) - 1] ;File Name.
Msgbox(0,"Result:" , StringRegExp($String, '[:"?*\/<>|]', ""))
Msgbox(0,"Result:" , StringRegExp($File , '[:"?*\/<>|]', ""))

Share this post


Link to post
Share on other sites

Slashes are perfectly valid in paths under Windows and are equivalent to backslashes for Windows programs with some exceptions. See the MSDN link given above.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

This should be close but it must have a path and file name, not just a path.

$sPath = @DocumentsDir & "\Some folder\somefile.txt"
$bValidPath = False
If StringRegExp($sPath, "^([a-zA-Z]\:|\\\\[^\/\\:*?"<>|]+\\[^\/\\:*?"<>|]+)(\\[^\/\\:*?"<>|]+)+(\.[^\/\\:*?"<>|]+)$") Then $bValidPath = True

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

This should be close but it must have a path and file name, not just a path.

$sPath = @DocumentsDir & "\Some folder\somefile.txt"
$bValidPath = False
If StringRegExp($sPath, "^([a-zA-Z]\:|\\\\[^\/\\:*?"<>|]+\\[^\/\\:*?"<>|]+)(\\[^\/\\:*?"<>|]+)+(\.[^\/\\:*?"<>|]+)$") Then $bValidPath = True

As jchd said previously, I can see that failing on certain volume types ( although I did see that regex type online somewhere before ).

If we follow the msdn link I provided several beers ago... I could fathom something like the following working:

Func _PathIsValid($s_path, $f_isdir = False, $f_usedin_IO = False)

    If StringLeft($s_path, 2) <> "\\" Then
        If FileExists($s_path) Then Return True
    EndIf

    ; for possible relative path
    $s_path = _PathFull($s_path)

    Local $s_drive, $s_dir, $s_fname, $s_ext
    _PathSplit($s_path, $s_drive, $s_dir, $s_fname, $s_ext)

    Local $f_unc = False
    If StringLeft($s_drive, 2) = "\\" Then $f_unc = True

    If IsKeyword($f_usedin_IO) Or $f_usedin_IO = -1 Then $f_usedin_IO = False

    ; validate this is not an I/O verification and not unc
    If Not $f_unc And $f_usedin_IO Then
        ; I/O API's convert "/" to "\"
        $s_path = StringReplace($s_path, "/", "\")
        If FileExists($s_path) Then Return True
    EndIf

    If IsKeyword($f_isdir) Or $f_isdir = -1 Then $f_isdir = False

    ; Concat dir and fname (and ext) if this is supposed to be a dir
    If Not $s_ext And $f_isdir Then
        $s_dir &= $s_fname & $s_ext
        $s_fname = ""
        $s_ext = ""
    EndIf

    ; If the drive doesn't exist, then it is not a valid path
    If Not $f_unc Then
        If Not FileExists($s_drive) Then Return False
    EndIf

    Local $s_fsystem = DriveGetFileSystem($s_drive)
    If @error Then Return False

    Local $iPATH_MAX = 255
    Local $i_dirlen = StringLen($s_dir)

    ; 12 chars must remain for filename and ext (optional)
    If ($i_dirlen > ($iPATH_MAX - 12)) Then Return False

    Switch $s_fsystem
        Case "UDF"
            If $i_dirlen > ($iPATH_MAX - 1) Then Return False
        Case Else
            ; Only found NTFS, exFAT, UDF, and FAT32 with options for this
            If $i_dirlen > $iPATH_MAX Then Return False
    EndSwitch

    Return (StringRegExp($s_dir & $s_fname & $s_ext, '[<>:"/\|\?\*]') == 0)
EndFunc

Although I did not test UNC very much at all. Anyone is more than welcome to test it and or change the above code to be more appropriate.

Edited by SmOke_N
made a change on my end

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

I was actually looking for the one you and I worked out one night where we also checked the lenght of the path, I'm just not sure where I filed it at the moment. It definitly hasn't been added to a library yet.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0