Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


create a folder with subfolders

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,58712
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> create a folder with subfolders
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 create a folder with subfolders - 4/2/2008 8:38:14 PM   
  lazydev

 

Posts: 21
Score: 0
Joined: 2/21/2008
Status: offline
I need to write a vb script to create a folder depending on user inputs .and it should contain 4 subfolders as folder1 ,folder2,folder3,folder4

I need some help on that.
This is what i have written .this to has some problems


dim Filesys,FolderPath,FolderName, NewFolder, newfolderpath
Wscript.StdOut.WriteLine "Enter your Folder Path and Name : "  
FolderPath = Wscript.StdIn.ReadLine
Wscript.Echo " " & FolderPath &  ""
set Filesys=CreateObject("Scripting.FileSystemObject")
If Not filesys.FolderExists(& FolderPath) Then
Set NewFolder = filesys.CreateFolder(newfolderpath)
Response.Write("A new folder has been created at: " & newfolderpath)
End If


Thanks
Lazydev
 
 
Post #: 1
 
 RE: create a folder with subfolders - 4/2/2008 8:56:07 PM   
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi lazydev,

see

   http://www.visualbasicscript.com/fb.aspx?m=38936

and check your code:

(1) why concat empty string(s) to FolderPath in

        Wscript.Echo " " & FolderPath &  ""

(2) If this

        Wscript.StdOut.WriteLine "Enter your Folder Path and Name : " 

      works, that

       Response.Write("A new folder has been created at: " & newfolderpath)

     shouldn't - and vice versa.

(3) Lazy people may think that indentation/proper code layout is a waste of
      time, but they are wrong.

Good luck

ehvbs

(in reply to lazydev)
 
 
Post #: 2
 
 RE: create a folder with subfolders - 4/2/2008 10:23:39 PM   
  lazydev

 

Posts: 21
Score: 0
Joined: 2/21/2008
Status: offline
This is what i have written  but i am getting wrong at second if loop .I am unable to figure it out.

just help me please

strRoot="D:\Archive\Active"

Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objFolder=objFSO.GetFolder(strRoot)
Set colFolders=objFolder.SubFolders

strFolder = InputBox("Please Enter the FolderName.")

If objFSO.FolderExists(strRoot & strFolder) Then
   WScript.Echo "Folder " & strRoot & strFolder &_  " already exists"
Else
   colFolders.Add strFolder
End If
Wscript.Echo " " & objFolder & "" 
Set objFolder=objFSO.GetFolder(strRoot \ strFolder)
Wscript.Echo " " & objFolder & "" 
Set colFolders=objFolder.SubFolders
arrFolders=Array("Test1","Test2","Test3","Test4")

For Each item In arrFolders
   If objFSO.FolderExists(strRoot & strFolder & "\" & item) Then
       WScript.Echo "Folder " & strRoot & strFolder &_
        "\" & item & " already exists"
   Else
       colFolders.Add item
   End If
NEXT

(in reply to ehvbs)
 
 
Post #: 3
 
 RE: create a folder with subfolders - 4/2/2008 11:35:49 PM   
  lazydev

 

Posts: 21
Score: 0
Joined: 2/21/2008
Status: offline
hey ehvbs

In the above script every thing is upto my requirement .Except this line






Wscript.Echo " " & objFolder & ""  ' i am getting output as D:\Archive\Active
Set objFolder=objFSO.GetFolder(strRoot &  strFolder)
Wscript.Echo " " & objFolder & ""   ' i am getting path not found

i need someone to solve this .I am unable to figure it out.

(in reply to ehvbs)
 
 
Post #: 4
 
 RE: create a folder with subfolders - 4/2/2008 11:57:02 PM   
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi lazydev,

(1) did you look at ebgreen's code? It should make all your loopings unnecessary
     (thereby avoiding all your errors)

(2) given

         strRoot="D:\Archive\Active"
          strFolder = "UsersInput"   ' completely unchecked BTW

     your concatenation

          strRoot & strFolder

     will result in

         "D:\Archive\ActiveUsersInput"  (missing \)

(3) This

        strRoot \ strFolder

   won't give you "D:\Archive\Active\UsersInput" either. (Didn't you get
   an error on this line?)

(4) Just now I'm not very lazy, so here is a clean version of your code - I skipped
    the user input by setting a variable


      

(5) Lazy people should use ebgreen's code:


      

Regards

ehvbs


   

< Message edited by ehvbs -- 4/2/2008 11:58:56 PM >

(in reply to lazydev)
 
 
Post #: 5
 
 RE: create a folder with subfolders - 4/3/2008 12:28:00 AM   
  lazydev

 

Posts: 21
Score: 0
Joined: 2/21/2008
Status: offline
hey you are really genuis

it's working fine



thanx for helping me.I would like to learn from you a lot on scripting


(in reply to ehvbs)
 
 
Post #: 6
 
 RE: create a folder with subfolders - 4/3/2008 2:38:12 AM   
  lazydev

 

Posts: 21
Score: 0
Joined: 2/21/2008
Status: offline
hey ehvbs  (vbguru)

We have given it for a single user input like "ehvbs"

how about to give for multiple names like if a person gives fold1  fold2 fold3 or fold1,fold2,fold3,

it should run a loop  any examples for running the loops for this script.


How About giving linux path in the vbscripts does it works We give asD:\Archive\Active\Test1   can we give as /Archive/Active/Test1

Clarify me these

Thanx a lot

(in reply to lazydev)
 
 
Post #: 7
 
 RE: create a folder with subfolders - 4/3/2008 2:42:03 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
Have you tried any code to figure these questions out on your own?

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to lazydev)
 
 
Post #: 8
 
 RE: create a folder with subfolders - 4/3/2008 2:57:45 AM   
  lazydev

 

Posts: 21
Score: 0
Joined: 2/21/2008
Status: offline
I have Tried the directory structure it failed when followed / slash

regarding while loop i can write the loop but how to get the user input's count .

i to got some in google search but failed in my script


(in reply to ebgreen)
 
 
Post #: 9
 
 RE: create a folder with subfolders - 4/3/2008 3:07:31 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
show the code that you are currently running and explain exactly what it does not do that you want it to do instead.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to lazydev)
 
 
Post #: 10
 
 RE: create a folder with subfolders - 4/3/2008 4:33:00 AM   
  lazydev

 

Posts: 21
Score: 0
Joined: 2/21/2008
Status: offline
 
Here's my code .If i give multiple values at user input with spaces or commas as delimiter i should have aloop run for all the folders.

it works fine for one value how to run it  in a loop

Dim objFSO   : Set objFSO = CreateObject( "Scripting.FileSystemObject" )
Dim strRoot  : strRoot    = "D:\Archive\Active"

Dim aFolders : aFolders   =  Array( "test1", "test2", "test3", "test4","Test5" )
Wscript.StdOut.WriteLine   "  Enter your Folder Name   : " 
Dim strUInp  : strUInp    =    Wscript.StdIn.ReadLine

Dim sFolder  : sFolder    = strRoot & "\" & strUInp
'Dim   Number   : Number  = 0
'While Number = 0
'NUMBER = number +1
If objFSO.FolderExists( sFolder ) Then
WScript.Echo "Folder '" & sFolder & "' already exists"
Else
objFSO.CreateFolder sFolder

End If
Dim oSubF    : Set oSubF  = objFSO.GetFolder( sFolder ).SubFolders
Dim sSubF

For Each sSubF In aFolders
If objFSO.FolderExists( sFolder & "\" & sSubF ) Then
WScript.Echo "Folder '" & sSubF & "' already exists"
Else
     oSubF.Add sSubF
  End If
Next
'Wend

(in reply to ebgreen)
 
 
Post #: 11
 
 RE: create a folder with subfolders - 4/3/2008 5:07:52 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
Well the first thing is to decide how the folders would be entered if their were multiple ones. Using spaces as a delimiter is not wise because space is a valid character for a folder name. This would cause trouble when parsing. I would personally use comma for the delimiter. What do you plan to use?

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to lazydev)
 
 
Post #: 12
 
 RE: create a folder with subfolders - 4/3/2008 5:25:34 AM   
  lazydev

 

Posts: 21
Score: 0
Joined: 2/21/2008
Status: offline
As i am new anything is ok

But just guide me how to do with commas as delimiter and get thecount of the userinputs and run the loop

guide me with the other new things in vbscripts

Thanx for your response

(in reply to ebgreen)
 
 
Post #: 13
 
 RE: create a folder with subfolders - 4/3/2008 5:34:30 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
