|
File System - Delete, Copy, Verify Files and Folders via MS Access
Listed here are some handy functions that use commands available within Microsoft Access to interact with the Windows File System.
Use the DIR Function to check if a file or folder exists.
Function FS_CheckPath(strPath As String) As Boolean
'Checks the Path of the File or Directory passed in the strPath Argument
'Returns True if the File or Directory exists
On Error GoTo Err_Trap
If Dir(strPath, vbDirectory) <> "" Then
FS_CheckPath = True
Else
FS_CheckPath = False
End If
Err_Trap_Exit:
Exit Function
Err_Trap:
FS_CheckPath = False
MsgBox Err.Description
Resume Err_Trap_Exit
End Function
Use the KILL Function to delete a file
Function FS_DeleteFile(strPath As String) As Boolean
'Delete File passed in the strPath Argument
'Returns True if the successful
On Error GoTo Err_Trap
If FS_CheckPath(strPath) Then
Kill (strPath)
FS_DeleteFile = True
Else
FS_DeleteFile = False
End If
Err_Trap_Exit:
Exit Function
Err_Trap:
FS_DeleteFile = False
MsgBox Err.Description
Resume Err_Trap_Exit
End Function
Use the COPYFILE function to copy a file.
Function FS_CopyFile(strSource As String, strDestination As String, blnOverWrite As Boolean) As Boolean
'Delete File passed in the strPath Argument
'Returns True if the successful
On Error GoTo Err_Trap
If FS_CheckPath(strSource) = False Then
MsgBox "Source File Missing"
FS_CopyFile = False
Else
If FS_CheckPath(strDestination) Then
If blnOverWrite = False Then
MsgBox "Destination File already exists"
FS_CopyFile = False
Else
'Copy file if Destination exist and Overwrite is True
FileCopy strSource, strDestination
FS_CopyFile = True
End If
Else
'Copy file if Destination dose not exist
FileCopy strSource, strDestination
FS_CopyFile = True
End If
End If
Err_Trap_Exit:
Exit Function
Err_Trap:
FS_CopyFile = False
'MsgBox Err.Description
Resume Err_Trap_Exit
End Function
|