Question Marks In Filenames In QB64

231 views Asked by At

Now, I have always known the following characters are reserved and cannot be used in a filename:

/:*?<>|"

However, there seems to be a case where the ? character (ascii 063) is being reported in filenames that contain Unicode.

I am trying to use FindFirstFileA to get filenames in preparation to copying and renaming them but I don't know why the function returns ? instead of Unicode.

My question is: How do I process filenames with ? in them?

REM program displays filenames with question marks in them.
' declare library constants.
CONST MAX_PATH = 260 ' length of an ASCIIZ string
CONST INVALID_HANDLE_VALUE = -1 ' returned from a FindFirstFile
' declare library structures.
TYPE FILETIME
    dwLowDateTime AS _UNSIGNED LONG
    dwHighDateTime AS _UNSIGNED LONG
END TYPE
' windows structure for a FindFile
TYPE WIN32_FIND_DATAA
    dwFileAttributes AS _UNSIGNED LONG
    ftCreationTime AS FILETIME
    ftLastAccessTime AS FILETIME
    ftLastWriteTime AS FILETIME
    nFileSizeHigh AS _UNSIGNED LONG
    nFileSizeLow AS _UNSIGNED LONG
    dwReserved0 AS _UNSIGNED LONG
    dwReserved1 AS _UNSIGNED LONG
    cFileName AS STRING * MAX_PATH
    cAlternateFileName AS STRING * 14
END TYPE
' declare external libraries.
DECLARE DYNAMIC LIBRARY "kernel32"
    FUNCTION FindFirstFileA~%& (BYVAL lpFileName~%&, BYVAL lpFindFileData~%&)
    FUNCTION FindNextFileA& (BYVAL hFindFile~%&, BYVAL lpFindFileData~%&)
    FUNCTION FindClose& (BYVAL hFindFile~%&)
    FUNCTION GetLastError& ()
    FUNCTION SetCurrentDirectoryA% (f$)
END DECLARE
DIM Attribute AS INTEGER ' the attribute of a file
DIM ASCIIZ AS STRING * 260 ' a null terminated filename
DIM finddata AS WIN32_FIND_DATAA ' the windows filename stcucture
DIM Wfile.Handle AS _UNSIGNED _OFFSET ' windows file handle for FindFile
' force default path
x$ = _STARTDIR$
f$ = x$ + CHR$(0)
x = SetCurrentDirectoryA(f$)
' make filename
Var$ = "*.txt"
PRINT "Processing: "; Var$
ASCIIZ = Var$ + CHR$(0)
' start the search and store the returned windows file handle
Wfile.Handle = FindFirstFileA(_OFFSET(ASCIIZ), _OFFSET(finddata))
' check if the file handle is valid
IF Wfile.Handle <> INVALID_HANDLE_VALUE THEN
    ' search through the filenames
    DO
        ' check directory attribute
        Attribute = finddata.dwFileAttributes
        ' make sure the file found is a file and not a directory
        IF (Attribute AND &H10) = &H0 THEN
            ' get the matching filename from the windows structure
            Filename$ = finddata.cFileName
            Filename$ = LEFT$(Filename$, INSTR(Filename$, CHR$(0)) - 1)
            ' check wildcards (ascii 063)
            Var = INSTR(Filename$, "?") ' check for a Unicode character
            IF Var THEN
                PRINT Filename$
            END IF
        END IF
        ' continue loop if more files exist in search specification
    LOOP WHILE FindNextFileA(Wfile.Handle, _OFFSET(finddata))
    ' release window file handle to structure
    x = FindClose(Wfile.Handle)
END IF
END
0

There are 0 answers