0

I'm a linux guy and new to windows, im trying to create a small vbs script to create bunch of folders. Im trying to create a mainfolder according to user input and 4 subfolder in it with static names. I somehow got two seperate working scripts for mainfolder and subfolder but I would like to combine them both which means once the mainfolder is created as per user input next it should create subfolders below that.

Script which creates Mainfolder.vbs as per user input:

strfolder = InputBox("Please enter a name for your new folder:")
set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CreateFolder "C:\test\" & strfolder

Script which creates subfolders.vbs folders:

Option Explicit

dim wshShell
set wshShell = wscript.CreateObject("WScript.Shell")
DIM fSO

DIM foldername1
DIM foldername2
DIM foldername3
DIM foldername4

foldername1=("subfolder1")
foldername2=("subfolder2")
foldername3=("subfolder3")
foldername4=("subfolder4")

dim folderpath
SET FSO=CreateObject("Scripting.FileSystemObject")

folderpath = "C:\test" & _        
             "\" & foldername1
wscript.echo "Creating folder: " & folderpath
FSO.CREATEFOLDER(folderpath)

folderpath = "C:\test" & _            
             "\" & foldername2 
wscript.echo "Creating folder: " & folderpath
FSO.CREATEFOLDER(folderpath)

folderpath = "C:\test" & _
             "\" & foldername3 
wscript.echo "Creating folder: " & folderpath
FSO.CREATEFOLDER(folderpath) 

folderpath = "C:\test" & _
             "\" & foldername4 
wscript.echo "Creating folder: " & folderpath
FSO.CREATEFOLDER(folderpath)

In short, how can I create a mainfolder as per user input and subfolders with static names?

would like to combine these both and work together as a single script.

aynber
  • 22,380
  • 8
  • 50
  • 63
Vinoth
  • 11
  • 3

1 Answers1

1

You can try with this subroutine : SmartCreateFolder(strFolder)


Sub SmartCreateFolder(strFolder)
    With CreateObject("Scripting.FileSystemObject")
        If Not .FolderExists(strFolder) then
            SmartCreateFolder(.getparentfoldername(strFolder))
            .CreateFolder(strFolder)
        End If
    End With 
End Sub

The combined whole script can be written like that :

Option Explicit
Const Title = "SmartCreateFolder"
Const Time2Wait = 2
Dim ws,MainFolderPath,MainFolderName,SubFolderPath,Arr_SubFolder_Name,i
Set ws  = CreateObject("wscript.shell")
Arr_SubFolder_Name = Array("subfolder1","subfolder2","subfolder3","subfolder4")

MainfolderName = InputBox(_
"Please enter a name for your new folder :",Title,"Type the name here")
If MainfolderName = "" Then Wscript.Quit(1)

MainFolderPath = "C:\test\" & MainfolderName

For i=LBound(Arr_SubFolder_Name) To UBound(Arr_SubFolder_Name)
    Call SmartCreateFolder(MainFolderPath)
    SubFolderPath = MainFolderPath & "\" & Arr_SubFolder_Name(i)
    Call SmartCreateFolder(SubFolderPath) 
Next
ws.run "Explorer " & MainFolderPath
'---------------------------------------------------------------
Sub SmartCreateFolder(strFolder)
    With CreateObject("Scripting.FileSystemObject")
        If Not .FolderExists(strFolder) then
            ws.Popup "Creating folder: " & StrFolder,Time2Wait,_
            Title,vbInformation+vbSystemModal
            SmartCreateFolder(.getparentfoldername(strFolder))
            .CreateFolder(strFolder)
        End If
    End With 
End Sub
'---------------------------------------------------------------
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • 1
    @user692942 Answer is edited refer to your comment ! Thank you ! and have a nice day ! – Hackoo Jun 08 '22 at 15:35
  • 1
    Definitely a better approach. – user692942 Jun 08 '22 at 15:38
  • Thanks a lot for the help. It works seamless. But it overwrites folder which exists. How can I add if condition to stop if folder exists? – Vinoth Jun 09 '22 at 05:23
  • @Vinoth by adding an `If` statement and [checking if the folder exists](https://stackoverflow.com/a/14121914/692942). – user692942 Jun 09 '22 at 07:21
  • @Vinoth Just to be clear the code dosen't overwrite folder which exists, but just repeating the MsgBox. Anyway, i tweaked the code above, just give a try and tell me the result ! So the code is edited in order to add a popup message only for creating new folders. – Hackoo Jun 09 '22 at 12:34
  • @Vinoth Please don't modify anything, just try it as it now ! i edited and the testing if exists or not is located on subroutine ! i tested on my side and it worked 5/5 – Hackoo Jun 10 '22 at 08:40
  • Thank you. It is not recreating the folder but still shows folder created pop ups though the folder is already exist, Is there a way to error out if folder exists? – Vinoth Jun 10 '22 at 08:44
  • adding an if condition was a old comment. please ignore it. thanks – Vinoth Jun 10 '22 at 08:45
  • @Vinoth Try out here : [SmartCreateFolder.vbs](https://pastebin.com/vJAYqS2S) – Hackoo Jun 10 '22 at 09:08
  • So finally it created a mainfolder and subfolders, also It opens existing folder if you try to create it. This looks great.. thank you @Hackoo – Vinoth Jun 10 '22 at 09:44
  • @Vinoth Great !! Please take Stack Overflow (SO) [tour](https://stackoverflow.com/tour) to learn how to say thanks on SO by accepting an answer on being helpful by clicking on gray check mark symbol left to an answer. See also [How does accepting an answer work?](https://meta.stackexchange.com/questions/5234/) A question with no accepted answer is rated as a not answered question even if there are good answers. – Hackoo Jun 10 '22 at 09:46