How to check for valid filename
- Otto
- Posts: 6403
- Joined: Fri Oct 07, 2005 7:07 pm
- Has thanked: 24 times
- Been thanked: 2 times
- Contact:
How to check for valid filename
Is there a function to check if a filename is valid?
Thanks in advance
Otto
Thanks in advance
Otto
Re: How to check for valid filename
Hi Otto:Otto wrote:Is there a function to check if a filename is valid?
Try this:
Code: Select all | Expand
If File( "YourFile" )
?"it exist"
else
?"not exist"
endif
Regards.
Manuel Mercado
- Otto
- Posts: 6403
- Joined: Fri Oct 07, 2005 7:07 pm
- Has thanked: 24 times
- Been thanked: 2 times
- Contact:
Thank 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 = ['<', '>', '|', '"', '\', '/', ':', '*', '?'];
Regards,
Otto
const
{ for short 8.3 file names }
ShortForbiddenChars : set of Char = [';', '=', '+', '<', '>', '|',
'"', '[', ']', '\', '/', ''''];
{ for long file names }
LongForbiddenChars : set of Char = ['<', '>', '|', '"', '\', '/', ':', '*', '?'];
- nageswaragunupudi
- Posts: 10721
- Joined: Sun Nov 19, 2006 5:22 am
- Location: India
- Been thanked: 8 times
- Contact:
- nageswaragunupudi
- Posts: 10721
- Joined: Sun Nov 19, 2006 5:22 am
- Location: India
- Been thanked: 8 times
- Contact:
- Armando
- Posts: 3271
- Joined: Fri Oct 07, 2005 8:20 pm
- Location: Toluca, México
- Been thanked: 2 times
- Contact:
Otto:
Perhaps this can help you
It's from xHarbour Doc
Best regards
Perhaps this can help you
Code: Select all | Expand
// 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. )
It's from xHarbour Doc
Best regards
SOI, s.a. de c.v.
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero
Otto,
I use this function :
Just add the characters which are not allowed to the array.
In case of shortname, you can add a test to check the length of the filename.
Good luck.
Michel
I use this function :
Code: Select all | Expand
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)
Just add the characters which are not allowed to the array.
In case of shortname, you can add a test to check the length of the filename.
Good luck.
Michel
Regards,
Michel D.
Genk (Belgium)
_____________________________________________________________________________________________
I use : FiveWin for (x)Harbour v. 24.09 - Harbour 3.2.0 (February 2024) - xHarbour Builder (January 2020) - Bcc773
Michel D.
Genk (Belgium)
_____________________________________________________________________________________________
I use : FiveWin for (x)Harbour v. 24.09 - Harbour 3.2.0 (February 2024) - xHarbour Builder (January 2020) - Bcc773
- xProgrammer
- Posts: 464
- Joined: Tue May 16, 2006 7:47 am
- Location: Australia
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.
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.
- James Bott
- Posts: 4840
- Joined: Fri Nov 18, 2005 4:52 pm
- Location: San Diego, California, USA
- Contact:
- nageswaragunupudi
- Posts: 10721
- Joined: Sun Nov 19, 2006 5:22 am
- Location: India
- Been thanked: 8 times
- Contact:
Syntax of FileValid function in xHarbour:
Code: Select all | Expand
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
Regards
G. N. Rao.
Hyderabad, India
G. N. Rao.
Hyderabad, India