How to use Scripting to create a folder structure

Hi,

I'd like to use a vbs script to create a job folder structure.  As I see it I need to do the following steps:-

1. Get the job number from the user
2. Check its a valid number
3. Check if the structure already exists, if not create it.

Any pointers to examples or completed scripts would be ace.

Ta
robomonkeyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris DentPowerShell DeveloperCommented:

1 and 2 are a bit vague. How is that happening or what is going on there?

Creating an individual Folder is quite easy, creating a group depends on the structure you want to create:

Set objFileSystem = CreateObject("Scripting.FileSystemObject")

' Check if a folder exists:

If Not objFileSystem.FolderExists(strFolderPath) Then
    objFileSystem.CreateFolder(strFolderPath)
End If

HTH

Chris
theruckCommented:
i have not understood completely but i have found everything about vbs i ever needed here: http://www.microsoft.com/technet/scriptcenter/resources/qanda/hsgarch.mspx
robomonkeyAuthor Commented:
Wow, that was quick, thanks for posting guys,

1. A user will create the structure by running the script, they will be presented with an inputbox asking for the jobnumber.
2. The script need to check the input to ensure it as a number (not text) and is >1,000 and <100,000
3. The script then ideally has to create missing folders, when I've run a script before it errors if the folder exists, not ideal as sometimes the script may be used to create missing folders under the jobnumber.

end result should be:-

\\server\jobnumber
\\server\jobnumber\folder1
\\server\jobnumber\folder2
\\server\jobnumber\folder3
\\server\jobnumber\folder4

If for example folder3 is missing the script need to be able to realise that all the other folders exist and to only create folder3.  I achieved this by running an if statement on each folder creation statement.

Once complete this script will create approximately 50-70 subfolders under the jobnumber, which is why I want the error handling and confirmation checking correct.
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

robomonkeyAuthor Commented:
Sorry forgot to mention another complication is that we sort our job folders by thousands, e.g. currently our numbers are upto 68531, so for a the next job the user will enter 68532 into the input box, from that I need to roun down to the nearest thousand and create\check for the subfolder, eg.

\\server\68000\68532\folder1\subfolder1
Chris DentPowerShell DeveloperCommented:

Is it really setup with a Share for the job number? It would be far simpler to have a share called Jobs and a folder under there, but I guess that's not possible?

How do you want to define folder1 and subfolder1?

This bit allows you to read in and initially validate the Job Number:

dblJobNumber = InputBox("Please Enter the Job Number")

Do Until IsNumeric(dblJobNumber)
      dblJobNumber = InputBox("Please Enter the Job Number")
Loop

If dblJobNumber < 1000  Or dblJobNumber > 100000 Then
      WScript.Echo "Job Number must be between 1000 and 100000"
      WScript.Quit
End If

Figuring out the folder it should be in can be done like this (might not be the best way, but it will work):

dblJobNumber = 65234
dblJobFolder = dblJobNumber / 1000
If InStr(dblJobFolder, ".") Then
      dblJobFolder = Left(dblJobFolder, InStr(dblJobFolder, ".") - 1)
End If
dblJobFolder = dblJobFolder * 1000

Which gives us the path:

strFolderPath = "\\Server\" & dblJobFolder & "\" & dblJobNumber

Now the tricky bit is creating the share for the JobNumber, I'm not going into that unless it's true that you have shares for each group of jobs.

For the folder creation part you hand over to the file system object:

If Not objFileSystem.Exists(strFolderPath) Then
      objFileSystem.CreateFolder(strFolderPath)
End If

Nothing in place for the SubFolders yet, is there a default folder structure for those?

Chris

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris DentPowerShell DeveloperCommented:

Ignore this bit: dblJobNumber = 65234

Was just testing ;)

Chris
robomonkeyAuthor Commented:
Cool, thanks

I can follow most of the script,

> dblJobNumber = InputBox("Please Enter the Job Number")

> Do Until IsNumeric(dblJobNumber)
>     dblJobNumber = InputBox("Please Enter the Job Number")
> Loop

does using dbl ensure that the variable is a "double" integer type variable? or is that just proper naming convention.

I don't understand why you have two entries for inputbox, is that just two ways of doing the same thing.
Chris DentPowerShell DeveloperCommented:

dbl is just like that to give an indication or what data type the variant stores. It has to be that because the number is out of range for an Integer, you can't have an Integer greater than 32767 - means we can't use things like CInt on it.

And no you're right, it don't need two InputBox statements, just an othersight on my part.

