Snippets



Path Exists.
Determine if a folder or file exists.
Will find a folder even if the folder is empty.

'Usage:
'Find a file - PathExists("c:\windows\system\kernel32.dll")
'Find a folder - PathExists("c:\windows\system")

a$ = "c:\windows\system"
If PathExists(a$) Then
    print "Path found!"
Else
    print "Path not found."
End If

end

Function PathExists(pathSpec$)
    struct PathExistsWFD, x as char[318] 'blank _WIN32_FIND_DATA struct
    calldll #kernel32, "FindFirstFileA", pathSpec$ as ptr, PathExistsWFD as struct, hfind as ulong
    calldll #kernel32, "FindClose", hfind as ulong, ret as boolean
    If hfind <> _INVALID_HANDLE_VALUE Then PathExists = 1
End Function


Compare File Times.
 
a$ = "c:\test.txt"
b$ = "c:\test2.txt"
ret = CompareFileTimes(a$, b$)

Select Case ret
    Case -1
        print "An error occured"
    Case 0
        print "Files have the same write times"
    Case 1
        print a$; " was written to last"
    Case 2
        print b$; " was written to last"
End Select

end

Function CompareFileTimes(file1$, file2$)
'Returns: File most recently written to.
'-1 for any error, 0 for equal times, 1 for file1$, 2 for file2$

    'FILETIME structs
    struct CR, dwLowDateTime as ulong, dwHighDateTime as ulong
    struct LA, dwLowDateTime as ulong, dwHighDateTime as ulong
    struct LW, dwLowDateTime as ulong, dwHighDateTime as ulong
    struct LW2, dwLowDateTime as ulong, dwHighDateTime as ulong

    calldll #kernel32, "CreateFileA", file1$ as ptr, _GENERIC_READ as ulong, _
    0 as ulong, 0 as long, _OPEN_EXISTING as ulong, _
    _FILE_ATTRIBUTE_NORMAL as ulong, 0 as long, hfile1 as ulong

    calldll #kernel32, "CreateFileA", file2$ as ptr, _GENERIC_READ as ulong, _
    0 as ulong, 0 as long, _OPEN_EXISTING as ulong, _
    _FILE_ATTRIBUTE_NORMAL as ulong, 0 as long, hfile2 as ulong

    If hfile1 <> _INVALID_HANDLE_VALUE and hfile2 <> _INVALID_HANDLE_VALUE Then
        calldll #kernel32, "GetFileTime", hfile1 as ulong, CR as ptr, LA as ptr, LW as ptr, Ra as boolean
        calldll #kernel32, "GetFileTime", hfile2 as ulong, CR as struct, LA as struct, LW2 as struct, Rb as boolean
        If Ra and Rb Then
            calldll #kernel32, "CompareFileTime", LW as struct, LW2 as struct, ret as long
            Select Case ret
                Case 0
                    CompareFileTimes = 0
                Case 1
                    CompareFileTimes = 1
                Case -1
                    CompareFileTimes = 2
            End Select
        Else
            CompareFileTimes = -1
        End If
    Else
        CompareFileTimes = -1
    End If
    calldll #kernel32, "CloseHandle", hfile1 as long, r as boolean
    calldll #kernel32, "CloseHandle", hfile2 as long, r as boolean
End Function



 Get the size of a folder. 

    struct WFD,_
    dwFileAttributes as ulong,_
    ftCreationTimedwLowDateTime as ulong,_
    ftCreationTimedwHighDateTime as ulong,_
    ftLastAccessTimedwLowDateTime as ulong,_
    ftLastAccessTimedwHighDateTime as ulong,_
    ftLastWriteTimedwLowDateTime as ulong,_
    ftLastWriteTimedwHighDateTime as ulong,_
    nFileSizeHigh as ulong,_
    nFileSizeLow as ulong,_
    dwReserved0 as ulong,_
    dwReserved1 as ulong,_
    cFileName as char[260],_
    cAlternateFileName as char[14]

folder$ = "c:\my documents"
size = GetFolderSize(folder$)
Print "Size of folder "; folder$; " is "; size

end

