• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1817
  • Last Modified:

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!
0
Drew_Mora
Asked:
Drew_Mora
  • 3
  • 2
  • 2
1 Solution
 
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
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!

 
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Grrrrrrrrr....
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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