How to check for valid filename

How to check for valid filename

Postby Otto » Wed Aug 20, 2008 2:08 pm

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

Thanks in advance
Otto
User avatar
Otto
 
Posts: 6346
Joined: Fri Oct 07, 2005 7:07 pm

Re: How to check for valid filename

Postby mmercado » Wed Aug 20, 2008 2:26 pm

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

Try this:

Code: Select all  Expand view  RUN
If File( "YourFile" )
   ?"it exist"
else
   ?"not exist"
endif

Regards.

Manuel Mercado
User avatar
mmercado
 
Posts: 782
Joined: Wed Dec 19, 2007 7:50 am
Location: Salamanca, Gto., México

Postby Otto » Wed Aug 20, 2008 3: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 = ['<', '>', '|', '"', '\', '/', ':', '*', '?'];
User avatar
Otto
 
Posts: 6346
Joined: Fri Oct 07, 2005 7:07 pm

Postby nageswaragunupudi » Wed Aug 20, 2008 3: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
User avatar
nageswaragunupudi
 
Posts: 10660
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Postby Otto » Wed Aug 20, 2008 4:02 pm

Thank you.
Regards,
Otto
User avatar
Otto
 
Posts: 6346
Joined: Fri Oct 07, 2005 7:07 pm

Postby Otto » Wed Aug 20, 2008 4: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
User avatar
Otto
 
Posts: 6346
Joined: Fri Oct 07, 2005 7:07 pm

Postby nageswaragunupudi » Wed Aug 20, 2008 4: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
User avatar
nageswaragunupudi
 
Posts: 10660
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Postby Armando » Wed Aug 20, 2008 11:58 pm

Otto:

Perhaps this can help you

Code: Select all  Expand view  RUN
// 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
Armando
 
Posts: 3231
Joined: Fri Oct 07, 2005 8:20 pm
Location: Toluca, México

Postby driessen » Thu Aug 21, 2008 12:24 am

Otto,

I use this function :

Code: Select all  Expand view  RUN
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.07 - Harbour 3.2.0 (February 2024) - xHarbour Builder (January 2020) - Bcc773
User avatar
driessen
 
Posts: 1422
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Postby xProgrammer » Thu Aug 21, 2008 1: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.
User avatar
xProgrammer
 
Posts: 464
Joined: Tue May 16, 2006 7:47 am
Location: Australia

Postby James Bott » Thu Aug 21, 2008 3: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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Postby nageswaragunupudi » Thu Aug 21, 2008 4:04 am

Syntax of FileValid function in xHarbour:

Code: Select all  Expand view  RUN
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
User avatar
nageswaragunupudi
 
Posts: 10660
Joined: Sun Nov 19, 2006 5:22 am
Location: India


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 35 guests