How to check for valid filename

Post Reply
User avatar
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

Post by Otto »

Is there a function to check if a filename is valid?

Thanks in advance
Otto
User avatar
mmercado
Posts: 782
Joined: Wed Dec 19, 2007 7:50 am
Location: Salamanca, Gto., México

Re: How to check for valid filename

Post by mmercado »

Otto wrote:Is there a function to check if a filename is valid?
Hi Otto:

Try this:

Code: Select all | Expand

If File( "YourFile" )
   ?"it exist"
else
   ?"not exist"
endif

Regards.

Manuel Mercado
User avatar
Otto
Posts: 6403
Joined: Fri Oct 07, 2005 7:07 pm
Has thanked: 24 times
Been thanked: 2 times
Contact:

Post by Otto »

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 = ['<', '>', '|', '"', '\', '/', ':', '*', '?'];
User avatar
nageswaragunupudi
Posts: 10721
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 8 times
Contact:

Post by nageswaragunupudi »

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.
Regards

G. N. Rao.
Hyderabad, India
User avatar
Otto
Posts: 6403
Joined: Fri Oct 07, 2005 7:07 pm
Has thanked: 24 times
Been thanked: 2 times
Contact:

Post by Otto »

Thank you.
Regards,
Otto
User avatar
Otto
Posts: 6403
Joined: Fri Oct 07, 2005 7:07 pm
Has thanked: 24 times
Been thanked: 2 times
Contact:

Post by 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
User avatar
nageswaragunupudi
Posts: 10721
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 8 times
Contact:

Post by nageswaragunupudi »

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.
Regards

G. N. Rao.
Hyderabad, India
User avatar
Armando
Posts: 3271
Joined: Fri Oct 07, 2005 8:20 pm
Location: Toluca, México
Been thanked: 2 times
Contact:

Post by Armando »

Otto:

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
User avatar
driessen
Posts: 1422
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Post by driessen »

Otto,

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
User avatar
xProgrammer
Posts: 464
Joined: Tue May 16, 2006 7:47 am
Location: Australia

Post by xProgrammer »

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.
User avatar
James Bott
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Post by James Bott »

I think if you use the file save as common control, then filename checking is handled by Windows.

See FWH's cGetFile() function.

James
User avatar
nageswaragunupudi
Posts: 10721
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 8 times
Contact:

Post by nageswaragunupudi »

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
Post Reply