Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create sub folders inside sub folders in VB 2

Status
Not open for further replies.

pujas

Programmer
Feb 2, 2005
22
0
0
US
Hi,
I'am trying the folding in vb but it does not work

Set fs = CreateObject("Scripting.FileSystemObject")
'Folder Client does not exist on C:\
fs.CreateFolder("C:\Client\subclient\subsubclient")

I want to know what function will help me creating subfolders inside sub folders when none of them exist.

I know that the below works
fs.CreateFolder("C:\Client")
but i want to create sub folder inside sub folders.
Is there a fubction do create all folders.
 
It seems like you are trying to implicitly create the subfolders.

Why not do this:
fs.CreateFolder("C:\Client")
fs.CreateFolder("C:\Client\subclient")
fs.CreateFolder("C:\Client\subclient\subsubclient")
 
or this:

Set fld = fs.CreateFolder("C:\Client")
Set sub1 = fld.SubFolders.Add("subclient")
Set sub2 = sub1.SubFolders.Add("subsubclient")
 
Hello pujas!

If you are wanting to dynamically create your subfolders, you could try calling a sub routine such as this:
Code:
Call subCreateFolders("C:\test1\test2\test3\")


Sub subCreateFolders(strPath)
   Dim objFileSys
   Dim strPath, strNewFolder
	
   Set objFileSys = CreateObject("Scripting.FileSystemObject")

   If Right(strPath, 1) <> "\" Then
      strPath = strPath & "\"
   End If

   strNewFolder = ""
   Do Until strPath = strNewFolder
      strNewFolder = Left(strPath, InStr(Len(strNewFolder) + 1, strPath, "\"))
	
      If objFileSys.FolderExists(strNewFolder) = False Then
         objFileSys.CreateFolder(strNewFolder)
      End If
   Loop
End Sub

Good luck!
 
This is exactly what i was looking for.
Thanks a ton!!
 
Or a spinoff of Sheco's code by adding this:
Code:
if not fso.FolderExists("C:\Client") then
  fso.CreateFolder("C:\Client") 
end if
if not fso.FolderExists("C:\Client\subclient") then
  fso.CreateFolder("C:\Client\subclient")
end if
if not fso.FolderExists("C:\Client\subclient\subsubclient") then
  fso.CreateFolder("C:\Client\subclient\subsubclient") 
end if
-SWarrior

 
This is my own version I've alredy posted a while ago:
Sub myCreateFolder(strPath)
tmpArr = Split(strPath, "\")
tmpPath = tmpArr(0)
For i = 1 To UBound(tmpArr)
If Not fso.FolderExists(tmpPath) Then
fso.CreateFolder tmpPath
End If
tmpPath = tmpPath & "\" & tmpArr(i)
Next
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top