×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

vbs script to create folders + subfolders by input foldernames

vbs script to create folders + subfolders by input foldernames

vbs script to create folders + subfolders by input foldernames

(OP)
Hello,

I am at this for a couple off days and totally new at this. This is what I got so far from google and youtube. It works and creates the folders, but I need the folders names from the inputboxes. I cant seem to get it work. Can somebody please help me. And also add some script for files that allrady exist so it looks at the date. just as you would add in a batch script \s \d.

I want to make this work

regards Tim



Option Explicit

DIM fSO

DIM foldername1
DIM foldername2
DIM foldername3

foldername1=INPUTBOX("KLANTNAAM")
foldername2=INPUTBOX("FENDERTYPE")
foldername3=INPUTBOX("ORDERNUMMER")

SET FSO=CreateObject("Scripting.FileSystemObject")

FSO.CREATEFOLDER("C:\Users\USERNAME\Desktop\TEST1\")
FSO.CREATEFOLDER("C:\Users\USERNAME\Desktop\TEST1\foldername1\")
FSO.CREATEFOLDER("C:\Users\USERNAME\Desktop\TEST1\foldername1\foldername2\")
FSO.CREATEFOLDER("C:\Users\USERNAME\Desktop\TEST1\foldername1\foldername2\foldername3\")

FSO.COPYFOLDER "C:\Users\USERNAME\Downloads","C:\Users\USERNAME\Desktop\TEST1\foldername1\foldername2\foldername3\"

RE: vbs script to create folders + subfolders by input foldernames

Hi TvdH1981,

This works

CODE

Option Explicit

dim wshShell
set wshShell = wscript.CreateObject("WScript.Shell")
dim username
username = wshShell.Environment("Process")("USERNAME")
'wscript.echo username 

DIM fSO

DIM foldername1
DIM foldername2
DIM foldername3

foldername1=INPUTBOX("KLANTNAAM")
foldername2=INPUTBOX("FENDERTYPE")
foldername3=INPUTBOX("ORDERNUMMER")

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

folderpath = "C:\Users\" & username & _
             "\Desktop\TESTT1\" 
wscript.echo "Creating folder: " & folderpath
FSO.CREATEFOLDER(folderpath)

folderpath = "C:\Users\" & username & _
             "\Desktop\TESTT1\" & _
             "\" & foldername1 
wscript.echo "Creating folder: " & folderpath
FSO.CREATEFOLDER(folderpath)

folderpath = "C:\Users\" & username & _
             "\Desktop\TESTT1\" & _
             "\" & foldername1 & "\" & foldername2 
wscript.echo "Creating folder: " & folderpath
FSO.CREATEFOLDER(folderpath)

folderpath = "C:\Users\" & username & _
             "\Desktop\TESTT1\" & _
             "\" & foldername1 & "\" & foldername2 & "\" & foldername3 
wscript.echo "Creating folder: " & folderpath
FSO.CREATEFOLDER(folderpath) 

RE: vbs script to create folders + subfolders by input foldernames

(OP)
Thank you so much. One question still. Can anybody use this script on their computer? So I don't have to change each username?

RE: vbs script to create folders + subfolders by input foldernames

That's the point of this line:

username = wshShell.Environment("Process")("USERNAME")

RE: vbs script to create folders + subfolders by input foldernames

(OP)
Is it correct you left out the copy part last line in my script. Where i copy files from a location to the newly created maps?

RE: vbs script to create folders + subfolders by input foldernames

CODE --> TvdH1981

Is it correct you left out the copy part last line in my script. Where i copy files from a location to the newly created maps? 
I left out the copy part only because I just didn't want to copy anything

RE: vbs script to create folders + subfolders by input foldernames

(OP)
I made it work. Thanks

RE: vbs script to create folders + subfolders by input foldernames

(OP)
Hi Again,

It works and creates the folders I want, but I can't create more folders after that under the same name. How can we do that?

Tim

RE: vbs script to create folders + subfolders by input foldernames

Hi Tim,

Trying to create a folder which is already created throws the runtime error: File already exists

You could do following:

Either: Before you create every folder test if the folder does not already exist

Or: Use on the beginning of your script the statement

CODE

On Error Resume Next 

This causes that, when an error occurs the script will not stop but continue on the next line.

RE: vbs script to create folders + subfolders by input foldernames

Here is the modified script which works for me:

CODE

Option Explicit
On Error Resume Next

dim wshShell
set wshShell = wscript.CreateObject("WScript.Shell")
dim username
username = wshShell.Environment("Process")("USERNAME")
'wscript.echo username 

DIM fSO

DIM foldername1
DIM foldername2
DIM foldername3

foldername1=INPUTBOX("KLANTNAAM")
foldername2=INPUTBOX("FENDERTYPE")
foldername3=INPUTBOX("ORDERNUMMER")

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

folderpath = "C:\Users\" & username & _
             "\Desktop\TESTT1\" 
wscript.echo "Creating folder: " & folderpath
FSO.CREATEFOLDER(folderpath)

folderpath = "C:\Users\" & username & _
             "\Desktop\TESTT1\" & _
             "\" & foldername1 
wscript.echo "Creating folder: " & folderpath
FSO.CREATEFOLDER(folderpath)

folderpath = "C:\Users\" & username & _
             "\Desktop\TESTT1\" & _
             "\" & foldername1 & "\" & foldername2 
wscript.echo "Creating folder: " & folderpath
FSO.CREATEFOLDER(folderpath)

folderpath = "C:\Users\" & username & _
             "\Desktop\TESTT1\" & _
             "\" & foldername1 & "\" & foldername2 & "\" & foldername3 
wscript.echo "Creating folder: " & folderpath
FSO.CREATEFOLDER(folderpath) 

RE: vbs script to create folders + subfolders by input foldernames

(OP)
Well the klantnaam or clientname can have multiple folders within. What I want is multiple folders created in the same klantnaam or same fendertype

RE: vbs script to create folders + subfolders by input foldernames

(OP)
Oh what maybe is even more better if possible. That I can make a list of names for foldername1 so it's a pulldownmenu instead of a input name. Is this possible to create?

Thanks for all the help sofar

RE: vbs script to create folders + subfolders by input foldernames

(OP)
Hello. Any ideas for me?

RE: vbs script to create folders + subfolders by input foldernames

Quote (TvdH1981)


Oh what maybe is even more better if possible. That I can make a list of names for foldername1 so it's a pulldownmenu instead of a input name. Is this possible to create?

Hi Tim,

As far as I know, VbScript does not have drop-down list boxes, but you can do that as na HTA application (see https://en.wikipedia.org/wiki/HTML_Application)

It would be something like this




RE: vbs script to create folders + subfolders by input foldernames

Hi Tim,

I have already tried Proof Of Concept of the HTA application. Here is the source:

TvdH1981.hta

CODE

<html>
  <head>
    <title>TvdH1981</title>
    <HTA:APPLICATION ID="hta:ListBoxApp" 
    APPLICATIONNAME="Folders Demo"
    SCROLL="auto"
    SINGLEINSTANCE="yes">
  </head>

<script language="VBScript">
  dim objShell
  set objShell = CreateObject("WScript.Shell")

  dim username
  username = objShell.Environment("Process")("USERNAME")
  
  dim basefolderpath
  basefolderpath = "C:\Users\" & username & "\Desktop\TESTT1"

  sub Window_OnLoad
    self.resizeTo 600, 400
    document.getElementById("myFolder").innerHTML = basefolderpath
  end sub

  sub createFolders
    On Error Resume Next

    dim fso
    set fso = CreateObject("Scripting.FileSystemObject")

    folderpath = ""
    if PickCustommer.value <> ""  and _
       PickFenderType.value <> "" and _
       PickOrderNumber.value <> "" then
       ' create base folder
       folderpath = basefolderpath
       MsgBox "Creating folder: " & VbCrLf & folderpath
       fso.CreateFolder(folderpath)
       
       ' create folder1
       foldername1 = PickCustommer.value
       folderpath = folderpath & "\" & foldername1
       MsgBox "Creating folder: " & VbCrLf & folderpath  
       fso.CreateFolder(folderpath)

       ' create folder2
       foldername2 = PickFenderType.value
       folderpath = folderpath & "\" & foldername2
       MsgBox "Creating folder: " & VbCrLf & folderpath  
       fso.CreateFolder(folderpath)
        
       ' create folder3
       foldername3 = PickOrderNumber.value
       folderpath = folderpath & "\" & foldername3
       MsgBox "Creating folder: " & VbCrLf & folderpath  
       fso.CreateFolder(folderpath)

       MsgBox "Done."  
    end if
  end sub
</script>

<body>
  <h1>Creating Folders in:</h1>
  <h2><span id="myFolder"></span></h2>
  <br>
  <p>
    <label>Custommer:</label>
    <select size="1" name="PickCustommer">
      <option value="">Select Custommer</option>
      <option value="Custommer01">Custommer #1</option>
      <option value="Custommer02">Custommer #2</option>
      <option value="Custommer03">Custommer #3</option>
      <option value="Custommer04">Custommer #4</option>
    </select>
  </p>

  <p>
    <label>Fender Type:</label>
    <select size="1" name="PickFenderType">
      <option value="">Select Fender Type</option>
      <option value="FenderType01">FenderType #1</option>
      <option value="FenderType02">FenderType #2</option>
      <option value="FenderType03">FenderType #3</option>
      <option value="FenderType04">FenderType #4</option>
    </select>
  </p>

  <p>
    <label>Order Number:</label>
    <select size="1" name="PickOrderNumber">
      <option value="">Select Order Number</option>
      <option value="OrderNumber01">OrderNumber #1</option>
      <option value="OrderNumber02">OrderNumber #2</option>
      <option value="OrderNumber03">OrderNumber #3</option>
      <option value="OrderNumber04">OrderNumber #4</option>
    </select>
  </p>

  <input type="button" value="Create" onClick="createFolders">
</body>
</html> 

Save the source as TvdH1981.hta and run it by doubble clicking.

RE: vbs script to create folders + subfolders by input foldernames

(OP)
this is nomething new. where do I put this source code? Not a notepadfile right?

RE: vbs script to create folders + subfolders by input foldernames

(OP)
Btw is this all save to use?

RE: vbs script to create folders + subfolders by input foldernames

(OP)
I am getting the error message "this is not a valid win 32 file

Also I only need for the custommer to be a pulldown menu. The the other 2 field can be inputboxes

can I add the copy part for the folder somewhere?

Thnak u so much for helping out

RE: vbs script to create folders + subfolders by input foldernames

You can copy the source into notepad and save it as a file with extension .hta e.g. TvdH1981.hta. Then to run it, double click on it.

I tested it on Windows 10.

Insert the copy part at the end of the subroutine createFolders before end if


RE: vbs script to create folders + subfolders by input foldernames

(OP)
Well. It doesn't run. I did exactly that.. Saved as.hta. My laptop is still win 7 though

RE: vbs script to create folders + subfolders by input foldernames

What's the error ?

RE: vbs script to create folders + subfolders by input foldernames

HTA - this technology, which is actually already outdated, should definitely run on Windows 7, because in Windows 7 Internet Explorer was still the default web browser and with it the program mshta.exe, which runs the HTA applications, should be there.

RE: vbs script to create folders + subfolders by input foldernames

If it is a company laptop, the use of HTA may have been disabled, as they are seen as a big security risk.

RE: vbs script to create folders + subfolders by input foldernames

(OP)


not a valid win32


its my own laptop. But if I cant run it on my work than the whole point of usage is gone :)

RE: vbs script to create folders + subfolders by input foldernames

(OP)
Well maybe I have to go to an excel sheet and great folders

RE: vbs script to create folders + subfolders by input foldernames

Maybe something wrong installed with your W7, first match in google got this: https://community.dynamics.com/ax/b/klaasdeforche/...
Check on your W7 if the .hta file extension has right association with mshta.exe

RE: vbs script to create folders + subfolders by input foldernames

(OP)
Ok thanks it works now. But I think this is way to much work to fill in all the veriables.

Can you change fendertype and ordernumber back to input boxes and leave the custommer as is? Is it possible to refer to a excel sheet with custommers?

RE: vbs script to create folders + subfolders by input foldernames

Quote (TvdH1981)


Can you change fendertype and ordernumber back to input boxes and leave the custommer as is?
Yes, i have done it



Here is the source code:

TvdH1981.hta

CODE

<html>
  <head>
    <title>TvdH1981</title>
    <HTA:APPLICATION ID="hta:ListBoxApp" 
    APPLICATIONNAME="Folders Demo"
    SCROLL="auto"
    SINGLEINSTANCE="yes">
  </head>

<script language="VBScript">
  dim objShell
  set objShell = CreateObject("WScript.Shell")

  dim username
  username = objShell.Environment("Process")("USERNAME")
  
  dim basefolderpath
  basefolderpath = "C:\Users\" & username & "\Desktop\TESTT1"

  sub Window_OnLoad
    self.resizeTo 600, 400
    document.getElementById("myFolder").innerHTML = basefolderpath
  end sub

  sub createFolders
    On Error Resume Next

    dim fso
    set fso = CreateObject("Scripting.FileSystemObject")

    folderpath = ""
    if PickCustommer.value <> ""  and _
       PickFenderType.value <> "" and _
       PickOrderNumber.value <> "" then
       ' create base folder
       folderpath = basefolderpath
       MsgBox "Creating folder: " & VbCrLf & folderpath
       fso.CreateFolder(folderpath)
       
       ' create folder1
       foldername1 = PickCustommer.value
       folderpath = folderpath & "\" & foldername1
       MsgBox "Creating folder: " & VbCrLf & folderpath  
       fso.CreateFolder(folderpath)

       ' create folder2
       foldername2 = PickFenderType.value
       folderpath = folderpath & "\" & foldername2
       MsgBox "Creating folder: " & VbCrLf & folderpath  
       fso.CreateFolder(folderpath)
        
       ' create folder3
       foldername3 = PickOrderNumber.value
       folderpath = folderpath & "\" & foldername3
       MsgBox "Creating folder: " & VbCrLf & folderpath  
       fso.CreateFolder(folderpath)

       MsgBox "Done."  
    end if
  end sub
</script>

<body>
  <h1>Creating Folders in:</h1>
  <h2><span id="myFolder"></span></h2>
  <br>
  <p>
    <label>Custommer:</label>
    <select size="1" name="PickCustommer">
      <option value="">Select Custommer</option>
      <option value="Custommer01">Custommer #1</option>
      <option value="Custommer02">Custommer #2</option>
      <option value="Custommer03">Custommer #3</option>
      <option value="Custommer04">Custommer #4</option>
    </select>
  </p>

  <p>
    <label>Fender Type:</label>
    <input type="text" name="PickFenderType" size="20" maxlength = "20">
  </p>

  <p>
    <label>Order Number:</label>
    <input type="text" name="PickOrderNumber" size="20" maxlength = "20">
  </p>

  <input type="button" value="Create" onClick="createFolders">
</body>
</html> 

RE: vbs script to create folders + subfolders by input foldernames

(OP)
Hi Mikrom,

I have it working now with the script.

What is the script to open the last created file?


RE: vbs script to create folders + subfolders by input foldernames

Hi TvdH1981
> What is the script to open the last created file?

Try to add at the end of the script following

Quote:


WshShell.Run folderpath

RE: vbs script to create folders + subfolders by input foldernames

(OP)
This doesn't seem to do anything

RE: vbs script to create folders + subfolders by input foldernames

I added the above statement at the end of the script I posted here on 30 Mar 22 07:46
I tried it and on my computer it works fine, it opens the last folder automatically.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close