I am unsure as to how to do this without creating a long winded
script.

I want check to see if a directory or 2 directories exist and if they
don't then create them.

ie:

dir1 \ dir2 \ dir 3 \dir 4
\ dir 5


So dir1 exists but 4 & 5 don't and neither do 2 + 3.

The only way to do this that i have found is to create a separate
variable for each directory and then create each separately

oDir1 = c:\dirA\dirB
oDir2 = dirC
oDir3 = dirD

mDir = oDir1 + oDimM
mDir = MDir + oDir3

(just a sample)

Is there a better way of doing this?


jp

Re: how to create directories? by Stefano

Stefano
Wed Jan 05 05:57:28 CST 2005

Hello jp,

In the Filesystemobject you can find something useful for your purpose. The
Method is:
FileSystemObject.CreateFolder


EXAMPLE:

Sub ShowFolderInfo()
Dim fso, fldr, s
' Get instance of FileSystemObject.
Set fso = CreateObject("Scripting.FileSystemObject")
' Get Drive object.
Set fldr = fso.GetFolder("c:")
' Print parent folder name.
Response.Write "Parent folder name is: " & fldr & "<br>"
' Print drive name.
Response.Write "Contained on drive " & fldr.Drive & "<br>"
' Print root file name.
If fldr.IsRootFolder = True Then
Response.Write "This is the root folder." & ""<br>"<br>"
Else
Response.Write "This folder isn't a root folder." & "<br><br>"
End If
' Create a new folder with the FileSystemObject object.
fso.CreateFolder ("C:\Bogus")
Response.Write "Created folder C:\Bogus" & "<br>"
' Print the base name of the folder.
Response.Write "Basename = " & fso.GetBaseName("c:\bogus") & "<br>"
' Delete the newly created folder.
fso.DeleteFolder ("C:\Bogus")
Response.Write "Deleted folder C:\Bogus" & "<br>"
End Sub


> I am unsure as to how to do this without creating a long winded
> script.
>
> I want check to see if a directory or 2 directories exist and if they
> don't then create them.
>
> ie:
>
> dir1 \ dir2 \ dir 3 \dir 4
> \ dir 5
> So dir1 exists but 4 & 5 don't and neither do 2 + 3.
>
> The only way to do this that i have found is to create a separate
> variable for each directory and then create each separately
>
> oDir1 = c:\dirA\dirB
> oDir2 = dirC
> oDir3 = dirD
> mDir = oDir1 + oDimM
> mDir = MDir + oDir3
> (just a sample)
>
> Is there a better way of doing this?
>
> jp
>



Re: how to create directories? by jp

jp
Wed Jan 05 07:03:56 CST 2005

Thanks - I like the way the code goes, however this still has a
limitation in writing a directory path that doesn't exist if the path
is more than one directory.

ie:

Path that exist might be: H:\MyStuff\2005

Path I am looking for might be: H:\MyStuff\2005\News\Response\Current

So i want it to be able to create at one time the
\News\Response\Current directories in the script

What I have seen so far from - fso.CreateFolder ("C:\Bogus")
is that it can only do that, not create something like -
fso.CreateFolder ("C:\Bogus\News\Response\Current")

Does that make more sense to anyone?


jp



On Wed, 05 Jan 2005 03:57:28 -0800, Stefano
<scecconelloATcdlan.it@Removethis> wrote:

>Hello jp,
>
>In the Filesystemobject you can find something useful for your purpose. The
>Method is:
>FileSystemObject.CreateFolder
>
>
>EXAMPLE:
>
>Sub ShowFolderInfo()
> Dim fso, fldr, s
> ' Get instance of FileSystemObject.
> Set fso = CreateObject("Scripting.FileSystemObject")
> ' Get Drive object.
> Set fldr = fso.GetFolder("c:")
> ' Print parent folder name.
> Response.Write "Parent folder name is: " & fldr & "<br>"
> ' Print drive name.
> Response.Write "Contained on drive " & fldr.Drive & "<br>"
> ' Print root file name.
> If fldr.IsRootFolder = True Then
> Response.Write "This is the root folder." & ""<br>"<br>"
> Else
> Response.Write "This folder isn't a root folder." & "<br><br>"
> End If
> ' Create a new folder with the FileSystemObject object.
> fso.CreateFolder ("C:\Bogus")
> Response.Write "Created folder C:\Bogus" & "<br>"
> ' Print the base name of the folder.
> Response.Write "Basename = " & fso.GetBaseName("c:\bogus") & "<br>"
> ' Delete the newly created folder.
> fso.DeleteFolder ("C:\Bogus")
> Response.Write "Deleted folder C:\Bogus" & "<br>"
>End Sub
>
>
>> I am unsure as to how to do this without creating a long winded
>> script.
>>
>> I want check to see if a directory or 2 directories exist and if they
>> don't then create them.
>>
>> ie:
>>
>> dir1 \ dir2 \ dir 3 \dir 4
>> \ dir 5
>> So dir1 exists but 4 & 5 don't and neither do 2 + 3.
>>
>> The only way to do this that i have found is to create a separate
>> variable for each directory and then create each separately
>>
>> oDir1 = c:\dirA\dirB
>> oDir2 = dirC
>> oDir3 = dirD
>> mDir = oDir1 + oDimM
>> mDir = MDir + oDir3
>> (just a sample)
>>
>> Is there a better way of doing this?
>>
>> jp
>>
>


Re: how to create directories? by Michael

Michael
Wed Jan 05 18:35:48 CST 2005

jp wrote:
> Thanks - I like the way the code goes, however this still has a
> limitation in writing a directory path that doesn't exist if the path
> is more than one directory.

Google Search
http://groups-beta.google.com/groups?q=createfolderpath%20group:*.scripting

--
Michael Harris
Microsoft MVP Scripting



Re: how to create directories? by jp

jp
Thu Jan 06 16:48:17 CST 2005

Thanks - Found this site so supportive of what i am looking for.

On the site found this code...

--- snipit
set fo = CreateFolderPath("c:\_temp\this\that")
wscript.echo fo.path

Function CreateFolderPath(Path)
With CreateObject("Scripting.FileSystemObject")
If not .FolderExists(.GetParentFolderName(Path)) Then
Call CreateFolderPath(.GetParentFolderName(Path))
End If
Set CreateFolderPath = .CreateFolder(Path)
End With
End Function
--- end

This works exactly as I hoped and on its own it is the perfect
solution, however when i put it into another vbs file it FAILS every
time.

I have tried declaring, DIMing but with no success. Not sure what "fo"
is supposed to be and i get errors with "CreateFolderPath".

What am i missing here so that it will work in my scripts?


jp




On Wed, 5 Jan 2005 16:35:48 -0800, "Michael Harris \(MVP\)" <mikhar at
mvps dot org> wrote:

>jp wrote:
>> Thanks - I like the way the code goes, however this still has a
>> limitation in writing a directory path that doesn't exist if the path
>> is more than one directory.
>
>Google Search
>http://groups-beta.google.com/groups?q=createfolderpath%20group:*.scripting


Re: how to create directories? by jp

jp
Sun Jan 09 19:24:38 CST 2005

Well took me a while but i found some resources, tweaked a bit and now
i have a functional VBS (WshShell) working version so i felt to share,
as I found lots of references on GetParentFolderName() and code that
worked when on its own but when combined with

Set WshShell = CreateObject("Wscript.shell")

nothing would seem to work or gave errors. Additionally I could not
find anything concise or directly pointing towards this in a script
fashion using the above.

So hope this helps anyone who has a need to build directory structures
with a mind to using VBS.


Note the DirPath needs to be set like...

DirPath = "H:\dir\dir\sub\sub"


--- snipit
Sub BuildDirectory(ByVal DirPath)
Dim fso
Dim strParentDir

Set fso = CreateObject("Scripting.FileSystemObject")
strParentDir = fso.GetParentFolderName(DirPath)
' Make sure strParentDir has a trailing slash to ensure
' FSO doesn't think strParentDir is a file instead of a
' folder.
If Right(strParentDir, 1) <> "\" Then
strParentDir = strParentDir & "\"
End If
Do While Not fso.FolderExists(DirPath)
If fso.FolderExists(strParentDir) Then
fso.CreateFolder (DirPath)
Else
Call BuildDirectory(strParentDir)
End If
Loop
Set fso = Nothing
Set strParentDir = Nothing
End Sub
--- end code


jp

On Wed, 5 Jan 2005 16:35:48 -0800, "Michael Harris \(MVP\)" <mikhar at
mvps dot org> wrote:

>jp wrote:
>> Thanks - I like the way the code goes, however this still has a
>> limitation in writing a directory path that doesn't exist if the path
>> is more than one directory.
>
>Google Search
>http://groups-beta.google.com/groups?q=createfolderpath%20group:*.scripting


Re: how to create directories? by Michael

Michael
Mon Jan 10 10:16:36 CST 2005

jp wrote:
> Well took me a while but i found some resources, tweaked a bit and now
> i have a functional VBS (WshShell) working version so i felt to share,
> as I found lots of references on GetParentFolderName() and code that
> worked when on its own but when combined with
>
> Set WshShell = CreateObject("Wscript.shell")
>
> nothing would seem to work or gave errors. Additionally I could not
> find anything concise or directly pointing towards this in a script
> fashion using the above.
>
> So hope this helps anyone who has a need to build directory structures
> with a mind to using VBS.
>


How is that different from the CreateFolderPath function I pointed you to?

If you don't want/need a reference to the folder object created then just
change this:

set fo = CreateFolderPath("c:\_temp\this\that")

to this:

Call CreateFolderPath("c:\_temp\this\that")

If you Call a function (as opposed to using it in the RHS of an assignment
statement) that returns a value (a folder object in this case), the returned
value is simply discarded.


>
> Note the DirPath needs to be set like...
>
> DirPath = "H:\dir\dir\sub\sub"
>
>
> --- snipit
> Sub BuildDirectory(ByVal DirPath)
> Dim fso
> Dim strParentDir
>
> Set fso = CreateObject("Scripting.FileSystemObject")
> strParentDir = fso.GetParentFolderName(DirPath)
> ' Make sure strParentDir has a trailing slash to ensure
> ' FSO doesn't think strParentDir is a file instead of a
> ' folder.
> If Right(strParentDir, 1) <> "\" Then
> strParentDir = strParentDir & "\"
> End If
> Do While Not fso.FolderExists(DirPath)
> If fso.FolderExists(strParentDir) Then
> fso.CreateFolder (DirPath)
> Else
> Call BuildDirectory(strParentDir)
> End If
> Loop
> Set fso = Nothing
> Set strParentDir = Nothing
> End Sub
> --- end code
>
>
> jp
>
> On Wed, 5 Jan 2005 16:35:48 -0800, "Michael Harris \(MVP\)" <mikhar at
> mvps dot org> wrote:
>
>> jp wrote:
>>> Thanks - I like the way the code goes, however this still has a
>>> limitation in writing a directory path that doesn't exist if the
>>> path is more than one directory.
>>
>> Google Search
>>
http://groups-beta.google.com/groups?q=createfolderpath%20group:*.scripting

--
Michael Harris
Microsoft.MVP.Scripting