Windows scripting help

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

Windows scripting help

Postposted on Tue Jan 27, 2009 11:27 am

Ok guys. here is my issue. I am not a script guy nor do i have access to one.

I get requests for new folder creations on a share. For every request i get i end up creating 19 folders. I would like to script this. The structure is as follows

New Folder
Sub folder1
Sub-sub folder1
Sub-sub folder2
Sub-sub folder3
Sub-sub folder4
Sub-sub folder5
Sub-sub folder6
Sub-sub folder7
Sub-sub folder8
Sub folder2
Sub-sub folder1
Sub-sub folder2
Sub-sub folder3
Sub-sub folder4
Sub-sub folder5
Sub-sub folder6
Sub-sub folder7
Sub-sub folder8

The only variable is the top lvl folder. All the folders below it have static names. If i could script the creation that would be sweet but eventually i would also like to set the permissions on those folder. those permissions atre static also but NOT inherrited. There are different for each bottom lvl folder.

Thank you in advance for any help you can provide.
Newbzilla
Gerbil In Training
 
Posts: 1
Joined: Tue Jan 27, 2009 11:20 am

Re: Windows scripting help

Postposted on Tue Jan 27, 2009 11:39 am

I don't know about setting the permission but windows scripting is probably not the best solution. A .NET executable might be better but google "file system object" with respect to your language of choice (Windows Scripting it looks like) and create away.

Code: Select all
Dim fso , desiredNumFolders, desiredSubFolders

set fso = server.createobject("scripting.filesystemobject")
for i = 0 to DesiredMainFolders
   fso.createfolder("c:\blah\Newfolder\" & i)
   for j = 0 to desiredSubFolders
      fso.createfolder("c:\blah\Newfolder" & i & "\Newfolder" & j)
   next j
next i


this code should almsot run, you'll need to initialize the 2 desired variables to a number. So for however many i folders you create, it will create those and place j number of subfolders. This assumes you have permission to run these types of scripts on the computer. Good luck!
Corsair 600T | ASUS P8P67 PRO | Intel 2500k @ 4.4Ghz | EVGA 560 TI | G.SKILL Ripjaws Series 8GB | Corsair HX650 650W
steelcity_ballin
Gerbilus Supremus
Silver subscriber
 
 
Posts: 11883
Joined: Mon May 26, 2003 4:55 am
Location: Pittsburgh PA

Re: Windows scripting help

Postposted on Tue Jan 27, 2009 12:28 pm

A VBScript is perfectly fine. Creating a .NET Executable seems like overkill. Exactly as pete says, but use an input box to prompt for the folder name.

Code: Select all
Dim fso, folderName, numberSubFolders, numberSubSubFolders

folderName = InputBox("Enter the name of the folder", "Enter Folder Name")
numberSubFolders = InputBox ("Enter the number of sub folders to create", "Number Sub Folders")
numberSubSubFolders = InputBox ("Enter the number of Sub-Sub folders to create", "Number Sub-Sub Folders")

set fso = server.createobject("scripting.filesystemobject")
fso.CreateFolder ("C:\FoldersDirectory\" & folderName)

for i = 1 to numberSubFolders
   fso.createfolder(C:\FoldersDirectory\" & folderName & "\Sub Folder " & i)
   for j = 1 to numberSubSubFolders
      fso.createfolder("C:\FoldersDirectory\" & folderName & "\Sub Folder " & i & "\" & "Sub Sub Folder " & j)
   Next
Next


Note that VBScript for loops are inclusive, hence starting at 1 and counting up to the number of folders wanted. I'm sure that you can also set NTFS permissions, just don't know off the top of my head. Quick google search says it's possible, but not straight forward.
Intel i7 860, Asus P7P55D Pro, 4x2GB Corsair XMS3 1600 (CMX4GX3M2A1600C9), EVGA GTX 560 Ti Superclocked
Seagate 7200.7 160GB, WD Caviar Black 640GB, WD Caviar Green 1TB, WD Caviar Green 2TB
Dell 2408WFP and Dell 2407WFP-HC for dual-24" goodness
emorgoch
Gerbil Elite
 
Posts: 686
Joined: Tue Mar 27, 2007 10:26 am
Location: Toronto, ON

Re: Windows scripting help

Postposted on Tue Jan 27, 2009 12:32 pm

Oops yep, made a mistake in my loop, I always start at 0 and go terminationValue -1 though. I made some other minor mistakes that I'd have caught if I wasn't typing directly into the post window instead of my editor :lol:
Corsair 600T | ASUS P8P67 PRO | Intel 2500k @ 4.4Ghz | EVGA 560 TI | G.SKILL Ripjaws Series 8GB | Corsair HX650 650W
steelcity_ballin
Gerbilus Supremus
Silver subscriber
 
 
Posts: 11883
Joined: Mon May 26, 2003 4:55 am
Location: Pittsburgh PA

Re: Windows scripting help

Postposted on Tue Jan 27, 2009 8:01 pm

Even a batch file would work if we don't talk about permissions.
Image
The Model M is not for the faint of heart. You either like them or hate them.

Gerbils unite! Fold for UnitedGerbilNation, team 2630.
Flying Fox
Gerbil God
 
Posts: 24141
Joined: Mon May 24, 2004 1:19 am

Re: Windows scripting help

Postposted on Thu Jan 29, 2009 5:28 pm

Guys, I see in your scripts that you're using loops. I think you've missed a key point:

Newbzilla wrote:All the folders below it have static names.


So, while I can't really offer a script that answers this, loops are not needed in this case. Unless you're using a custom variable. (I forget what it's called, but I know it exists in C++.)
The best things in life are free.
http://www.gentoo.org
Guy 1: Surely, you will fold with me.
Guy 2: Alright, but don't call me Shirley.
titan
Grand Gerbil Poohbah
 
Posts: 3276
Joined: Mon Feb 18, 2002 6:00 pm
Location: Great Smoky Mountains

Re: Windows scripting help

Postposted on Thu Jan 29, 2009 5:40 pm

Flying Fox wrote:Even a batch file would work if we don't talk about permissions.

Actually it will work even with permissions ( http://www.ss64.com/nt/cacls.html ). And I would probably choose the batch file route.

They are so darn simple to make, and you don't need to google all sorts of funny function names, that it's not even funny.

I even use
Code: Select all
if(system("copy a.txt+b.txt c.txt"))
     //dostuff

stuff in c/c++ quite often as well. Why bother with studying parameters and all error conditions for something like this ( http://msdn.microsoft.com/en-us/library/aa363851(loband).aspx ) when the console solution takes one line of code and returns one integer that tells everything.

Of course, if the application goes to a client and is the regular shiny CD type, then the CopyFileW is probably better, but if it's some back end script/process then console version beats it with a stick.
Core 2 Duo E6300, MSI P45 NEO-F, Club 3D GTX 260, 4Gb DDR2-800Mhz, Audigy X-Fi Fatal1ty Champ1on ed., 0.5Tb+1Tb Seagate Barracuda 7200.12, 630W AXP, Samsung SyncMaster BX2450, ViewSonic VP171b
Madman
Minister of Gerbil Affairs
 
Posts: 2317
Joined: Tue Apr 01, 2003 3:55 am
Location: Latvia

Re: Windows scripting help

Postposted on Thu Feb 19, 2009 10:04 am

I'm not sure if anyone ever did it right for you but this should work. I didn't use loops because you specified these are all static folder names, and you would want to assign specific permissions to each of them. If you want me to include code for specifying some default permissions for all of them, I can, just let me know. Hopefully this works for you. If you have any questions, feel free to post em. To make this script work, copy the contents in the code block into a notepad file and rename it to "File.VBS" This should run on any 2000 or later system.

Code: Select all
'Script creates 19 folder structure

Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Variable setup
Dim Path
Dim TopFolder
Dim Sub1
Dim Sub2

'Prompt for the top folder "New Folder Name"
TopFolder = InputBox("Provide a name for the top level folder")

'Provide the two main subfolder names
Sub1 = "Sub Folder 1"
Sub2 = "Sub Folder 2"

'Network path to the shared drive/share the top level folder is being created on
Path = "\\Server1\Test"

If objFSO.FolderExists (Path&"\"&TopFolder) Then
   WshShell.Popup "Folder already Exists."
   WScript.Quit()
Else
   objFSO.CreateFolder Path&"\"&TopFolder
   objFSO.CreateFolder Path&"\"&TopFolder&"\"&Sub1
   objFSO.CreateFolder Path&"\"&TopFolder&"\"&Sub2
   
   'Set all your sub-sub folder names here for Sub Folder 1
   objFSO.CreateFolder   Path&"\"&TopFolder&"\"&Sub1&"\"&"Sub-sub Folder 1"
   objFSO.CreateFolder   Path&"\"&TopFolder&"\"&Sub1&"\"&"Sub-sub Folder 2"
   objFSO.CreateFolder   Path&"\"&TopFolder&"\"&Sub1&"\"&"Sub-sub Folder 3"
   objFSO.CreateFolder   Path&"\"&TopFolder&"\"&Sub1&"\"&"Sub-sub Folder 4"
   objFSO.CreateFolder   Path&"\"&TopFolder&"\"&Sub1&"\"&"Sub-sub Folder 5"
   objFSO.CreateFolder   Path&"\"&TopFolder&"\"&Sub1&"\"&"Sub-sub Folder 6"
   objFSO.CreateFolder   Path&"\"&TopFolder&"\"&Sub1&"\"&"Sub-sub Folder 7"
   objFSO.CreateFolder   Path&"\"&TopFolder&"\"&Sub1&"\"&"Sub-sub Folder 8"
   
   'Set all your sub-sub folder names here for Sub Folder 2
   objFSO.CreateFolder   Path&"\"&TopFolder&"\"&Sub2&"\"&"Sub-sub Folder 1"
   objFSO.CreateFolder   Path&"\"&TopFolder&"\"&Sub2&"\"&"Sub-sub Folder 2"
   objFSO.CreateFolder   Path&"\"&TopFolder&"\"&Sub2&"\"&"Sub-sub Folder 3"
   objFSO.CreateFolder   Path&"\"&TopFolder&"\"&Sub2&"\"&"Sub-sub Folder 4"
   objFSO.CreateFolder   Path&"\"&TopFolder&"\"&Sub2&"\"&"Sub-sub Folder 5"
   objFSO.CreateFolder   Path&"\"&TopFolder&"\"&Sub2&"\"&"Sub-sub Folder 6"
   objFSO.CreateFolder   Path&"\"&TopFolder&"\"&Sub2&"\"&"Sub-sub Folder 7"
   objFSO.CreateFolder   Path&"\"&TopFolder&"\"&Sub2&"\"&"Sub-sub Folder 8"
   
   'confirmation prompt
   WshShell.Popup "Folders Created"
End If
i7-920 @ 4.20GHz | Biostar Tpower X58 | 4870 1GB | 3x2 DDR3-1600 OCZ Gold Modules | Windows 7 Ultimate x64 | 4x 1TB Black WD Hard Disk Raid 1
Master Kenobi
Gerbil First Class
 
Posts: 121
Joined: Wed Aug 15, 2007 8:08 am

Re: Windows scripting help

Postposted on Thu Apr 09, 2009 3:17 pm

Honestly, I don't even see the need for a script if it's all static. Just create a template folder structure in a staging folder and copy the whole structure whenever you get these requests. If the permissions are static, you could pre-populate that as well and then use robocopy to copy with exact permission duplication.
cygnus1
Gerbil
Gold subscriber
 
 
Posts: 64
Joined: Fri Oct 28, 2005 8:49 pm


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 1 guest