Results 1 to 5 of 5

Thread: Directories finding and naming

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2001
    Posts
    20

    Directories finding and naming

    Here is what I want to do, there are folders called UserData(x) where (x) is there is a number. They are named UserData1 Userdata2 Userdata3 etc. up to 7 / 8 normally.
    However I want to be able to work out how many directories there are with these names, so I can create a new one with a name 1 number higher than the existing highest.
    eg. if the higher one was called UserData8 I would create UserData9 with my program.
    I know about creating directories, but I need a bit of help with the first bit, and adding the number to the directory name.

  2. #2
    PWNettle
    Guest
    I'd imagine this could be done with the built-in file functions, but here's a way to do it for sure with the FileSystemObject:
    Code:
    '  Requires a reference to the Microsoft Scripting Runtime.
    Dim objFSO As Scripting.FileSystemObject
    Dim objFolder As Scripting.Folder
    Dim strBasePath As String
    Dim lngHighest As Long
    Dim lngCurrent As Long
    
    '  Set the base path that contains the 'userdata' folders,
    '    including the trailing backslash character.
    strBasePath = "c:\temp\"
    lngHighest = 0
    
    Set objFSO = New Scripting.FileSystemObject
    
    For Each objFolder In objFSO.GetFolder(strBasePath).SubFolders
      With objFolder
        If LCase$(Left$(.Name, 8)) = "userdata" Then
          lngCurrent = Right$(.Name, 1)
          If lngCurrent > lngHighest Then
            lngHighest = lngCurrent
          End If
        End If
      End With
    Next
    
    Call objFSO.CreateFolder(strBasePath & "UserData" & CStr(lngHighest + 1))
    
    Set objFSO = Nothing
    You have to know the path that the subfolders to examine exist in. This looks at the subfolders within that folder, checks them to see if they are 'userdata' folders, and keeps track of the highest folder number. You can then create a new folder based on the hightest number found.

    Paul

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2001
    Posts
    20
    Thanks - exactly what I wanted.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Dec 2001
    Posts
    20
    Just one problem with this code
    Code:
    lngCurrent = Right$(.Name, 1)
    This piece of code comes up with the error:
    Runtime error 13
    Type Mismatch

    Please help
    Thanks

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2001
    Posts
    20
    I forgot to say, one thing to make it more annoying is the folders start with:
    UserData
    UserData2
    UserData3
    etc.
    There is no userdata1!

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