Ok, so you need a way to take a comma delimited string and turn that into a list that you can iterate through. The simplest list to iterate through in vbscript (and most languages) is the array. So what you need is a function that takes a string and turns it into an array. That function is Split(). Look up the documentation for Split() and you should find an example of taking a string, breaking it into parts, then doing something with each of those parts.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to lazydev)
 
 
Post #: 14
 
 RE: create a folder with subfolders - 4/3/2008 6:28:06 AM   
  lazydev

 

Posts: 21
Score: 0
Joined: 2/21/2008
Status: offline
Nothing is working just give me a fine example for split ,count the number of fine inputs

Dim i As String = "Line 0,Line 1,Line 2,Line 3"
   Dim a() As String
   Dim j As Integer
   a = i.Split(",")
   For j = 0 To a.GetUpperBound(0)
   MsgBox(a(j))
   Next

(in reply to ebgreen)
 
 
Post #: 15
 
 RE: create a folder with subfolders - 4/3/2008 6:43:00 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
Did you read the WSH documentation? Specifically for Split?

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to lazydev)
 
 
Post #: 16
 
 RE: create a folder with subfolders - 4/3/2008 7:18:48 AM   
  lazydev

 

Posts: 21
Score: 0
Joined: 2/21/2008
Status: offline
Ok .Split works fine

Now how to get the count of this value .


Dim strAryWords
Dim strValue
strValue = "This,is,the,first,VB,script,by,abcd,xyx"
strAryWords = Split(strValue, ",")
' - strAryWords is now an array
Dim i
For i = 0 to Ubound(strAryWords)
Wscript.echo  i & " = " & strAryWords(i) & ""
Next

(in reply to ebgreen)
 
 
Post #: 17
 
 RE: create a folder with subfolders - 4/3/2008 7:26:46 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
Well to be honest with you, you are already using the count. UBound+1 is the number of items in an array.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to lazydev)
 
 
Post #: 18
 
 RE: create a folder with subfolders - 4/3/2008 7:32:07 AM   
  lazydev

 

Posts: 21
Score: 0
Joined: 2/21/2008
Status: offline
once again i am in problem

yes that is understood but i want to run the loop as while count <= 0 do count ++

In my program now if i give the input from user it fails .It shows entire one as a single test .It's not splitting.Just check it

Dim strAryWords
Dim strValue
Dim objFSO   : Set objFSO = CreateObject( "Scripting.FileSystemObject" )
Dim strRoot  : strRoot    = "D:\Archive\Active"
Wscript.StdOut.WriteLine   "  Enter your Folder Name   : " 
strValue =     Wscript.StdIn.ReadLine

Dim sFolder  : sFolder    = strRoot & "\" & strValue

strAryWords = Split(strValue, ",")
Dim i
For i = 0 to Ubound(strAryWords)
If objFSO.FolderExists( sFolder ) Then
WScript.Echo "Folder '" & sFolder & "' already exists"
Else
objFSO.CreateFolder sFolder
End If
NExt

(in reply to ebgreen)
 
 
Post #: 19
 
 RE: create a folder with subfolders - 4/3/2008 7:54:53 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
What exactly are you entering to be split? By the way it is often easier to hard code a value while you are developing rather than asking the user every time that the script runs. That way you don't have to continually enter a value every time that you test something. Also to get the user input I would change these lines:


Wscript.StdOut.WriteLine   "  Enter your Folder Name   : " 
strValue =     Wscript.StdIn.ReadLine


To this:

strValue = InputBox("Enter your Folder Name")


Then think further about exactly what the script is doing. You ask the user for multiple files, Let's say I put in "Foo,Bar,Weasles" when you ask me. So now:

strValue = "Foo,Bar,Weasels"

Then right after that you put the contents of strValue together with strRoot to get a value for sFolder so after this line executes:

Dim sFolder  : sFolder    = strRoot & "\" & strValue

then sFolder has a value of:

sFolder = "D:\Archive\Active\Foo,Bar,Weasels"

Then in the loop you check to see if sFolder exists. Will a folder named "D:\Archive\Active\Foo,Bar,Weasels" ever exist? Of course not. For one thing , is not valid in a folder name. That is why we chose it.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to lazydev)
 
 
Post #: 20
 
 
Page:   [1] 2   next >   >>
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> create a folder with subfolders Page: [1] 2   next >   >>
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts