CheckPath()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

CheckPath()

Post by nco2k »

since there is a CheckFilename(), it would be cool to have a CheckPath() aswell.

c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Post 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.
pb 5.11
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Post 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
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post 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" )
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post 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.
Tranquil
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post by Hroudtwolf »

Tranquil wrote:regular expressions are so evil....
You would treat my harddisk as a necronomicon. XD
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: CheckPath()

Post 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.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Mohawk70
Enthusiast
Enthusiast
Posts: 404
Joined: Thu May 11, 2006 1:04 am
Location: Florida, USA

Re: CheckPath()

Post 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 !
HP Z800 Workstation
CPU : Dual Xeon 5690 3.46GHz
RAM : 96GB RAM ( 8GB x 12 )
PSU : 1100W
GPU : NVIDIA RTX 3050 8GB
STORAGE : 9TB
(4) 2TB Seagate IronWolf Pro HDD
(1) 1TB Samsung 870 EVO SSD
Mohawk70
Enthusiast
Enthusiast
Posts: 404
Joined: Thu May 11, 2006 1:04 am
Location: Florida, USA

Re: CheckPath()

Post 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 "==================================="
HP Z800 Workstation
CPU : Dual Xeon 5690 3.46GHz
RAM : 96GB RAM ( 8GB x 12 )
PSU : 1100W
GPU : NVIDIA RTX 3050 8GB
STORAGE : 9TB
(4) 2TB Seagate IronWolf Pro HDD
(1) 1TB Samsung 870 EVO SSD
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: CheckPath()

Post 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:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Post Reply