Page 1 of 1

CheckPath()

Posted: Sun Apr 05, 2009 11:22 pm
by nco2k
since there is a CheckFilename(), it would be cool to have a CheckPath() aswell.

c ya,
nco2k

Posted: Tue Apr 07, 2009 6:40 am
by gnasen
If I remember right, FileSize() returns -1 or -2 (see help) if it "hits" a directory.

Maybe an own command would be more clear to have an own command, but its not necessary.

Posted: Tue Apr 07, 2009 6:56 am
by nco2k
im talking about a command, to check if the specified path$ contains invalid characters. the counterpart to -> CheckFileName(). i should have called it CheckPathName() or something like that, from the start. :)

c ya,
nco2k

Posted: Tue Apr 07, 2009 7:44 am
by Hroudtwolf
Hi,

As long as this feature isn't available in PureBasic, you could use this little function.

Best regards

Wolf

PS: Feel free to enhance it if you need.

Code: Select all

; Hroudtwolf
; 04/07/2009
; PureBasic 4.x
; Windows, Linux, OS X

Procedure CheckPath ( sPath.s )
   Protected idRegEx .i = CreateRegularExpression ( #PB_Any , "^[[:alnum:]]{1,}[:]{1}(\\{1}[äöüÄÖÜ[:alnum:][:space:]&$§!()}{~#.,´'@€]+)*\\?$" )
   Protected blResult.i = MatchRegularExpression ( idRegEx , sPath )
  
   FreeRegularExpression ( idRegEx )
   
   ProcedureReturn blResult
EndProcedure

; Good
Debug CheckPath ( "c:\windows\system32" )
Debug CheckPath ( "c:\documents\Ölplattformen in der Nordsee" )
Debug CheckPath ( "c:\documents\Ölplattformen in der Nordsee\Faunaschäden" )
Debug CheckPath ( "c:\documents\test@test" )

; Bad
Debug CheckPath ( "c:\documents\test|test" )
Debug CheckPath ( "c:\documents\your money bag is < than mine" )

Posted: Tue Apr 07, 2009 1:40 pm
by Tranquil
regular expressions are so evil. Dont have a clue how to build them.
Anyway, its not possible to check for an UNC Path with that expression.

Posted: Tue Apr 07, 2009 2:06 pm
by Hroudtwolf
Tranquil wrote:regular expressions are so evil....
You would treat my harddisk as a necronomicon. XD

Re: CheckPath()

Posted: Wed Apr 08, 2009 10:40 am
by Psychophanta
nco2k wrote:since there is a CheckFilename(), it would be cool to have a CheckPath() aswell.

c ya,
nco2k
At least in windows, what is valid for a file name is also valid for a folder name. And folders are treated as files internally by FAT32 and others. (This is not the same in amiga for example, where you can have a folder with the same exact name as a file located into the same path.)
So then you can easely compose a quite simple function to detect whether a complete path name is valid or not.

Re: CheckPath()

Posted: Mon Dec 14, 2009 11:47 pm
by Mohawk70
Psychophanta wrote:
nco2k wrote:since there is a CheckFilename(), it would be cool to have a CheckPath() aswell.

c ya,
nco2k
At least in windows, what is valid for a file name is also valid for a folder name. And folders are treated as files internally by FAT32 and others. (This is not the same in amiga for example, where you can have a folder with the same exact name as a file located into the same path.)
So then you can easely compose a quite simple function to detect whether a complete path name is valid or not.
BUT If you use CheckFilename() to test C:\MyCompany\MyProgram\ which IS a valid fully qualified path, you will get a zero result.
This is because \ is NOT a valid character in a file name. Test it with PureBasic 4.40 and see for yourself !

Re: CheckPath()

Posted: Tue Dec 15, 2009 1:12 am
by Mohawk70
Here's something I knocked out real quick. Seems to work perfectly. Anybody who tests this let me know
if you get correct results or not by replying. If you find a valid path that returns as invalid or an invalid
path that returns as valid !

Code: Select all

Enabledebugger
Procedure.l CheckPathName(in.s)
  index = 1
  lOut.l = #True
  Drive$ = StringField(in,index,"\")
  p = FindString("ABCDEFGHIJKLMNOPQRSTUVWXYZ",UCase(Left(in,1)),1)
  If p <> 0 And Len(in)>2 And Mid(in,2,2) = ":\"
    x$  = Drive$ + "\"
    Repeat
      index + 1
      folder$ = StringField(in,index,"\")
      If folder$ <> ""
        r = CheckFilename(folder$)
        If r = 0
          lOut.l = #False
        EndIf
      EndIf
    Until folder$=""
  Else
    lOut.l = #False
  EndIf
  ProcedureReturn lOut.l
EndProcedure

Procedure IsPathNameOk(path.s)
  r = CheckPathName(path)
  Select r
    Case #False
      Debug "INVALID"
    Case #True
      Debug "VALID"
  EndSelect
  ProcedureReturn
EndProcedure

;Test Code
path.s = "C:\Program Files\My_Company\My Application\"
Debug path.s
IsPathNameOk(path.s)
Debug "==================================="

path.s = "C:\Program Files\My_Company\{My.Application}\"
Debug path.s
IsPathNameOk(path.s)
Debug "==================================="

path.s = "C:\Program Files\My_Company\My_Application\"
Debug path.s
IsPathNameOk(path.s)
Debug "==================================="

path.s = "C:\Program Files\My_Company\My?Application\"
Debug path.s
IsPathNameOk(path.s)
Debug "==================================="

path.s = "C:\|rogram Files\My_Company\My*Application\"
Debug path.s
IsPathNameOk(path.s)
Debug "==================================="

path.s = "C:\Program Files\My_Company\My|Application\"
Debug path.s
IsPathNameOk(path.s)
Debug "==================================="

path.s = "C:\Program Files\My_Company\My<Application\"
Debug path.s
IsPathNameOk(path.s)
Debug "==================================="

path.s = "C:\Program Files\My_Company\My>Application\"
Debug path.s
IsPathNameOk(path.s)
Debug "==================================="

path.s = "C:\Program Files\My_Company\My" + Chr(34) + "Application\"
Debug path.s
IsPathNameOk(path.s)
Debug "==================================="

Re: CheckPath()

Posted: Tue Dec 15, 2009 10:14 pm
by Psychophanta
Mohawk70 wrote:If you find a valid path that returns as invalid or an invalid
path that returns as valid !
Shouldn't "c:" be valid?
And also relative paths like:
"\..", "\.", "..", "."
:roll: