Hello.

I've got a script that creates subfolders at multiple levels, on a
remote server:

(watch for word-wrap)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dim strDestinationServer, strAppName, DestinationPath, strTicketNumber
Dim Yearfrom, Monthfrom, Dayfrom, strDate, InstallPath, Sub1, Sub2,
CodeSub1, Sub3

strDestinationServer = "000-SERVER-05"
strAppName = "Webteam"
DestinationPath = "\\" & strDestinationServer & "\D$\Install\" &
strAppName & "\Installs\"
strTicketNumber = "7146"

Yearfrom = Year(Date)
Monthfrom = Month(Date)
Dayfrom = Day(Date)
strDate = Yearfrom & "_" & Monthfrom & "-" & Dayfrom
InstallPath = strDate & "_Ticket_" & strTicketNumber

Set objFSO = CreateObject("Scripting.FileSystemObject")
IF NOT objFSO.FolderExists(DestinationPath & InstallPath) Then
objFSO.CreateFolder(DestinationPath & InstallPath)
End IF

Sub1 = DestinationPath & InstallPath & "\D_Project\"
IF NOT objFSO.FolderExists(Sub1) Then
objFSO.CreateFolder(Sub1)
End IF

Sub2 = Sub1 & strAppName
IF NOT objFSO.FolderExists(Sub2) Then
objFSO.CreateFolder(Sub2)
End IF

CodeSub1 = "\Teamsite\"

Sub3 = Sub2 & CodeSub1
IF NOT objFSO.FolderExists(Sub3) Then
objFSO.CreateFolder(Sub3)
End IF

strCMD = "EXPLORER.EXE"
Set objShell = CreateObject("WScript.Shell")
Set objWshScriptExec = objShell.Exec (strCMD & " " & Sub3)

Set objFSO = nothing
Set objShell = nothing
Set objWshScriptExec = nothing
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The script works fine, but I'm wondering if there's a cleaner way to
create all the subfolders:
\2006_2-22_Ticket_7146\D_Project\Webteam\Teamsite

Can I create those subfolders at multiple levels in one fell swoop,
versus having to repeat the "objFSO.CreateFolder" block of code several
times:

IF NOT objFSO.FolderExists(Sub3) Then
objFSO.CreateFolder(Sub3)
End IF

with the different variables Sub1, Sub2, and Sub3?

Any suggestions would be greatly appreciated. Thanks!

Re: Creating subfolders at multiple levels by maximillianx

maximillianx
Wed Feb 22 15:42:45 CST 2006

Hi there - Here is what I use, it works pretty well:

Just call like this:

MakeSureDireectoryTreeExists("c:\one\two\three")

' The MakeSureDirectoryTreeExists Function

' Although the FSO model doesn't have a direct method to create nested
' folders, you can use the following function. This VBScript function uses
' VBScript's Split function to break the folder path it receives into
' components. From those components, the MakeSureDirectoryTreeExists
' creates subfolders, one at a time. Because the function checks for the
' folder's existence before proceeding, you can pass it any tree, as long as
' you make sure that, after it returns, the entire tree exists as you
specified.
' With the MakeSureDirectoryTreeExists function, a call such as

' MakeSureDirectoryTreeExists "C:\one\two\three"

' is legitimate and won't result in an error message.

Function MakeSureDirectoryTreeExists(dirName)
Dim aFolders, newFolder
dim delim
' Creates the FSO object.
Set fso = CreateObject("Scripting.FileSystemObject")

' Checks the folder's existence.
If Not fso.FolderExists(dirName) Then

' Splits the various components of the folder name.
If instr(dirname,"\\") then
'I set up this delimiter to handle UNC paths
delim = "-_-_-_-"
dirname = replace(dirname,"\\",delim)
'wscript.echo dirname
End if

aFolders = split(dirName, "\")

If InStr(dirname,delim) Then
dirname = replace(aFolders(0),delim,"\\")
'wscript.echo "aFolders = " & dirname
End if

' Obtains the drive's root folder.

newFolder = fso.BuildPath(dirname, "\")

' Scans each component in the array, and create the appropriate folder.
For i=1 to UBound(aFolders)
If IntDebug = 1 Then
strTempVar = "Checking to see if " & newfolder _
& " exists..."
Call writedebug(strtempvar)
End If
newFolder = fso.BuildPath(newFolder, aFolders(i))
'wscript.echo newfolder


If Not fso.FolderExists(newFolder) Then
If IntDebug = 1 Then
strTempVar = "Creating " & newfolder
Call writedebug(strTempVar)
End If
fso.CreateFolder newFolder
err.clear
End If
Next
End If
End Function

"Highlander" <tron9901@msn.com> wrote in message
news:1140625664.692396.125980@g44g2000cwa.googlegroups.com...
> Hello.
>
> I've got a script that creates subfolders at multiple levels, on a
> remote server:
>
> (watch for word-wrap)
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Dim strDestinationServer, strAppName, DestinationPath, strTicketNumber
> Dim Yearfrom, Monthfrom, Dayfrom, strDate, InstallPath, Sub1, Sub2,
> CodeSub1, Sub3
>
> strDestinationServer = "000-SERVER-05"
> strAppName = "Webteam"
> DestinationPath = "\\" & strDestinationServer & "\D$\Install\" &
> strAppName & "\Installs\"
> strTicketNumber = "7146"
>
> Yearfrom = Year(Date)
> Monthfrom = Month(Date)
> Dayfrom = Day(Date)
> strDate = Yearfrom & "_" & Monthfrom & "-" & Dayfrom
> InstallPath = strDate & "_Ticket_" & strTicketNumber
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> IF NOT objFSO.FolderExists(DestinationPath & InstallPath) Then
> objFSO.CreateFolder(DestinationPath & InstallPath)
> End IF
>
> Sub1 = DestinationPath & InstallPath & "\D_Project\"
> IF NOT objFSO.FolderExists(Sub1) Then
> objFSO.CreateFolder(Sub1)
> End IF
>
> Sub2 = Sub1 & strAppName
> IF NOT objFSO.FolderExists(Sub2) Then
> objFSO.CreateFolder(Sub2)
> End IF
>
> CodeSub1 = "\Teamsite\"
>
> Sub3 = Sub2 & CodeSub1
> IF NOT objFSO.FolderExists(Sub3) Then
> objFSO.CreateFolder(Sub3)
> End IF
>
> strCMD = "EXPLORER.EXE"
> Set objShell = CreateObject("WScript.Shell")
> Set objWshScriptExec = objShell.Exec (strCMD & " " & Sub3)
>
> Set objFSO = nothing
> Set objShell = nothing
> Set objWshScriptExec = nothing
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> The script works fine, but I'm wondering if there's a cleaner way to
> create all the subfolders:
> \2006_2-22_Ticket_7146\D_Project\Webteam\Teamsite
>
> Can I create those subfolders at multiple levels in one fell swoop,
> versus having to repeat the "objFSO.CreateFolder" block of code several
> times:
>
> IF NOT objFSO.FolderExists(Sub3) Then
> objFSO.CreateFolder(Sub3)
> End IF
>
> with the different variables Sub1, Sub2, and Sub3?
>
> Any suggestions would be greatly appreciated. Thanks!
>



Re: Creating subfolders at multiple levels by maximillianx

maximillianx
Wed Feb 22 15:46:47 CST 2006

Whoops - remove any reference to 'writedebug' - I pulled it out of my code,
and forgot that was there!

Rob

"maximillianx" <u1p2p3h4o5l6d72001@hotmail.com> wrote in message
news:OVGjpj$NGHA.532@TK2MSFTNGP15.phx.gbl...
> Hi there - Here is what I use, it works pretty well:
>
> Just call like this:
>
> MakeSureDireectoryTreeExists("c:\one\two\three")
>
> ' The MakeSureDirectoryTreeExists Function
>
> ' Although the FSO model doesn't have a direct method to create nested
> ' folders, you can use the following function. This VBScript function uses
> ' VBScript's Split function to break the folder path it receives into
> ' components. From those components, the MakeSureDirectoryTreeExists
> ' creates subfolders, one at a time. Because the function checks for the
> ' folder's existence before proceeding, you can pass it any tree, as long
> as
> ' you make sure that, after it returns, the entire tree exists as you
> specified.
> ' With the MakeSureDirectoryTreeExists function, a call such as
>
> ' MakeSureDirectoryTreeExists "C:\one\two\three"
>
> ' is legitimate and won't result in an error message.
>
> Function MakeSureDirectoryTreeExists(dirName)
> Dim aFolders, newFolder
> dim delim
> ' Creates the FSO object.
> Set fso = CreateObject("Scripting.FileSystemObject")
>
> ' Checks the folder's existence.
> If Not fso.FolderExists(dirName) Then
>
> ' Splits the various components of the folder name.
> If instr(dirname,"\\") then
> 'I set up this delimiter to handle UNC paths
> delim = "-_-_-_-"
> dirname = replace(dirname,"\\",delim)
> 'wscript.echo dirname
> End if
>
> aFolders = split(dirName, "\")
>
> If InStr(dirname,delim) Then
> dirname = replace(aFolders(0),delim,"\\")
> 'wscript.echo "aFolders = " & dirname
> End if
>
> ' Obtains the drive's root folder.
>
> newFolder = fso.BuildPath(dirname, "\")
>
> ' Scans each component in the array, and create the appropriate folder.
> For i=1 to UBound(aFolders)
> If IntDebug = 1 Then
> strTempVar = "Checking to see if " & newfolder _
> & " exists..."
> Call writedebug(strtempvar)
> End If
> newFolder = fso.BuildPath(newFolder, aFolders(i))
> 'wscript.echo newfolder
>
>
> If Not fso.FolderExists(newFolder) Then
> If IntDebug = 1 Then
> strTempVar = "Creating " & newfolder
> Call writedebug(strTempVar)
> End If
> fso.CreateFolder newFolder
> err.clear
> End If
> Next
> End If
> End Function
>
> "Highlander" <tron9901@msn.com> wrote in message
> news:1140625664.692396.125980@g44g2000cwa.googlegroups.com...
>> Hello.
>>
>> I've got a script that creates subfolders at multiple levels, on a
>> remote server:
>>
>> (watch for word-wrap)
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> Dim strDestinationServer, strAppName, DestinationPath, strTicketNumber
>> Dim Yearfrom, Monthfrom, Dayfrom, strDate, InstallPath, Sub1, Sub2,
>> CodeSub1, Sub3
>>
>> strDestinationServer = "000-SERVER-05"
>> strAppName = "Webteam"
>> DestinationPath = "\\" & strDestinationServer & "\D$\Install\" &
>> strAppName & "\Installs\"
>> strTicketNumber = "7146"
>>
>> Yearfrom = Year(Date)
>> Monthfrom = Month(Date)
>> Dayfrom = Day(Date)
>> strDate = Yearfrom & "_" & Monthfrom & "-" & Dayfrom
>> InstallPath = strDate & "_Ticket_" & strTicketNumber
>>
>> Set objFSO = CreateObject("Scripting.FileSystemObject")
>> IF NOT objFSO.FolderExists(DestinationPath & InstallPath) Then
>> objFSO.CreateFolder(DestinationPath & InstallPath)
>> End IF
>>
>> Sub1 = DestinationPath & InstallPath & "\D_Project\"
>> IF NOT objFSO.FolderExists(Sub1) Then
>> objFSO.CreateFolder(Sub1)
>> End IF
>>
>> Sub2 = Sub1 & strAppName
>> IF NOT objFSO.FolderExists(Sub2) Then
>> objFSO.CreateFolder(Sub2)
>> End IF
>>
>> CodeSub1 = "\Teamsite\"
>>
>> Sub3 = Sub2 & CodeSub1
>> IF NOT objFSO.FolderExists(Sub3) Then
>> objFSO.CreateFolder(Sub3)
>> End IF
>>
>> strCMD = "EXPLORER.EXE"
>> Set objShell = CreateObject("WScript.Shell")
>> Set objWshScriptExec = objShell.Exec (strCMD & " " & Sub3)
>>
>> Set objFSO = nothing
>> Set objShell = nothing
>> Set objWshScriptExec = nothing
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>> The script works fine, but I'm wondering if there's a cleaner way to
>> create all the subfolders:
>> \2006_2-22_Ticket_7146\D_Project\Webteam\Teamsite
>>
>> Can I create those subfolders at multiple levels in one fell swoop,
>> versus having to repeat the "objFSO.CreateFolder" block of code several
>> times:
>>
>> IF NOT objFSO.FolderExists(Sub3) Then
>> objFSO.CreateFolder(Sub3)
>> End IF
>>
>> with the different variables Sub1, Sub2, and Sub3?
>>
>> Any suggestions would be greatly appreciated. Thanks!
>>
>
>



Re: Creating subfolders at multiple levels by Carl

Carl
Wed Feb 22 15:54:30 CST 2006

You seem to know a few things - any idea how to do that with compressed folders?
(aka zip file, not compress a folder) - If so, have a look at subject:
"createing subdirs in a compressed folder (zip)" from a few days ago.

Thanks, Carl

maximillianx wrote:
> Hi there - Here is what I use, it works pretty well:
>
> Just call like this:
>
> MakeSureDireectoryTreeExists("c:\one\two\three")
>
> ' The MakeSureDirectoryTreeExists Function
>
> ' Although the FSO model doesn't have a direct method to create nested
> ' folders, you can use the following function. This VBScript function uses
> ' VBScript's Split function to break the folder path it receives into
> ' components. From those components, the MakeSureDirectoryTreeExists
> ' creates subfolders, one at a time. Because the function checks for the
> ' folder's existence before proceeding, you can pass it any tree, as long as
> ' you make sure that, after it returns, the entire tree exists as you
> specified.
> ' With the MakeSureDirectoryTreeExists function, a call such as
>
> ' MakeSureDirectoryTreeExists "C:\one\two\three"
>
> ' is legitimate and won't result in an error message.
>
> Function MakeSureDirectoryTreeExists(dirName)
> Dim aFolders, newFolder
> dim delim
> ' Creates the FSO object.
> Set fso = CreateObject("Scripting.FileSystemObject")
>
> ' Checks the folder's existence.
> If Not fso.FolderExists(dirName) Then
>
> ' Splits the various components of the folder name.
> If instr(dirname,"\\") then
> 'I set up this delimiter to handle UNC paths
> delim = "-_-_-_-"
> dirname = replace(dirname,"\\",delim)
> 'wscript.echo dirname
> End if
>
> aFolders = split(dirName, "\")
>
> If InStr(dirname,delim) Then
> dirname = replace(aFolders(0),delim,"\\")
> 'wscript.echo "aFolders = " & dirname
> End if
>
> ' Obtains the drive's root folder.
>
> newFolder = fso.BuildPath(dirname, "\")
>
> ' Scans each component in the array, and create the appropriate folder.
> For i=1 to UBound(aFolders)
> If IntDebug = 1 Then
> strTempVar = "Checking to see if " & newfolder _
> & " exists..."
> Call writedebug(strtempvar)
> End If
> newFolder = fso.BuildPath(newFolder, aFolders(i))
> 'wscript.echo newfolder
>
>
> If Not fso.FolderExists(newFolder) Then
> If IntDebug = 1 Then
> strTempVar = "Creating " & newfolder
> Call writedebug(strTempVar)
> End If
> fso.CreateFolder newFolder
> err.clear
> End If
> Next
> End If
> End Function
>
> "Highlander" <tron9901@msn.com> wrote in message
> news:1140625664.692396.125980@g44g2000cwa.googlegroups.com...
>
>>Hello.
>>
>>I've got a script that creates subfolders at multiple levels, on a
>>remote server:
>>
>>(watch for word-wrap)
>>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>Dim strDestinationServer, strAppName, DestinationPath, strTicketNumber
>>Dim Yearfrom, Monthfrom, Dayfrom, strDate, InstallPath, Sub1, Sub2,
>>CodeSub1, Sub3
>>
>>strDestinationServer = "000-SERVER-05"
>>strAppName = "Webteam"
>>DestinationPath = "\\" & strDestinationServer & "\D$\Install\" &
>>strAppName & "\Installs\"
>>strTicketNumber = "7146"
>>
>>Yearfrom = Year(Date)
>>Monthfrom = Month(Date)
>>Dayfrom = Day(Date)
>>strDate = Yearfrom & "_" & Monthfrom & "-" & Dayfrom
>>InstallPath = strDate & "_Ticket_" & strTicketNumber
>>
>>Set objFSO = CreateObject("Scripting.FileSystemObject")
>> IF NOT objFSO.FolderExists(DestinationPath & InstallPath) Then
>> objFSO.CreateFolder(DestinationPath & InstallPath)
>> End IF
>>
>>Sub1 = DestinationPath & InstallPath & "\D_Project\"
>> IF NOT objFSO.FolderExists(Sub1) Then
>> objFSO.CreateFolder(Sub1)
>> End IF
>>
>>Sub2 = Sub1 & strAppName
>> IF NOT objFSO.FolderExists(Sub2) Then
>> objFSO.CreateFolder(Sub2)
>> End IF
>>
>>CodeSub1 = "\Teamsite\"
>>
>>Sub3 = Sub2 & CodeSub1
>> IF NOT objFSO.FolderExists(Sub3) Then
>> objFSO.CreateFolder(Sub3)
>> End IF
>>
>>strCMD = "EXPLORER.EXE"
>>Set objShell = CreateObject("WScript.Shell")
>>Set objWshScriptExec = objShell.Exec (strCMD & " " & Sub3)
>>
>>Set objFSO = nothing
>>Set objShell = nothing
>>Set objWshScriptExec = nothing
>>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>>The script works fine, but I'm wondering if there's a cleaner way to
>>create all the subfolders:
>>\2006_2-22_Ticket_7146\D_Project\Webteam\Teamsite
>>
>>Can I create those subfolders at multiple levels in one fell swoop,
>>versus having to repeat the "objFSO.CreateFolder" block of code several
>>times:
>>
>> IF NOT objFSO.FolderExists(Sub3) Then
>> objFSO.CreateFolder(Sub3)
>> End IF
>>
>>with the different variables Sub1, Sub2, and Sub3?
>>
>>Any suggestions would be greatly appreciated. Thanks!
>>
>
>
>

RE: Creating subfolders at multiple levels by jefrie

jefrie
Thu Feb 23 01:37:01 CST 2006

In my opinion your way to create the path is quite good.
Here is a function i wrote some time ago. Maybe you can use it.


WScript.Echo "" & createPath("C:\1\2\3\4\5")


Function createPath(strPath)
Set objFS = CreateObject("Scripting.FileSystemObject")
arrTemp = Split(strPath,"\")
strTempPath = arrTemp(0)
For i=1 To UBound(arrTemp)
If(Not(objFS.FolderExists(strTempPath & "\" & arrTemp(i)))) Then
objFS.CreateFolder strTempPath & "\" & arrTemp(i)
If(Err.number<>0) Then
createPath = False
Set objFS = Nothing
Exit Function
End If
End If
strTempPath = strTempPath & "\" & arrTemp(i)
Next
createPath = True
Set objFS = Nothing
End Function

--
Jens Frieben (Germany)


"Highlander" wrote:

> Hello.
>
> I've got a script that creates subfolders at multiple levels, on a
> remote server:
>
> (watch for word-wrap)
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Dim strDestinationServer, strAppName, DestinationPath, strTicketNumber
> Dim Yearfrom, Monthfrom, Dayfrom, strDate, InstallPath, Sub1, Sub2,
> CodeSub1, Sub3
>
> strDestinationServer = "000-SERVER-05"
> strAppName = "Webteam"
> DestinationPath = "\\" & strDestinationServer & "\D$\Install\" &
> strAppName & "\Installs\"
> strTicketNumber = "7146"
>
> Yearfrom = Year(Date)
> Monthfrom = Month(Date)
> Dayfrom = Day(Date)
> strDate = Yearfrom & "_" & Monthfrom & "-" & Dayfrom
> InstallPath = strDate & "_Ticket_" & strTicketNumber
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> IF NOT objFSO.FolderExists(DestinationPath & InstallPath) Then
> objFSO.CreateFolder(DestinationPath & InstallPath)
> End IF
>
> Sub1 = DestinationPath & InstallPath & "\D_Project\"
> IF NOT objFSO.FolderExists(Sub1) Then
> objFSO.CreateFolder(Sub1)
> End IF
>
> Sub2 = Sub1 & strAppName
> IF NOT objFSO.FolderExists(Sub2) Then
> objFSO.CreateFolder(Sub2)
> End IF
>
> CodeSub1 = "\Teamsite\"
>
> Sub3 = Sub2 & CodeSub1
> IF NOT objFSO.FolderExists(Sub3) Then
> objFSO.CreateFolder(Sub3)
> End IF
>
> strCMD = "EXPLORER.EXE"
> Set objShell = CreateObject("WScript.Shell")
> Set objWshScriptExec = objShell.Exec (strCMD & " " & Sub3)
>
> Set objFSO = nothing
> Set objShell = nothing
> Set objWshScriptExec = nothing
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> The script works fine, but I'm wondering if there's a cleaner way to
> create all the subfolders:
> \2006_2-22_Ticket_7146\D_Project\Webteam\Teamsite
>
> Can I create those subfolders at multiple levels in one fell swoop,
> versus having to repeat the "objFSO.CreateFolder" block of code several
> times:
>
> IF NOT objFSO.FolderExists(Sub3) Then
> objFSO.CreateFolder(Sub3)
> End IF
>
> with the different variables Sub1, Sub2, and Sub3?
>
> Any suggestions would be greatly appreciated. Thanks!
>
>

Re: Creating subfolders at multiple levels by Dr

Dr
Thu Feb 23 09:22:10 CST 2006

JRS: In article <1140625664.692396.125980@g44g2000cwa.googlegroups.com>
, dated Wed, 22 Feb 2006 08:27:44 remote, seen in news:microsoft.public.
scripting.vbscript, Highlander <tron9901@msn.com> posted :
>
>Yearfrom = Year(Date)
>Monthfrom = Month(Date)
>Dayfrom = Day(Date)
>strDate = Yearfrom & "_" & Monthfrom & "-" & Dayfrom
>InstallPath = strDate & "_Ticket_" & strTicketNumber
>

Writing numeric dates and times with variable-length fields is bad
practice. Follow ISO 8601, and put a leading zero on month and day
numbers less than ten. Continue to eschew FFF.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

Re: Creating subfolders at multiple levels by Highlander

Highlander
Thu Feb 23 13:37:42 CST 2006

Rob - your MakeSureDirectoryTreeExists Function is awesome! Exactly
what I needed, thanks!

When I test the Function in a standalone script - where I define the
path in a variable, it works great:

path = "c:\one\two\three"
call MakeSureDirectoryTreeExists(path)
Function MakeSureDirectoryTreeExists(dirName)

And I'm also able to use the Function to process multiple paths that
are in a text file:

Const ForReading = 1
Dim path
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("PathNames.txt", ForReading)
Do
path= objTextFile.ReadLine
MakeSureDirectoryTreeExists (path)
Loop until objTextFile.atEndofStream
Function MakeSureDirectoryTreeExists(dirName)

But I've run into a snag. I'm trying to get the Function to work in an
HTA, where the value of the variable "path" is obtained from a
textarea, and it's not working:

Dim path
path = document.all.textarea.value
Set textstream = objEntry.OpenAsTextStream(path, ForReading)
Do
path = textstream.ReadLine
MakeSureDirectoryTreeExists (path)
Loop until objTextFile.atEndofStream
Function MakeSureDirectoryTreeExists(dirName)

The textarea input includes multiple lines, and looks like this:

c:\one\two\three
c:\one\two\three\four
c:\one\two\three\four\five

My code for this Do Loop is not correct. Do you know how I can
configure a Do Loop to read each line of the textarea value and have
your Function create the directory tree? Any suggestions would be
greatly appreciated. Thanks!

- Dave

maximillianx wrote:
> Hi there - Here is what I use, it works pretty well:
>
> Just call like this:
>
> MakeSureDireectoryTreeExists("c:\one\two\three")
>
> ' The MakeSureDirectoryTreeExists Function
>
> ' Although the FSO model doesn't have a direct method to create nested
> ' folders, you can use the following function. This VBScript function uses
> ' VBScript's Split function to break the folder path it receives into
> ' components. From those components, the MakeSureDirectoryTreeExists
> ' creates subfolders, one at a time. Because the function checks for the
> ' folder's existence before proceeding, you can pass it any tree, as long as
> ' you make sure that, after it returns, the entire tree exists as you
> specified.
> ' With the MakeSureDirectoryTreeExists function, a call such as
>
> ' MakeSureDirectoryTreeExists "C:\one\two\three"
>
> ' is legitimate and won't result in an error message.
>
> Function MakeSureDirectoryTreeExists(dirName)
> Dim aFolders, newFolder
> dim delim
> ' Creates the FSO object.
> Set fso = CreateObject("Scripting.FileSystemObject")
>
> ' Checks the folder's existence.
> If Not fso.FolderExists(dirName) Then
>
> ' Splits the various components of the folder name.
> If instr(dirname,"\\") then
> 'I set up this delimiter to handle UNC paths
> delim = "-_-_-_-"
> dirname = replace(dirname,"\\",delim)
> 'wscript.echo dirname
> End if
>
> aFolders = split(dirName, "\")
>
> If InStr(dirname,delim) Then
> dirname = replace(aFolders(0),delim,"\\")
> 'wscript.echo "aFolders = " & dirname
> End if
>
> ' Obtains the drive's root folder.
>
> newFolder = fso.BuildPath(dirname, "\")
>
> ' Scans each component in the array, and create the appropriate folder.
> For i=1 to UBound(aFolders)
> If IntDebug = 1 Then
> strTempVar = "Checking to see if " & newfolder _
> & " exists..."
> Call writedebug(strtempvar)
> End If
> newFolder = fso.BuildPath(newFolder, aFolders(i))
> 'wscript.echo newfolder
>
>
> If Not fso.FolderExists(newFolder) Then
> If IntDebug = 1 Then
> strTempVar = "Creating " & newfolder
> Call writedebug(strTempVar)
> End If
> fso.CreateFolder newFolder
> err.clear
> End If
> Next
> End If
> End Function
>
> "Highlander" <tron9901@msn.com> wrote in message
> news:1140625664.692396.125980@g44g2000cwa.googlegroups.com...
> > Hello.
> >
> > I've got a script that creates subfolders at multiple levels, on a
> > remote server:
> >
> > (watch for word-wrap)
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > Dim strDestinationServer, strAppName, DestinationPath, strTicketNumber
> > Dim Yearfrom, Monthfrom, Dayfrom, strDate, InstallPath, Sub1, Sub2,
> > CodeSub1, Sub3
> >
> > strDestinationServer = "000-SERVER-05"
> > strAppName = "Webteam"
> > DestinationPath = "\\" & strDestinationServer & "\D$\Install\" &
> > strAppName & "\Installs\"
> > strTicketNumber = "7146"
> >
> > Yearfrom = Year(Date)
> > Monthfrom = Month(Date)
> > Dayfrom = Day(Date)
> > strDate = Yearfrom & "_" & Monthfrom & "-" & Dayfrom
> > InstallPath = strDate & "_Ticket_" & strTicketNumber
> >
> > Set objFSO = CreateObject("Scripting.FileSystemObject")
> > IF NOT objFSO.FolderExists(DestinationPath & InstallPath) Then
> > objFSO.CreateFolder(DestinationPath & InstallPath)
> > End IF
> >
> > Sub1 = DestinationPath & InstallPath & "\D_Project\"
> > IF NOT objFSO.FolderExists(Sub1) Then
> > objFSO.CreateFolder(Sub1)
> > End IF
> >
> > Sub2 = Sub1 & strAppName
> > IF NOT objFSO.FolderExists(Sub2) Then
> > objFSO.CreateFolder(Sub2)
> > End IF
> >
> > CodeSub1 = "\Teamsite\"
> >
> > Sub3 = Sub2 & CodeSub1
> > IF NOT objFSO.FolderExists(Sub3) Then
> > objFSO.CreateFolder(Sub3)
> > End IF
> >
> > strCMD = "EXPLORER.EXE"
> > Set objShell = CreateObject("WScript.Shell")
> > Set objWshScriptExec = objShell.Exec (strCMD & " " & Sub3)
> >
> > Set objFSO = nothing
> > Set objShell = nothing
> > Set objWshScriptExec = nothing
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >
> > The script works fine, but I'm wondering if there's a cleaner way to
> > create all the subfolders:
> > \2006_2-22_Ticket_7146\D_Project\Webteam\Teamsite
> >
> > Can I create those subfolders at multiple levels in one fell swoop,
> > versus having to repeat the "objFSO.CreateFolder" block of code several
> > times:
> >
> > IF NOT objFSO.FolderExists(Sub3) Then
> > objFSO.CreateFolder(Sub3)
> > End IF
> >
> > with the different variables Sub1, Sub2, and Sub3?
> >
> > Any suggestions would be greatly appreciated. Thanks!
> >


Re: Creating subfolders at multiple levels by maximillianx

maximillianx
Thu Feb 23 14:58:22 CST 2006

Could you do a .readall method? You could parse against vbnewline
I don't know if this code is 100%, I just kinda whipped it up, hopefully you
can troubleshoot :)

Set textstream = objEntry.OpenAsTextStream(path, ForReading)
patharray = split(textstream.ReadAll,vbnewline)

for i = 0 to ubound patharray
msgbox "Processing " & trim(patharray(i))
MakeSureDirectoryTreeExists (trim(patharray(i)))
next

Oh, and to clarify, that function isn't mine, I hoisted it from a script
site somewhere...but I did modify it to work with long filenames/paths with
spaces in it, etc... :), so I guess a very small part of it is mine!

Rob


"Highlander" <tron9901@msn.com> wrote in message
news:1140723462.325695.123020@i40g2000cwc.googlegroups.com...
> Rob - your MakeSureDirectoryTreeExists Function is awesome! Exactly
> what I needed, thanks!
>
> When I test the Function in a standalone script - where I define the
> path in a variable, it works great:
>
> path = "c:\one\two\three"
> call MakeSureDirectoryTreeExists(path)
> Function MakeSureDirectoryTreeExists(dirName)
>
> And I'm also able to use the Function to process multiple paths that
> are in a text file:
>
> Const ForReading = 1
> Dim path
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objTextFile = objFSO.OpenTextFile("PathNames.txt", ForReading)
> Do
> path= objTextFile.ReadLine
> MakeSureDirectoryTreeExists (path)
> Loop until objTextFile.atEndofStream
> Function MakeSureDirectoryTreeExists(dirName)
>
> But I've run into a snag. I'm trying to get the Function to work in an
> HTA, where the value of the variable "path" is obtained from a
> textarea, and it's not working:
>
> Dim path
> path = document.all.textarea.value
> Set textstream = objEntry.OpenAsTextStream(path, ForReading)
> Do
> path = textstream.ReadLine
> MakeSureDirectoryTreeExists (path)
> Loop until objTextFile.atEndofStream
> Function MakeSureDirectoryTreeExists(dirName)
>
> The textarea input includes multiple lines, and looks like this:
>
> c:\one\two\three
> c:\one\two\three\four
> c:\one\two\three\four\five
>
> My code for this Do Loop is not correct. Do you know how I can
> configure a Do Loop to read each line of the textarea value and have
> your Function create the directory tree? Any suggestions would be
> greatly appreciated. Thanks!
>
> - Dave
>
> maximillianx wrote:
>> Hi there - Here is what I use, it works pretty well:
>>
>> Just call like this:
>>
>> MakeSureDireectoryTreeExists("c:\one\two\three")
>>
>> ' The MakeSureDirectoryTreeExists Function
>>
>> ' Although the FSO model doesn't have a direct method to create nested
>> ' folders, you can use the following function. This VBScript function
>> uses
>> ' VBScript's Split function to break the folder path it receives into
>> ' components. From those components, the MakeSureDirectoryTreeExists
>> ' creates subfolders, one at a time. Because the function checks for the
>> ' folder's existence before proceeding, you can pass it any tree, as long
>> as
>> ' you make sure that, after it returns, the entire tree exists as you
>> specified.
>> ' With the MakeSureDirectoryTreeExists function, a call such as
>>
>> ' MakeSureDirectoryTreeExists "C:\one\two\three"
>>
>> ' is legitimate and won't result in an error message.
>>
>> Function MakeSureDirectoryTreeExists(dirName)
>> Dim aFolders, newFolder
>> dim delim
>> ' Creates the FSO object.
>> Set fso = CreateObject("Scripting.FileSystemObject")
>>
>> ' Checks the folder's existence.
>> If Not fso.FolderExists(dirName) Then
>>
>> ' Splits the various components of the folder name.
>> If instr(dirname,"\\") then
>> 'I set up this delimiter to handle UNC paths
>> delim = "-_-_-_-"
>> dirname = replace(dirname,"\\",delim)
>> 'wscript.echo dirname
>> End if
>>
>> aFolders = split(dirName, "\")
>>
>> If InStr(dirname,delim) Then
>> dirname = replace(aFolders(0),delim,"\\")
>> 'wscript.echo "aFolders = " & dirname
>> End if
>>
>> ' Obtains the drive's root folder.
>>
>> newFolder = fso.BuildPath(dirname, "\")
>>
>> ' Scans each component in the array, and create the appropriate folder.
>> For i=1 to UBound(aFolders)
>> If IntDebug = 1 Then
>> strTempVar = "Checking to see if " & newfolder _
>> & " exists..."
>> Call writedebug(strtempvar)
>> End If
>> newFolder = fso.BuildPath(newFolder, aFolders(i))
>> 'wscript.echo newfolder
>>
>>
>> If Not fso.FolderExists(newFolder) Then
>> If IntDebug = 1 Then
>> strTempVar = "Creating " & newfolder
>> Call writedebug(strTempVar)
>> End If
>> fso.CreateFolder newFolder
>> err.clear
>> End If
>> Next
>> End If
>> End Function
>>
>> "Highlander" <tron9901@msn.com> wrote in message
>> news:1140625664.692396.125980@g44g2000cwa.googlegroups.com...
>> > Hello.
>> >
>> > I've got a script that creates subfolders at multiple levels, on a
>> > remote server:
>> >
>> > (watch for word-wrap)
>> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> > Dim strDestinationServer, strAppName, DestinationPath, strTicketNumber
>> > Dim Yearfrom, Monthfrom, Dayfrom, strDate, InstallPath, Sub1, Sub2,
>> > CodeSub1, Sub3
>> >
>> > strDestinationServer = "000-SERVER-05"
>> > strAppName = "Webteam"
>> > DestinationPath = "\\" & strDestinationServer & "\D$\Install\" &
>> > strAppName & "\Installs\"
>> > strTicketNumber = "7146"
>> >
>> > Yearfrom = Year(Date)
>> > Monthfrom = Month(Date)
>> > Dayfrom = Day(Date)
>> > strDate = Yearfrom & "_" & Monthfrom & "-" & Dayfrom
>> > InstallPath = strDate & "_Ticket_" & strTicketNumber
>> >
>> > Set objFSO = CreateObject("Scripting.FileSystemObject")
>> > IF NOT objFSO.FolderExists(DestinationPath & InstallPath) Then
>> > objFSO.CreateFolder(DestinationPath & InstallPath)
>> > End IF
>> >
>> > Sub1 = DestinationPath & InstallPath & "\D_Project\"
>> > IF NOT objFSO.FolderExists(Sub1) Then
>> > objFSO.CreateFolder(Sub1)
>> > End IF
>> >
>> > Sub2 = Sub1 & strAppName
>> > IF NOT objFSO.FolderExists(Sub2) Then
>> > objFSO.CreateFolder(Sub2)
>> > End IF
>> >
>> > CodeSub1 = "\Teamsite\"
>> >
>> > Sub3 = Sub2 & CodeSub1
>> > IF NOT objFSO.FolderExists(Sub3) Then
>> > objFSO.CreateFolder(Sub3)
>> > End IF
>> >
>> > strCMD = "EXPLORER.EXE"
>> > Set objShell = CreateObject("WScript.Shell")
>> > Set objWshScriptExec = objShell.Exec (strCMD & " " & Sub3)
>> >
>> > Set objFSO = nothing
>> > Set objShell = nothing
&g