'*******************************************************************************
'*******************************************************************************
'** Module: modFile
'**
'** Purpose: This module has a number of useful functions for working
'** with files and folders.
'**
'** Created By: Clint Olsen
'** Last Modified: 05/08/2003
'**
'** CONTENTS
'** ********
'** FileExists() - Determines if a file exists
'** DirectoryExists() - Determines if a directory exists
'** PathAndFile() - Concatenates a path and a filename
'** CopyFiles() - Copies files from a source to a destination directory
'** CopyFile() - Copies a file from a source to a destination directory
'** MoveFile() - Moves a file from a source to a destination path
'** DeleteFile() - Deletes the specified file
'** CompareFiles() - Checks if two files are the same (checks size and dates)
'** CreateFile() - Creates a file (for output) and returns the file number
'** OpenFile() - Opens an existing file (for input) and returns the file number
'** CloseFile() - Closes a file by file number
'** GetFileTitle() - Returns the file title for a given path (eg. testfile)
'** GetFileName() - Returns the file name for a given path (ex. testfile.txt)
'** GetFilePath() - Returns the location of the file in the given path (ex. C:\Temp\)
'** WriteArrayToFile() - Writes an array of strings to the specified file
'** ReadArrayFromFile() - Reads a file and returns the contents as an array of strings
'** MoveToRecycleBin() - Moves a file to the recycling bin
'*******************************************************************************
'*******************************************************************************
Option Explicit
'*******************************************************************************
'** MODULE DECLARATIONS
'*******************************************************************************
Public Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As Long
End Type
Public Declare Function SHFileOperation Lib "shell32.dll" _
Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Public Const FO_DELETE = &H3
Public Const FOF_ALLOWUNDO = &H40
'*******************************************************************************
'** Function: FileExists
'** Parameters:
'** FilePath (String) - the full path of a file
'** Returns: True if the file exists, false otherwise
'*******************************************************************************
Public Function FileExists(FilePath As String) As Boolean
If Len(FilePath) = 0 Then
FileExists = False
Else
If Len(Dir$(FilePath)) > 0 Then
FileExists = True
Else
FileExists = False
End If
End If
End Function
'*******************************************************************************
'** Function: DirectoryExists
'** Parameters:
'** DirectoryPath (String) - the full path of a directory
'** Returns: True if the directory exists, false otherwise
'*******************************************************************************
Public Function DirectoryExists(DirectoryPath As String) As Boolean
If Len(DirectoryPath) = 0 Then
DirectoryExists = False
Else
If Len(Dir$(DirectoryPath, vbDirectory)) > 0 Then
DirectoryExists = True
Else
DirectoryExists = False
End If
End If
End Function
'*******************************************************************************
'** Function: PathAndFile
'** Parameters:
'** DirectoryPath (String) - the full path of a directory
'** FileName (String) - a file name
'** Returns: The full file path and file name
'*******************************************************************************
Public Function PathAndFile(DirectoryPath As String, FileName As String) As String
If Right$(DirectoryPath, 1) = "\" Then
PathAndFile = DirectoryPath & FileName
Else
PathAndFile = DirectoryPath & "\" & FileName
End If
End Function