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.
CodePudding user response:
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
'---------------------------------------------------------------