Add a number after the foldername if typed foldername already exists

Hi!
I use the md command + inputbox function to create new folders. But when you create a folder with a name that already exists, there occurs an error. I want the an error handling that does like this:

When the folder name already exists I want the program to automatically add a number after the foldername. Like this:

New Folder
New Folder(2)
New Folder(3)
New Folder(4)
Etc.

(It is the same when you add new folder I in The Windows Explorer)

/Christian
ChristianJAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ggilmanCommented:
You didn't mention the language but since you mention "Input Box Function", I'm assuming Visual Basic. I have it working off a command button. If you want the path to be part of the input box, you can just get rid of it.

Private Sub Command1_Click()
Dim path As String
Dim fName As String
Dim curNum As Long

path = "c:\"
fName = InputBox("Enter Filename")
curNum = 1
While Dir(path & fName, vbDirectory) <> "" 'Check to see if it exists
  curNum = curNum + 1
  fName = "New Folder" & "(" & curNum & ")"
Wend
MkDir path & fName
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ggilmanCommented:
Oops...little problem with fName. Mine would always make the file be "New Folder" Here's the fix.

Private Sub Command1_Click()
Dim path As String
Dim fName As String
Dim inputName as String
Dim curNum As Long

path = "c:\"
inputName = InputBox("Enter Folder name")
fName = inputName
curNum = 1
While Dir(path & fName, vbDirectory) <> "" 'Check to see if it exists
  curNum = curNum + 1
  fName = inputName & "(" & curNum & ")"
Wend
MkDir path & fName
End Sub
0
ChristianJAuthor Commented:
Sorry for not telling you I was using VB

Your code worked Excellent!!

THANKS!

/ChristianJ
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Hardware

From novice to tech pro — start learning today.