how to write a VBScript to create a folder path 7 levels deep and check for the folder on each level

Hello experts, thanks in advance for the help.

Here is my issue: I am trying to write a script that does the following:
-Checks to see if the folder exists
- Creates the folder
-Copies files

i would like to check for "h:\PicisTest\E" and "h:\PicisTest\F"
-->if exists then "Folder already exists" else 'createFolder"

Here is what I have so far:

'TestIfElseLoopThenCreateFolder.vbs
'Check for folder then createFolder.vbs

'+++++++++++++++++
'check for folder
'+++++++++++++++++
WScript.Echo(CheckFolder("h:\PicisTest\E"))
Function CheckFolder (foldr)
Dim fs, s
  Set fs = CreateObject("Scripting.FileSystemObject")
If (fs.FolderExists(foldr)) Then
  s = foldr & " already exists"
Else
'create folders for data copy
Set fs = CreateObject("Scripting.FileSystemObject")
Set foldr = fs.CreateFolder("h:\PicisTest\E")
Set foldr = fs.CreateFolder("h:\PicisTest\F")
Set foldr = fs.CreateFolder("h:\PicisTest\E\MSSQL$PICIS")
Set foldr = fs.CreateFolder("h:\PicisTest\E\MSSQL$PICIS\DATA")
Set foldr = fs.CreateFolder("h:\PicisTest\E\MSSQL$PICIS\DATA\MSSQL$PICIS")
Set foldr = fs.CreateFolder("h:\PicisTest\E\MSSQL$PICIS\DATA\MSSQL$PICIS\Data")
'output to user informing user of folder creation
msgbox "Folder Created " & foldr
'Start new folder creation
Set foldr = fs.CreateFolder("h:\PicisTest\F\MSSQL$PICIS")
Set foldr = fs.CreateFolder("h:\PicisTest\F\MSSQL$PICIS\DATA")
Set foldr = fs.CreateFolder("h:\PicisTest\F\MSSQL$PICIS\DATA\MSSQL$PICIS")
Set foldr = fs.CreateFolder("h:\PicisTest\F\MSSQL$PICIS\DATA\MSSQL$PICIS\Data")
'output to user informing user of folder creation
msgbox "Folder Created " & foldr
End If
CheckFolder = s
End Function

Thanks much!
Drew_MoraAsked:
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.

Drew_MoraAuthor Commented:
One more thing, the sript currently will check for the one directory, but then bombs out after creating the first path.

Thanks
0
BeldoranCommented:
You need to create the root directory first if it is not there
eg.
Set foldr = fs.CreateFolder("d:\PicisTest")
Set foldr = fs.CreateFolder("d:\PicisTest\E")

you are testing for d:\picistest\E not d:\picistest and if the first one does not exist...

Regards
Bel
0
BeldoranCommented:
sorry i maent h: I was using d: to test it on my system.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Drew_MoraAuthor Commented:
Thanks BelDroan! I think I gave mis-leading info.

The root folder should be H:\PicisTest

Then I would like to check for "E" and "F"
  If E does not exist then create E\E\MSSQL$PICIS\DATA\MSSQL$PICIS\Data

Check for "F" if "F" exists then end. Else create F\MSSQL$PICIS\DATA\MSSQL$PICIS\Data

So far i have only been succesful when checking for 1 folder.

Thanks
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is one way to create a path:

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
path = "c:\a\b\c\d\e"
folders = Split (path, "\")
for i = lbound(folders) to ubound(folders)
    if folders(i) <> "" then
        p = p & folders(i) & "\"
        if (not fso.FolderExists (p)) then
            fso.CreateFolder (p)
        end if
    end if
next
0
BeldoranCommented:
Code below works. notes...
You can use your code for creating folders, that works, but Idle_Minds code is nicer ;-)
I tested this and it works.
The function tests for the existance of a path, but the creation routine also tests so unless you need this you can remove this bit.

Bel

'TestIfElseLoopThenCreateFolder.vbs
'Check for folder then createFolder.vbs

'+++++++++++++++++
'check for folder
'+++++++++++++++++
WScript.Echo(CheckFolder("h:\PicisTest\E", "h:\PicisTest\E\MSSQL$PICIS\DATA\MSSQL$PICIS\Data"))
WScript.Echo(CheckFolder("h:\PicisTest\F", "h:\PicisTest\F\MSSQL$PICIS\DATA\MSSQL$PICIS\Data"))
Function CheckFolder (foldr, fpath)
Dim fs, s, folders, p, i
  Set fs = CreateObject("Scripting.FileSystemObject")
If (fs.FolderExists(foldr)) Then
  s = foldr & " already exists"
Else
'create folders for data copy
folders = Split (fpath, "\")
for i = lbound(folders) to ubound(folders)
    if folders(i) <> "" then
        p = p & folders(i) & "\"
        if (not fs.FolderExists (p)) then
            fs.CreateFolder (p)
        end if
    end if
next
'output to user informing user of folder creation
msgbox "Folder Created " & foldr
End If
CheckFolder = s
End Function
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
Mike TomlinsonMiddle School Assistant TeacherCommented:
Grrrrrrrrr....
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
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.