How do I access series of files in perticular folder

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abhiab54
    New Member
    • Jan 2007
    • 10

    How do I access series of files in perticular folder

    I need to access serially all files in perticular folder eg. C:\xls files
    In 'Xls files' folder in 'C' drive there are no of XLS files i need to open them all one by one & update them.
    Is there any concept like pointer in VB wherein I could increase ptr that will then point to next file in same folder.
  • bgirardo
    New Member
    • Jan 2007
    • 4

    #2
    Originally posted by abhiab54
    I need to access serially all files in perticular folder eg. C:\xls files
    In 'Xls files' folder in 'C' drive there are no of XLS files i need to open them all one by one & update them.
    Is there any concept like pointer in VB wherein I could increase ptr that will then point to next file in same folder.

    You may be looking at the "Dir" Function

    Here's the example in the help file:
    This example uses the Dir function to check if certain files and directories exist. On the Macintosh, “HD:” is the default drive name and portions of the pathname are separated by colons instead of backslashes. Also, the Microsoft Windows wildcard characters are treated as valid file-name characters on the Mac. However, you can use the MacID function to specify file groups.


    Code:
    Dim MyFile, MyPath, MyName
    ' Returns "WIN.INI" (on Microsoft Windows) if it exists.
    MyFile = Dir("C:\WINDOWS\WIN.INI")    
    
    ' Returns filename with specified extension. If more than one *.ini
    ' file exists, the first file found is returned.
    MyFile = Dir("C:\WINDOWS\*.INI")
    
    ' Call Dir again without arguments to return the next *.INI file in the 
    ' same directory.
    MyFile = Dir
    
    ' Return first *.TXT file with a set hidden attribute.
    MyFile = Dir("*.TXT", vbHidden)
    
    ' Display the names in C:\ that represent directories.
    MyPath = "c:\"    ' Set the path.
    MyName = Dir(MyPath, vbDirectory)    ' Retrieve the first entry.
    Do While MyName <> ""    ' Start the loop.
        ' Ignore the current directory and the encompassing directory.
        If MyName <> "." And MyName <> ".." Then
            ' Use bitwise comparison to make sure MyName is a directory.
            If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
                Debug.Print MyName    ' Display entry only if it
            End If    ' it represents a directory.
        End If
        MyName = Dir    ' Get next entry.
    Loop

    Comment

    • seshu
      New Member
      • Nov 2006
      • 156

      #3
      Hi this is seshu
      i saw ur query i dont know wat u are trying to do on each file i mean access means there are many thigs but here i have code to check whether the file exists or not and the folder, sub folder exists or not if exists then the application will copye a file from the folder u specified else creates that path and then copies and here is the code
      Code:
      Dim fsys As New FileSystemObject
      Dim fsys1 As New FileSystemObject
      Dim fd As Folder
      Dim fd1 As Folder
      Dim fls As Files
      Dim f As File
      Dim str As String
      Dim str1 As String
      Dim str2 As String
      Set fd = fsys.GetFolder(path)
      Set fls = fd.Files
      For Each f In fls
          'Debug.Print f.Name
              str = Split(f.Name, "_")(1)
              str1 = Split(f.Name, "_")(3)
              str3 = "path" & str
              str2 = "path" & str & "\" & str1
           If fsys1.FolderExists(str3) Then
               If fsys1.FolderExists(str2) Then
                  fsys.CopyFile f.Path, "destination path\" & str & "\" & str1 & "\", True
              Else
                  fsys1.CreateFolder "folder to be created path" & str & "\" & str1
                  fsys.CopyFile f.Path, "destination path\" & str & "\" & str1 & "\", True
              End If
            Else
                  fsys1.CreateFolder "folder to be created path" & str
           End If
      Next
      End Sub
      here my file name are like abc_def.txt that is the reason y i took split function
      hope u will understand the code
      regards
      seshu

      Comment

      Working...