Results 1 to 12 of 12

Thread: VB - Files & Folders Module

Threaded View

  1. #1

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545

    VB - Files & Folders Module

    Hi,

    Here is a module I use with almost all of my applications. It allows you to easily work with files and folders, without using the FileSystemObject.

    Email me at [email protected] with any improvements or criticisms, or things you would like added!!!!!!

    P.S. I have posted the code here, but I've also attached the module in case you would like to use this. It's pretty straight forward to use, but I will post some examples of how to use the functions when i get home from work :-)

    (I had to break it up into 3 seperate posts, because it was too long )

    VB Code:
    1. '*******************************************************************************
    2. '*******************************************************************************
    3. '** Module:         modFile
    4. '**
    5. '** Purpose:        This module has a number of useful functions for working
    6. '**                 with files and folders.
    7. '**
    8. '** Created By:     Clint Olsen
    9. '** Last Modified:  05/08/2003
    10. '**
    11. '** CONTENTS
    12. '** ********
    13. '** FileExists()        - Determines if a file exists
    14. '** DirectoryExists()   - Determines if a directory exists
    15. '** PathAndFile()       - Concatenates a path and a filename
    16. '** CopyFiles()         - Copies files from a source to a destination directory
    17. '** CopyFile()          - Copies a file from a source to a destination directory
    18. '** MoveFile()          - Moves a file from a source to a destination path
    19. '** DeleteFile()        - Deletes the specified file
    20. '** CompareFiles()      - Checks if two files are the same (checks size and dates)
    21. '** CreateFile()        - Creates a file (for output) and returns the file number
    22. '** OpenFile()          - Opens an existing file (for input) and returns the file number
    23. '** CloseFile()         - Closes a file by file number
    24. '** GetFileTitle()      - Returns the file title for a given path (eg. testfile)
    25. '** GetFileName()       - Returns the file name for a given path (ex. testfile.txt)
    26. '** GetFilePath()       - Returns the location of the file in the given path (ex. C:\Temp\)
    27. '** WriteArrayToFile()  - Writes an array of strings to the specified file
    28. '** ReadArrayFromFile() - Reads a file and returns the contents as an array of strings
    29. '** MoveToRecycleBin()  - Moves a file to the recycling bin
    30. '*******************************************************************************
    31. '*******************************************************************************
    32.  
    33.  
    34. Option Explicit
    35.  
    36.  
    37. '*******************************************************************************
    38. '** MODULE DECLARATIONS
    39. '*******************************************************************************
    40. Public Type SHFILEOPSTRUCT
    41.     hwnd As Long
    42.     wFunc As Long
    43.     pFrom As String
    44.     pTo As String
    45.     fFlags As Integer
    46.     fAnyOperationsAborted As Long
    47.     hNameMappings As Long
    48.     lpszProgressTitle As Long
    49. End Type
    50.  
    51. Public Declare Function SHFileOperation Lib "shell32.dll" _
    52.     Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
    53.  
    54. Public Const FO_DELETE = &H3
    55. Public Const FOF_ALLOWUNDO = &H40
    56.  
    57.  
    58. '*******************************************************************************
    59. '** Function:   FileExists
    60. '** Parameters:
    61. '**     FilePath (String) - the full path of a file
    62. '** Returns: True if the file exists, false otherwise
    63. '*******************************************************************************
    64. Public Function FileExists(FilePath As String) As Boolean
    65.     If Len(FilePath) = 0 Then
    66.         FileExists = False
    67.     Else
    68.         If Len(Dir$(FilePath)) > 0 Then
    69.             FileExists = True
    70.         Else
    71.             FileExists = False
    72.         End If
    73.     End If
    74. End Function
    75.  
    76.  
    77. '*******************************************************************************
    78. '** Function:   DirectoryExists
    79. '** Parameters:
    80. '**     DirectoryPath (String) - the full path of a directory
    81. '** Returns: True if the directory exists, false otherwise
    82. '*******************************************************************************
    83. Public Function DirectoryExists(DirectoryPath As String) As Boolean
    84.     If Len(DirectoryPath) = 0 Then
    85.         DirectoryExists = False
    86.     Else
    87.         If Len(Dir$(DirectoryPath, vbDirectory)) > 0 Then
    88.             DirectoryExists = True
    89.         Else
    90.             DirectoryExists = False
    91.         End If
    92.     End If
    93. End Function
    94.  
    95.  
    96. '*******************************************************************************
    97. '** Function:   PathAndFile
    98. '** Parameters:
    99. '**     DirectoryPath (String) - the full path of a directory
    100. '**     FileName (String) - a file name
    101. '** Returns: The full file path and file name
    102. '*******************************************************************************
    103. Public Function PathAndFile(DirectoryPath As String, FileName As String) As String
    104.     If Right$(DirectoryPath, 1) = "\" Then
    105.         PathAndFile = DirectoryPath & FileName
    106.     Else
    107.         PathAndFile = DirectoryPath & "\" & FileName
    108.     End If
    109. End Function

    See next 2 posts, for more!
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width