Is there a function to check if a filename is valid?
Thanks in advance
Otto
Is there a function to check if a filename is valid?
Thanks in advance
Otto
Otto wrote:Is there a function to check if a filename is valid?Hi Otto:
If File( "YourFile" )
?"it exist"
else
?"not exist"
endifThank you Manuel. I meant not if the file exists but if the name is valid, i.e. that only supported characters ( *,. Etc.) are included.
Regards,
Otto
const
{ for short 8.3 file names }
ShortForbiddenChars : set of Char = [';', '=', '+', '<', '>', '|',
'"', '[', ']', '\', '/', ''''];
{ for long file names }
LongForbiddenChars : set of Char = ['<', '>', '|', '"', '\', '/', ':', '*', '?'];
function FileValid( <testname> ) --> .t. or .f.
This was a function in CA-Tools in the old DOS Clipper times. XHarbour implemented this with some extensions. Should be available in Harbour too.
Thank you.
Regards,
Otto
Hello NageswaraRao,
I tried with xHarbour but I get an unresoved external errror.
Could you please tell me which lib I have to link.
Thanks in advance
Otto
Please include ct.lib
This is the lib that contains all old CA-Tools functions. ( Many of them are not needed in the present systems and for windows )
There must be a lib with similar name in Harbour too.
// The example implements the function LongFileValid() which tests
// if a long file name is valid
PROCEDURE Main
? FileValid( "MyApp.prg" ) // result: .T.
? FileValid( "My New Application.prg" ) // result: .F.
? LongFileValid( "My New Application.prg" ) // result: .T.
RETURN
FUNCTION LongFileValid( cFileName, lExtension )
IF Valtype( lExtension ) <> "L"
lExtension := .F.
ENDIF
RETURN FileValid( cFileName, 255, 255, lExtension, .T. )FUNCTION CheckFileName(cName)
LOCAL CharNo := {",","=","+","<",">","|","[","]","\","/"}
LOCAL cResult := .T.
FOR i=1 TO LEN(cName)
IF AT(CharNo[i],cName) <> 0
cResult := .F.
i := LEN(cName)
ENDIF
NEXT
MsgInfo("Filename is " + IF(cResult,"","not ") + "valid")
RESULT(cResult)Regards,
Michel D.
Genk (Belgium)
I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773
Hi all
Just a note of warning about checking for the validity of file names based on testing for forbidden characters. Remember that xBase strings can contain any character. CHR(0) through CHR(31) are not allowed in DOS file names. Also certain names are reserved for devices: CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. Microsoft warns against using these names even with extensions. These restrictions might not be relevant normally but you should be aware that many of the functions that check for the validity of a filename are not 100% robust because they don't check for CHR(0) through CHR(31) nor do they check for reserved device names.
You might like to check out http://msdn.microsoft.com/en-us/library/aa365247.aspx
Please note that Linux/Unix is much less restrictive. The only characters I know to be forbidden are / (obviously because it separates directory names and the filename) and NUL. No drive names are required so : can be used. In Linux a leading . denotes a hidden file.
I think if you use the file save as common control, then filename checking is handled by Windows.
See FWH's cGetFile() function.
James
FileValid( <cFileName> , ;
[<nMaxName>] , ; // default 8. Assign 255 for long file names
[<nMaxExtension>], ; // default 3. Assging 255 for lfn
[<lNoExtension>] , ; // better retain the default .f.
[<lSpaces>] ) // default .f. . Assign .t. for long file names