FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour How to check for valid filename
Posts: 6984
Joined: Fri Oct 07, 2005 07:07 PM
How to check for valid filename
Posted: Wed Aug 20, 2008 02:08 PM

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

Thanks in advance
Otto

Posts: 782
Joined: Wed Dec 19, 2007 07:50 AM
Re: How to check for valid filename
Posted: Wed Aug 20, 2008 02:26 PM
Otto wrote:Is there a function to check if a filename is valid?
Hi Otto:

Try this:

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

Regards.

Manuel Mercado
manuelmercado at prodigy dot net dot mx
Posts: 6984
Joined: Fri Oct 07, 2005 07:07 PM
How to check for valid filename
Posted: Wed Aug 20, 2008 03:22 PM

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 = ['<', '>', '|', '"', '\', '/', ':', '*', '?'];

Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
How to check for valid filename
Posted: Wed Aug 20, 2008 03:30 PM

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
Posts: 6984
Joined: Fri Oct 07, 2005 07:07 PM
How to check for valid filename
Posted: Wed Aug 20, 2008 04:02 PM

Thank you.
Regards,
Otto

Posts: 6984
Joined: Fri Oct 07, 2005 07:07 PM
How to check for valid filename
Posted: Wed Aug 20, 2008 04:14 PM

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

Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
How to check for valid filename
Posted: Wed Aug 20, 2008 04:18 PM

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
Posts: 3358
Joined: Fri Oct 07, 2005 08:20 PM
How to check for valid filename
Posted: Wed Aug 20, 2008 11:58 PM
Otto:

Perhaps this can help you

// 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
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
How to check for valid filename
Posted: Thu Aug 21, 2008 12:24 AM
Otto,

I use this function :

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. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 464
Joined: Tue May 16, 2006 07:47 AM
How to check for valid filename
Posted: Thu Aug 21, 2008 01:30 AM

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.

Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
How to check for valid filename
Posted: Thu Aug 21, 2008 03:33 AM

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

See FWH's cGetFile() function.

James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
How to check for valid filename
Posted: Thu Aug 21, 2008 04:04 AM
Syntax of FileValid function in xHarbour:

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

Continue the discussion