Wednesday, January 7, 2009

Create Nested Subfolders in VBScript

Here is the simple vbscript to create nested subfolders depending upon the number entered by the user:


Dim fso,flr,resp,subFoldernum,nestedFoldernum,stuffTowrite,dateStamp,folderPath
dateStamp = Date()Const ForWriting = 8
stuffTowrite = "Whatever you want written " & dateStamp
Set fso = CreateObject("Scripting.FileSystemObject")
resp = InputBox("Enter Folder Name","Create Folder")
subFoldernum = InputBox("Enter Number of Subfolders","Create SubFolder")
If IsNumeric(subFoldernum) then
nestedFoldernum = InputBox("Enter Number of folders you want to create in the Subfolders","Create Nested SubFolders")
If IsNumeric(nestedFoldernum) then
Set Fldr=fso.createfolder(resp)
For x = 1 to subFoldernum
Set Fldr=fso.createfolder(resp & "\SubFolder"&x)
For y = 1 to nestedFoldernum
folderPath=resp& "\SubFolder"&x&"\nested"&y
Set Fldr=fso.createfolder(folderPath)
Set WriteStuff = fso.OpenTextFile(folderPath & "\test.txt", ForWriting,true)
WriteStuff.WriteLine(stuffTowrite)
WriteStuff.Close
Next
Next
else
msgbox("Whatever you submitted,is not a numeric value.")
end if
else
msgbox("Whatever you submitted,is not a numeric value.")
end if

1 comment:

Manoj said...

Thanks...Vinod..It worked for me....Actually It catered my all needs.......