[windows] IsNameValid

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

[windows] IsNameValid

Post by BackupUser »

Code updated for 5.20+
Restored from previous forum. Originally posted by PB.

Code: Select all

; IsNameValid procedure by PB -- do what you want with it.
; Checks if a file or folder name contains a valid DOS name.
; Useful for when a user supplies a name for a file operation.
; Usage: r=IsNameValid(name$,msg)
; Returns 0 for invalid or 1 for valid.
; If msg=1 then an error message is shown (if invalid).
;
Procedure IsNameValid(name$,msg)
  name$=GetFilePart(name$)
  r=r+FindString(name$,Chr(34),1)
  r=r+FindString(name$,"\",1)
  r=r+FindString(name$,"/",1)
  r=r+FindString(name$,":",1)
  r=r+FindString(name$,"*",1)
  r=r+FindString(name$,"?",1)
  r=r+FindString(name$,"",1)
  r=r+FindString(name$,"|",1)
  If r=0
    ok=1
  Else
    If msg=1
      a$=   "A name cannot contain any of the following characters:"+Chr(13)
      a$=a$+"                 \ / : * ? "+Chr(34)+"  |"
      MessageBox_(0,a$,"Error",#MB_ICONERROR)
    EndIf
  EndIf
  ProcedureReturn ok
EndProcedure
;
Debug "C:\AutoExec.bat = "+Str(IsNameValid("C:\AutoExec.bat",0))
Debug "C:\AutoExec.ba* = "+Str(IsNameValid("C:\AutoExec.ba*",0))
Debug "C:\AutoExec.ba* = "+Str(IsNameValid("C:\AutoExec.ba*",1))

PB - Registered PureBasic Coder