Results 1 to 12 of 12

Thread: How to pass all files and sub directories?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2003
    Posts
    160

    How to pass all files and sub directories?

    Hi guys
    I'm trying to use VBAccelerator zip to compress all content of directory and all files inside,

    how to use dir command to get all files in child directories?

    thanks

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: How to pass all files and sub directories?

    Post your code. I think I read that you can look at the files before you zip them. What do you need it for? Are you zipping up a folder?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2003
    Posts
    160

    Re: How to pass all files and sub directories?

    exactly, i'm zipping a folder and I dont have any code yet since I dont have all files

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to pass all files and sub directories?

    I use this to load all of the files in a certain directory into a listbox. You may be able to modify it to get the files, and do your zip thing. Hope this helps.
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim LoadFiles As String
    3. List1.Clear
    4. LoadFiles = Dir("c:\program files\qcap\*.*")
    5. Do While LoadFiles > ""
    6.    List1.AddItem LoadFiles
    7.    LoadFiles = Dir
    8. Loop
    9. End Sub

  5. #5
    Fanatic Member
    Join Date
    Jan 2005
    Location
    In front of this pc.
    Posts
    580

    Re: How to pass all files and sub directories?

    If you want to recursively search directories I think you'll need to use some APIs instead of Dir. Here's some code which does that.

    VB Code:
    1. 'Create a form with a command button (command1), a list box (list1)
    2. 'and four text boxes (text1, text2, text3 and text4).
    3. 'Type in the first textbox a startingpath like c:\
    4. 'and in the second textbox you put a pattern like *.* or *.txt
    5.  
    6. Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    7. Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
    8. Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
    9. Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
    10.  
    11. Const MAX_PATH = 260
    12. Const MAXDWORD = &HFFFF
    13. Const INVALID_HANDLE_VALUE = -1
    14. Const FILE_ATTRIBUTE_ARCHIVE = &H20
    15. Const FILE_ATTRIBUTE_DIRECTORY = &H10
    16. Const FILE_ATTRIBUTE_HIDDEN = &H2
    17. Const FILE_ATTRIBUTE_NORMAL = &H80
    18. Const FILE_ATTRIBUTE_READONLY = &H1
    19. Const FILE_ATTRIBUTE_SYSTEM = &H4
    20. Const FILE_ATTRIBUTE_TEMPORARY = &H100
    21.  
    22. Private Type FILETIME
    23.     dwLowDateTime As Long
    24.     dwHighDateTime As Long
    25. End Type
    26.  
    27. Private Type WIN32_FIND_DATA
    28.     dwFileAttributes As Long
    29.     ftCreationTime As FILETIME
    30.     ftLastAccessTime As FILETIME
    31.     ftLastWriteTime As FILETIME
    32.     nFileSizeHigh As Long
    33.     nFileSizeLow As Long
    34.     dwReserved0 As Long
    35.     dwReserved1 As Long
    36.     cFileName As String * MAX_PATH
    37.     cAlternate As String * 14
    38. End Type
    39. Function StripNulls(OriginalStr As String) As String
    40.     If (InStr(OriginalStr, Chr(0)) > 0) Then
    41.         OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
    42.     End If
    43.     StripNulls = OriginalStr
    44. End Function
    45.  
    46. Function FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, DirCount As Integer)
    47.     'KPD-Team 1999
    48.     'E-Mail: [email][email protected][/email]
    49.     'URL: [url]http://www.allapi.net/[/url]
    50.  
    51.     Dim FileName As String ' Walking filename variable...
    52.     Dim DirName As String ' SubDirectory Name
    53.     Dim dirNames() As String ' Buffer for directory name entries
    54.     Dim nDir As Integer ' Number of directories in this path
    55.     Dim i As Integer ' For-loop counter...
    56.     Dim hSearch As Long ' Search Handle
    57.     Dim WFD As WIN32_FIND_DATA
    58.     Dim Cont As Integer
    59.     If Right(path, 1) <> "\" Then path = path & "\"
    60.     ' Search for subdirectories.
    61.     nDir = 0
    62.     ReDim dirNames(nDir)
    63.     Cont = True
    64.     hSearch = FindFirstFile(path & "*", WFD)
    65.     If hSearch <> INVALID_HANDLE_VALUE Then
    66.         Do While Cont
    67.         DirName = StripNulls(WFD.cFileName)
    68.         ' Ignore the current and encompassing directories.
    69.         If (DirName <> ".") And (DirName <> "..") Then
    70.             ' Check for directory with bitwise comparison.
    71.             If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
    72.                 dirNames(nDir) = DirName
    73.                 DirCount = DirCount + 1
    74.                 nDir = nDir + 1
    75.                 ReDim Preserve dirNames(nDir)
    76.             End If
    77.         End If
    78.         Cont = FindNextFile(hSearch, WFD) 'Get next subdirectory.
    79.         Loop
    80.         Cont = FindClose(hSearch)
    81.     End If
    82.     ' Walk through this directory and sum file sizes.
    83.     hSearch = FindFirstFile(path & SearchStr, WFD)
    84.     Cont = True
    85.     If hSearch <> INVALID_HANDLE_VALUE Then
    86.         While Cont
    87.             FileName = StripNulls(WFD.cFileName)
    88.             If (FileName <> ".") And (FileName <> "..") Then
    89.                 FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
    90.                 FileCount = FileCount + 1
    91.                 List1.AddItem path & FileName
    92.             End If
    93.             Cont = FindNextFile(hSearch, WFD) ' Get next file
    94.         Wend
    95.         Cont = FindClose(hSearch)
    96.     End If
    97.     ' If there are sub-directories...
    98.     If nDir > 0 Then
    99.         ' Recursively walk into them...
    100.         For i = 0 To nDir - 1
    101.             FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "\", SearchStr, FileCount, DirCount)
    102.         Next i
    103.     End If
    104. End Function
    105. Sub Command1_Click()
    106.     Dim SearchPath As String, FindStr As String
    107.     Dim FileSize As Long
    108.     Dim NumFiles As Integer, NumDirs As Integer
    109.     Screen.MousePointer = vbHourglass
    110.     List1.Clear
    111.     SearchPath = Text1.Text
    112.     FindStr = Text2.Text
    113.     FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
    114.     Text3.Text = NumFiles & " Files found in " & NumDirs + 1 & " Directories"
    115.     Text4.Text = "Size of files found under " & SearchPath & " = " & Format(FileSize, "#,###,###,##0") & " Bytes"
    116.     Screen.MousePointer = vbDefault
    117. End Sub

  6. #6
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Re: How to pass all files and sub directories?

    You can ALSO use the FileSystemObject.

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim FSO As New Scripting.FileSystemObject, FileDir As Folder, Files As File
    3.  
    4. Set FileDir = FSO.GetFolder("C:\WINDOWS")
    5.  
    6. For Each Files In FileDir.Files
    7.     List1.AddItem Files.Name
    8. Next Files
    9.  
    10. Set FileDir = Nothing
    11. Set Files = Nothing
    12. Set FSO = Nothing
    13.  
    14. End Sub

    -Sir Loin

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2003
    Posts
    160

    Re: How to pass all files and sub directories?

    Sir Loin
    I can't find the reference to FSO, whats the exact name?

    anotherVBnewbie
    this is exactly what I want but how to put it inside an array , I want to make my application run as a service and there may not be any controls


    thanks

  8. #8
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: How to pass all files and sub directories?

    Add a reference to Microsoft Scripting Runtime

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Sep 2003
    Posts
    160

    Re: How to pass all files and sub directories?

    well , your code only passes *.* files in windows folder and not its subdirectories... but anotherVBnewbie's code that does the job is actually passing it to a listbox

  10. #10
    Fanatic Member
    Join Date
    Jan 2005
    Location
    In front of this pc.
    Posts
    580

    Re: How to pass all files and sub directories?

    It shouldn't be that hard to convert the code I posted to do what you want it to do...but since it's an example it is using listboxes and text boxes to display what all goes on..just dump the command click (replace that with whatever you want to call the actual function with) and all references to listboxes, etc, and replace the "List1.AddItem path & FileName" with a couple of lines like
    VB Code:
    1. arrayName(Ubound(arrayName)) = path & FileName
    2. ReDim Preserve arrayName(Ubound(arrayName) + 1)

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Sep 2003
    Posts
    160

    Re: How to pass all files and sub directories?

    I tried to do that, but since this function is calling itself inside, it gives me this error: "This Array is fixed or temporarily locked"

    what can I do about that?

  12. #12
    Fanatic Member
    Join Date
    Jan 2005
    Location
    In front of this pc.
    Posts
    580

    Re: How to pass all files and sub directories?

    You can either declare the array in the declares portion of the form or make it a public array by declaring it in a module. Also, if this code is going to run more than once per program cycle, you should redim the array prior to each time the code is executed to prevent adding new data to the old data.

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