Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How check for valid file name
Message
From
20/11/2000 07:05:50
 
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00443211
Message ID:
00443574
Views:
21
>>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
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform