Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How check for valid file name
Message
De
20/11/2000 07:05:50
 
 
À
20/11/2000 00:55:16
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00443211
Message ID:
00443574
Vues:
16
>>How to tell if they entered a valid filename? What are the limitations? Only alpha and numeric? They are entering a file name in a textbox (the file may not exist) and I want to check it for being valid.
>
>That's going to depend on the file system you're writing to; an older DOS or NetWare volume may only accept old DOS-style 8.3 file names, Win32 files systems accept file names with much greater flexibility, and other platforms vary in their rules.
>
>A trick I use to check for file name legality is to create a unique, temporary directory name beneath the target directory, and try to FCREATE() the file in the temporary directory:
>
>
FUNCTION TestFileName(tcFileNameToTest, tcTargetPath)
>LOCAL cTempDir, nFH, cOldErrorHandler
>IF TYPE(tcFileNameToTest) # 'C'
>   RETURN .F.
>ENDIF
>cTempDir = ADDBS(FULLPATH(IIF(TYPE(tcTargetPath) # 'C' OR LEN(tcTargetPath) = 0,'.',tcTargetPath))+SYS(3)
>DO WHILE DIRECTORY(cTempDir)
>   cTempDir = LEFT(cTempDir,RAT('\',cTempDir))+SYS(3)
>ENDDO
>cOldErrorHandler = ON('ERROR')
>nFH = 0
>ON ERROR nFH = -1
>MKDIR (cTempDir)
>IF nFH = 0
>   nFH = FCREATE(cTempDir+'\'+cFileNameToTest)
>   IF nFH > 0
>      *  OK
>      =FCLOSE(nFH)
>      ERASE (cTempDir+'\'+cFileNameToTest)
>   ENDIF
>   RMDIR (cTempDir)
>ENDIF
>ON ERROR &cOldErrorHandler
>RETURN nFH > 0
>
>>
>>Thanks
>>
>>Brenda

Once you've sorted out the deliberate errors in the above code, beware that it does no check for a path in the filename, this could be a particular problem if someone typed in something like "..\filename.ext", where filename.ext exists in the current directory - the fcreate followed by erase would ensure that it was deleted & let you know the filename was valid.
Mike

"I can live with doubt and uncertainty and not knowing. I think it is much more interesting to live not knowing than to have answers that might be wrong." - Richard Feynman
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform