'************************************************
'** HOW TO USE THESE FUNCTIONS
'************************************************
'*************************************************************
'How to use FileExists()
'*************************************************************
Dim strFileName As String
strFileName = "C:\Temp\testfile.txt"
If FileExists(strFileName) Then
'The file exists
Else
'The file doesn't exist!
End If
'*************************************************************
'How to use DirectoryExists()
'*************************************************************
Dim strFolderName As String
strFolderName = "C:\Temp\"
If DirectoryExists(strFolderName) Then
'The folder exists
Else
'The folder does not exist
End If
'*************************************************************
'How to use PathAndFile()
'NOTE: I use this function because App.Path usually returns a
'backslash if your program is running on C:\, but if you run
'your program from a folder inside, such as C:\Temp it does not
'return a backslash at the end
'
'I also use this in other places in my code to prevent mistakes
'from omitting a slash by accident!
'*************************************************************
Dim strFileName As String
'Ensure that we get the correct application path
strFileName = PathAndFile(App.Path, "data.txt")
'*************************************************************
'How to use CopyFiles()
'*************************************************************
Dim blnSuccess As Boolean
'To copy ALL files and overwrite them
blnSuccess = CopyFiles("C:\Temp\Test1\", "C:\Temp\Test2\")
'To copy ALL files WITHOUT overwriting any
blnSuccess = CopyFiles("C:\Temp\Test1\", "C:\Temp\Test2", False)
'To copy ALL txt files overwriting any existing files
blnSuccess = CopyFiles("C:\Temp\Test1\", "C:\Temp\Test2", True, "txt")
If blnSuccess Then
'The copy procedure was successful
Else
'The copy procedure was NOT successful
End If
'*************************************************************
'How to use CopyFile()
'*************************************************************
Dim blnSuccess As Boolean
'To copy a file, overwriting it if it already exists
blnSuccess = CopyFile("C:\Temp\Test1\test1.txt", "C:\Temp\Test2\test1.txt")
'To copy a file, WITHOUT overwriting it if it already exists
blnSuccess = CopyFile("C:\Temp\Test1\test1.txt", "C:\Temp\Test2\test1.txt", False)
If blnSuccess Then
'The copy procedure was successful
Else
'The copy procedure was successful
End If
'*************************************************************
'How to use MoveFile()
'*************************************************************
'Same as CopyFile, except it deletes the source file after copying
'*************************************************************
'How to use CompareFiles()
'NOTE: All this does is check the file sizes, and dates,
' so it is potentially still possible for files to be different
'*************************************************************
Dim blnSuccess As Boolean
blnSuccess = CompareFiles("C:\Temp\file1.txt", "C:\Temp\file2.txt")
If blnSuccess = True Then
'THe files are the same!
Else
'The files are NOT the same!
End If
'*************************************************************
'How to use CreateFile()
'NOTE: I use this function every time I need to create a file
' for output. It allows you to avoid calling FreeFile
' each time
'*************************************************************
Dim fileNumber As Long
fileNumber = CreateFile("C:\Temp\testfile.txt")
'If you need to check to make sure the file was created
'do it like this
If fileNumber < 0 Then
'An error occured and the file was not created!
Else
'The file was created successfully
End If
'*************************************************************
'How to use OpenFile()
'NOTE: This function is used to open a file for input
'*************************************************************
Dim fileNumber As Long
fileNumber = OpenFile("C:\Temp\testfile.txt")
'If you need to check to make sure the file was opened
'successfully, do it like this
If fileNumber < 0 Then
'An error occured and the file was not opened!
Else
'The file was created successfully
End If
'*************************************************************
'How to use CloseFile()
'*************************************************************
Dim fileNumber As Long
fileNumber = OpenFile("C:\Temp\testfile.txt")
'do your stuff here
CloseFile (fileNumber)
'*************************************************************
'How to use DeleteFile()
'*************************************************************
Dim blnSuccess As Boolean
blnSuccess = DeleteFile("C:\Temp\testfile.txt")
If blnSuccess Then
'The file was deleted successfully
Else
'The file was not deleted!
End If
'*************************************************************
'How to use GetFileTitle()
'Note: This returns the file title only
' Example: For C:\Temp\testfile.txt it returns testfile
'*************************************************************
Dim strFileName As String
Dim strFileTitle As String
strFileName = "C:\Temp\testfile.txt"
strFileTitle = GetFileTitle(strFileName)
'*************************************************************
'How to use GetFileName()
'Note: This returns the file name (with extension)
' Example: For C:\temp\testfile.txt it returns testfile.txt
'*************************************************************
Dim strFileName As String
Dim strFileNameOnly As String
strFileName = "C:\Temp\testfile.txt"
strFileNameOnly = GetFileName(strFileName)
'*************************************************************
'How to use GetFilePath()
'Note: This returns the file path only
' Example: For C:\Temp\testfile.txt it returns C:\Temp\
'*************************************************************
Dim strFileName As String
Dim strFilePath As String
strFileName = "C:\Temp\testfile.txt"
strFilePath = GetFilePath(strFileName)
'*************************************************************
'How to use WriteArrayToFile()
'*************************************************************
Dim strArr(3) As String
strArr(0) = "Test 1"
strArr(1) = "Test 2"
strArr(2) = "Test 3"
strArr(3) = "Test 4"
blnSuccess = WriteArrayToFile(strArr, "C:\Temp\output.txt")
If blnSuccess Then
'The array was successfully written to the file
Else
'The array was not written to the file!
End If
'*************************************************************
'How to use ReadArrayFromFile()
'*************************************************************
Dim strArr() As String
strArr = ReadArrayFromFile("C:\Temp\output.txt")
'*************************************************************
'How to use MoveToRecycleBin()
'*************************************************************
Dim strFileName As String
blnSuccess = MoveToRecycleBin(strFileName)
If blnSuccess = True Then
'The file was sent to the recycling bin
Else
'The file was NOT sent to the recycling bin
End If