Chris
robomonkeyAuthor Commented:
Also no, its not for creating a share for each job, it is as you said above higher in the structure, this is solely for creating the folder structure
Chris DentPowerShell DeveloperCommented:

Great, that does make it easier :)

In that case we can use exactly the same objFileSystem.FolderExists check on dblJobFolder so that can be created if it doesn't exist already.

Chris
robomonkeyAuthor Commented:
Hi I think I've cracked most it just need copy your bit for creating the parent 1000's folder and to add the 50 odd if statements to create the folders in the middle.

Can I get this statement to default to No and also add the vbQuestion to it, I just get errors when I do it
> intConfirm = MsgBox("Are you sure you want to create " & JobNumber, vbYesNo, "Confirm creation")



Can you spot any potential problems so far?

===========================================================
Option Explicit



Dim JobNumber, intConfirm, msg, SubFolders


Jobnumber = Inputbox("Please enter the Jobnumber without the leading zero", "Please Enter JobNumber")
      If IsNumeric(JobNumber) = True Then
            If JobNumber >=10000 And JobNumber <=100000 Then
                  intConfirm = MsgBox("Are you sure you want to create " & JobNumber, vbYesNo, "Confirm creation")
                  If intConfirm = vbYes Then
            
            

            
            
            
            
            
            
            
                        SubFolders = 5
                      msg = Msgbox("Job number " & JobNumber & " has been successfully created with " & SubFolders & " subfolders", vbInformation, "Creation Successfull")
                  Else
                      msg = MsgBox("Job folders for " & jobnumber & " have not been created", vbExclamation, "Creation Aborted")
                      WScript.Quit
                  End If


            Else
                  msg = MsgBox(Jobnumber & " is an invalid Job Number, It must be between 10,000 and 100,000, Please check and try again", vbCritical, "Invalid Input")
                  Wscript.Quit
            End If                  

Else
      msg = MsgBox(Jobnumber & " is an invalid Job Number, Please check and try again", vbCritical, "Invalid Input")
      Wscript.Quit
End If
Chris DentPowerShell DeveloperCommented:

You can have multiple Button Types if you add them together:

intConfirm = MsgBox("Are you sure you want to create " & JobNumber, VbQuestion + vbYesNo, "Confirm creation")

Then adding in the Default No:

intButtons = VbDefaultButton2 + VbQuestion + VbYesNo
intConfirm = MsgBox("Are you sure you want to create " & JobNumber, intButtons, "Confirm creation")

Otherwise looks pretty good to me.

Chris
robomonkeyAuthor Commented:
This is great I love it :)

Thanks for you help
Chris DentPowerShell DeveloperCommented:

Pleasure :)

Chris
robomonkeyAuthor Commented:
Is there a way to count the number of subfolders under a folder?
Chris DentPowerShell DeveloperCommented:

Can't remember if there's a number property of the SubFolders object, I'll have a quick look. Otherwise:

Set objFolder = objFileSystem.GetFolder(strFolderPath)
i = 0
For Each objSubFolder in objFolder.SubFolders
      i = i + 1
Next
WScript.Echo i

Chris
Chris DentPowerShell DeveloperCommented:

Well I can't found a count property so I guess we might be stuck with the above.

Chris
robomonkeyAuthor Commented:
lol, I can't get that to work what would it be to count subfolders in c:\winnt?
Chris DentPowerShell DeveloperCommented:

Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFileSystem.GetFolder("C:\WinNT")

i = 0
For Each objSubFolder in objFolder.SubFolders
      i = i + 1
Next
WScript.Echo i

Chris
robomonkeyAuthor Commented:
hehe cool, ta just couldn't make it work.

Unfortunately it doesn't check subfolders of subfolders so it won't give me any useful information, but thanks anyway very much appreciated.
Chris DentPowerShell DeveloperCommented:

It can be made to... what are you looking to return? Just need to add a spot of recursion into it

Chris
robomonkeyAuthor Commented:
I'd like it to return the results you'd get from showing properties of a subfolder, i.e. the total number of subfolders, does that make sense?
Chris DentPowerShell DeveloperCommented:

Ohh for the entire structure down?

This should work:

Sub FolderCounter(objFolder)

      Dim objSubFolder

      For Each objSubFolder in objFolder.SubFolders
            i = i + 1
            FolderCounter(objSubFolder)
      Next

End Sub

Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFileSystem.GetFolder("C:\WinNT")

i = 0
FolderCounter(objFolder)
WScript.Echo i

Chris
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2003

From novice to tech pro — start learning today.