[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

How do I create a function that loops through folders to see if a folder exists and create a new folder?

Hello.  So here is what I need.  I need a function that accepts a folder path like, "C:\users\myuser" and checks to see if that folder exists and if it does exist it needs to append a "1" to the end of 'myuser' and continue to check if the appended folder name exists until it finds a folder that does NOT exist and return that folder path.  I may have "myuser1" and "myuser2" existing but "myuser3" is available.  so i would need my function to return that path to my main function so it could create the folder and move on with it's other operations.


basically i have a main function that does a lot of operations and one part of the operation is to call a function that checks if a folder name is available for creation.

anyone help with that?

thanks
0
linuxrox
Asked:
linuxrox
  • 3
  • 2
3 Solutions
 
BaleboosCommented:
Might be buggy. Don't have an IDE

Function folderExists(fld as string) as string

fld = fld.tolower.trim(New Char() {"\"}) 'get rid of trailing slash because you need to check if file exists

dim fso = createobject("scripting.filesystemobject")

if not fso.folderexists(fld) andalso not fso.fileexists(fld) return fld

dim prnFld 

if fso.folderexists(fld) then
   prnFld = fso.getfolder(fld).parentfolder
else
   prnFld = fso.getfile(fld).parentfolder
end if
dim maxFld = 0
for each sFld in prnFld.subfolders
if left(sfld.path, len(fld)).tolower() = fld and IsNumeric(mid(sfld.path, len(fld), 1000)) then 
   dim num = val(mid(sfld.path, len(fld), 1000))
   if num > maxfld then maxfld = num
end if
next

for each sFld in prnFld.files
if left(sfld.path, len(fld)).tolower() = fld and IsNumeric(mid(sfld.path, len(fld), 1000)) then 
   dim num = val(mid(sfld.path, len(fld), 1000))
   if num > maxfld then maxfld = num
end if
next



fso.move(fld, fld & maxfld)


return  fld & (maxfld + 1)

end function

Open in new window

0
 
Jacques Bourgeois (James Burger)Commented:
Babelos did provide something that works, but this code uses classes created for VBScript / VBA, not .NET.

For performance, maintenance, scalability and most important, simply because they offer a lot more possibilities, you should use .NET classes as much as possible.

.NET has classes that do a lot more than the old Scripting library to work with drives, directories and files. Here is how I would approach it in purely .NET code.
Dim baseName As String = "C:\users\myuser"

'You did not say what you wanted to do C:\users\myuser did not exist,
'so I assume that you want to create it

If Not System.IO.Directory.Exists(baseName) Then
	System.IO.Directory.CreateDirectory("C:\users\myuser")
Else
	For index As Integer = 1 To 100	'Replace 100 by the highest number or directories you are ready to accept
		If Not System.IO.Directory.Exists(baseName & index) Then
			System.IO.Directory.CreateDirectory("C:\users\myuser" & index)
		End If
	Next
End If

Open in new window

0
 
linuxroxAuthor Commented:
here's what i came up with guys....this works perfectly
create "c:\folder" and "c:\folder1" and "c:\folder2" on your drive and run this.  

Imports System
Imports System.IO

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim folder As String = ""
        Dim x As Integer = -1
        Dim found As Boolean = False
        Do
            x = x + 1
            If x = 0 Then
                folder = "c:\folder"
            Else
                folder = "c:\folder" & x
            End If
            found = checkfolder(folder, x)
        Loop Until found = True
        MsgBox(folder)
    End Sub


Private Function checkfolder(ByVal folder As String, ByVal x As Integer) As Boolean
        If Directory.Exists(folder) Then
            checkfolder = False
        Else
            checkfolder = True
            Return checkfolder
        End If
    End Function

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
BaleboosCommented:
James, 2 comments:

1) You don't check if file exists. You can't name a directory the same as a file. And because you need to check files as well, you need to get rid of the trailing slash.

2) Author wants the directory path string, not the directory.

Linuxrox: your solution also has the first problem.
0
 
linuxroxAuthor Commented:
Baleboos:  There are no files in the root folder where the folders are being created so there is no need to check for existing files with the same name.  these are basically home folders for users in active directory and they are all created on a main root drive for which no files exist.
0
 
linuxroxAuthor Commented:
This was exactly what I was trying to achieve.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now