File name verfication
#5
(03-15-2023, 05:37 AM)SMcNeill Wrote: INPUT "Give me a file name =>"; file$
IF _FILEEXISTS(file$) THEN 
   PRINT "Found that file!"
ELSE
    PRINT "File doesn't exist!"
END IF

*********************

If you're looking for a way to check to see if a name is valid, for saving purposes rather than loading, that's something you'd have to write yourself as per the ruleset of what's acceptable for you OS.  (Windows is much pickier, for example, than Linux when it comes to valid file name characters.)

I take you up on your challenge and came up with this. Anyone is free to use said code! I tried to make it customizable for others as wll.

Code: (Select All)
OPTION _EXPLICIT

CONST TRUE = -1, FALSE = NOT TRUE

DIM Try$

DO
    CLS
    INPUT "Enter a filename to check (enter quit or exit to exit): "; Try$
    IF UCASE$(Try$) = "QUIT" OR UCASE$(Try$) = "EXIT" THEN SYSTEM
    IF ISVALID(Try$) THEN PRINT CHR$(34) + Try$ + CHR$(34) + " is a vaild filename." ELSE PRINT CHR$(34) + Try$ + CHR$(34) + " is invalid as a filename."
    PRINT "Press any key to countinue..."
    SLEEP
    _DELAY .1
    _KEYCLEAR
LOOP


'This fuction returns true if vaild and false if not (as a bit)
FUNCTION ISVALID` (FileName AS STRING)

    DIM AS STRING Legal(1 TO 50), FileNameCanidate
    DIM AS STRING FileNameParts(1 TO 96)
    DIM AS INTEGER LoopCounter, LoopCounter1, NumberOfVaildCharacters

    FileNameCanidate = UCASE$(FileName)

    'Set the number of characters to check (easily add or remove desired characters)
    NumberOfVaildCharacters = 39

    'Define vaild characters (letters, numbers, spaces, and selected symbols only).
    Legal(1) = "A": Legal(2) = "B": Legal(3) = "C": Legal(4) = "D"
    Legal(5) = "E": Legal(6) = "F": Legal(7) = "G": Legal(8) = "H"
    Legal(9) = "I": Legal(10) = "J": Legal(11) = "K": Legal(12) = "L"
    Legal(13) = "M": Legal(14) = "N": Legal(15) = "O": Legal(16) = "P"
    Legal(17) = "Q": Legal(18) = "R": Legal(19) = "S": Legal(20) = "T"
    Legal(21) = "U": Legal(22) = "V": Legal(23) = "W": Legal(24) = "X"
    Legal(25) = "Y": Legal(26) = "Z": Legal(27) = " ": Legal(28) = "1"
    Legal(29) = "2": Legal(30) = "3": Legal(31) = "4": Legal(32) = "5"
    Legal(33) = "6": Legal(34) = "7": Legal(35) = "8": Legal(36) = "9"
    Legal(37) = "0": Legal(38) = "-": Legal(39) = "_"

        'All file names are limited to 100 (96 + 4 for .***) characters (paths are limited to 255)
    IF LEN(FileNameCanidate) >= 96 THEN ISVALID` = FALSE: EXIT FUNCTION

    'Filenames can not start or end with a space
    IF MID$(FileNameCanidate, 1, 1) = " " THEN ISVALID` = FALSE: EXIT FUNCTION
    IF MID$(FileNameCanidate, LEN(FileNameCanidate), 1) = " " THEN ISVALID` = FALSE: EXIT FUNCTION

    'Each part of the canidate name is pulled about to check.
    FOR LoopCounter = 1 TO LEN(FileNameCanidate)
        FileNameParts(LoopCounter) = MID$(FileNameCanidate, LoopCounter, 1)
    NEXT LoopCounter
   
    'Check the canidate vs the legal characters.
    'If a legal character is not found in a position, false is returned without further checking.
    FOR LoopCounter = 1 TO LEN(FileName)
        ISVALID` = FALSE 'Reset the tripwire
        FOR LoopCounter1 = 1 TO NumberOfVaildCharacters
            IF FileNameParts(LoopCounter) = Legal(LoopCounter1) THEN ISVALID` = TRUE: EXIT FOR
        NEXT LoopCounter1
        IF NOT ISVALID` THEN EXIT FUNCTION 'If not tripped to true, exit with further checking.
    NEXT LoopCounter

    'If all tests are passed then return true
    ISVALID` = TRUE
END FUNCTION

Instead of what's not vaild, I tried to capture what is vaild and also ensure our file meets whatever restrictions that is set. I also included a limit and spaces to avoid stranger file names.

(03-15-2023, 07:39 AM)RhoSigma Wrote: This is my verification function, feel free to use it and/or adapt to your needs:
Code: (Select All)
LINE INPUT "Filename: "; f$

IF ValidFilename%(f$) THEN
    PRINT "Filename is ok."
ELSE
    PRINT "Filename contains invalid chars."
END IF
END

FUNCTION ValidFilename% (file$)
'--- so far, assume invalid ---
ValidFilename% = 0
'--- now check all chars ---
FOR i% = 1 TO LEN(file$)
    SELECT CASE ASC(file$, i%)
        CASE 34, 42, 47, 60, 62, 63, 124
            'invalid chars, list above is for Windows,
            'may need adjustment for other OS
            EXIT FUNCTION
    END SELECT
NEXT i%
'--- check succesfully passed ---
ValidFilename% = -1
END FUNCTION

I like how short and simple this is! If I saw it before I made one, I might have just used this instead  Cool
Reply


Messages In This Thread
File name verfication - by NasaCow - 03-15-2023, 04:44 AM
RE: File name verfication - by SMcNeill - 03-15-2023, 05:37 AM
RE: File name verfication - by RhoSigma - 03-15-2023, 07:39 AM
RE: File name verfication - by mnrvovrfc - 03-15-2023, 12:15 PM
RE: File name verfication - by NasaCow - 03-16-2023, 03:57 AM
RE: File name verfication - by SpriggsySpriggs - 03-16-2023, 05:08 AM
RE: File name verfication - by mnrvovrfc - 03-16-2023, 03:37 PM
RE: File name verfication - by NasaCow - 03-17-2023, 02:29 AM
RE: File name verfication - by SMcNeill - 03-17-2023, 06:11 AM
RE: File name verfication - by Kernelpanic - 03-16-2023, 05:47 PM
RE: File name verfication - by mnrvovrfc - 03-16-2023, 06:17 PM
RE: File name verfication - by SpriggsySpriggs - 03-16-2023, 09:15 PM
RE: File name verfication - by SpriggsySpriggs - 03-17-2023, 04:58 AM
RE: File name verfication - by NasaCow - 03-18-2023, 02:56 AM
RE: File name verfication - by SpriggsySpriggs - 03-18-2023, 11:55 PM



Users browsing this thread: 3 Guest(s)