CheckPath()
Posted: Sun Apr 05, 2009 11:22 pm
since there is a CheckFilename(), it would be cool to have a CheckPath() aswell.
c ya,
nco2k
c ya,
nco2k
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" )
You would treat my harddisk as a necronomicon. XDTranquil wrote:regular expressions are so evil....
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.)nco2k wrote:since there is a CheckFilename(), it would be cool to have a CheckPath() aswell.
c ya,
nco2k
BUT If you use CheckFilename() to test C:\MyCompany\MyProgram\ which IS a valid fully qualified path, you will get a zero result.Psychophanta wrote: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.)nco2k wrote:since there is a CheckFilename(), it would be cool to have a CheckPath() aswell.
c ya,
nco2k
So then you can easely compose a quite simple function to detect whether a complete path name is valid or not.
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 "==================================="
Shouldn't "c:" be valid?Mohawk70 wrote:If you find a valid path that returns as invalid or an invalid
path that returns as valid !