Function GetFolderSize(folderPath$)
    Path$ = lower$(folderPath$)+ "\*"
    calldll #kernel32, "FindFirstFileA", Path$ as ptr, WFD as struct, hfind as ulong
    If hfind <> _INVALID_HANDLE_VALUE Then found = 1
    Do While found
        dirName$ = WFD.cFileName.struct
        If (WFD.dwFileAttributes.struct and _FILE_ATTRIBUTE_DIRECTORY) Then
            If dirName$ <> "." and dirName$ <> ".." Then
                folderSize = folderSize + GetFolderSize(folderPath$ + "\" + dirName$)
            End If
        Else
            folderSize = folderSize + WFD.nFileSizeLow.struct
        End If
        calldll #kernel32, "FindNextFileA", hfind as ulong, WFD as struct, found as boolean
    Loop
    calldll #kernel32, "FindClose", hfind as ulong, ret as boolean
    GetFolderSize = folderSize
End Function




Make a copy of  file.
The CopyFile function copies an existing file to a new file while preserving the original.

'Parameters:
'ExistingFileName$: Path and name of the file to be copied.

'NewFileName$: New path and name for the copied file.

'FailIfExists: If this parameter is TRUE and the new file already exists,
'the function fails. If this parameter is FALSE and the new file already
'exists, the function overwrites the existing file and succeeds.
'If the function succeeds, the return value is nonzero.

'Usage

Global TRUE, FALSE: TRUE = 1: FALSE = 0

r = CopyFile(fileA$, fileB$, FALSE)

end

Function CopyFile(ExistingFileName$, NewFileName$, FailIfExists)
    calldll #kernel32, "CopyFileA", ExistingFileName$ as ptr, NewFileName$ as ptr, _
    FailIfExists as boolean, CopyFile as boolean
End Function



Folder Killer.

WARNING! Use extreme caution when using this set of routines!

All files and subfolders in the specified folder will be permanently deleted.
THE DELETED FILES AND FOLDERS CANNOT BE RESTORED!


    struct WFD,_
    dwFileAttributes as ulong,_
    ftCreationTimedwLowDateTime as ulong,_
    ftCreationTimedwHighDateTime as ulong,_
    ftLastAccessTimedwLowDateTime as ulong,_
    ftLastAccessTimedwHighDateTime as ulong,_
    ftLastWriteTimedwLowDateTime as ulong,_
    ftLastWriteTimedwHighDateTime as ulong,_
    nFileSizeHigh as ulong,_
    nFileSizeLow as ulong,_
    dwReserved0 as ulong,_
    dwReserved1 as ulong,_
    cFileName as char[260],_
    cAlternateFileName as char[14]

folder$ = "c:\0\deleteme"

Confirm "Are you sure you want to"+chr$(13)+"DELETE "+upper$(folder$)+chr$(13)+" and everything in it?"; answer$
If answer$ = "no" Then end

ret = KillFolder(folder$)
If ret = 0 Then
    Print folder$; " has been deleted."
Else
    Print folder$; " could not be deleted."
End If

end

Function KillFolder(folderPath$)
    Call KillSubFolders folderPath$
    KillFolder = rmdir(folderPath$)
End Function

Sub KillSubFolders rootPath$
    Path$ = lower$(rootPath$)+ "\*"
    calldll #kernel32, "FindFirstFileA", Path$ as ptr, WFD as struct, hfind as ulong
    If hfind <> _INVALID_HANDLE_VALUE Then found = 1
    Do While found
        fdName$ = WFD.cFileName.struct
        If (WFD.dwFileAttributes.struct and _FILE_ATTRIBUTE_DIRECTORY) Then
            If fdName$ <> "." and fdName$ <> ".." Then
                Call KillSubFolders rootPath$ + "\" + fdName$
                ret = rmdir(rootPath$ + "\" + fdName$)
            End If
        Else
            Kill rootPath$ + "\" + fdName$
        End If
        calldll #kernel32, "FindNextFileA", hfind as ulong, WFD as struct, found as boolean
    Loop
    calldll #kernel32, "FindClose", hfind as ulong, ret as boolean
End Sub