• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 563
  • Last Modified:

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
0
robomonkey
Asked:
robomonkey
  • 12
  • 10
1 Solution
 
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
0
 
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
0
 
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
0
 
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
0
 
Chris DentPowerShell DeveloperCommented:

Ignore this bit: dblJobNumber = 65234

Was just testing ;)

Chris
0
 
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.
0
 
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
0
 
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
0
 
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
0
 
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
0
 
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
0
 
robomonkeyAuthor Commented:
This is great I love it :)

Thanks for you help
0
 
Chris DentPowerShell DeveloperCommented:

Pleasure :)

Chris
0
 
robomonkeyAuthor Commented:
Is there a way to count the number of subfolders under a folder?
0
 
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
0
 
Chris DentPowerShell DeveloperCommented:

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

Chris
0
 
robomonkeyAuthor Commented:
lol, I can't get that to work what would it be to count subfolders in c:\winnt?
0
 
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
0
 
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.
0
 
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
0
 
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?
0
 
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
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

  • 12
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now