|
-
Jan 11th, 2002, 01:36 PM
#1
Thread Starter
Junior Member
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.
-
Jan 11th, 2002, 01:54 PM
#2
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
-
Jan 11th, 2002, 02:07 PM
#3
Thread Starter
Junior Member
Thanks - exactly what I wanted.
-
Jan 13th, 2002, 06:57 AM
#4
Thread Starter
Junior Member
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
-
Jan 13th, 2002, 06:59 AM
#5
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|