Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
Newbzilla
Gerbil In Training
Topic Author
Posts: 1
Joined: Tue Jan 27, 2009 12:20 pm

Windows scripting help

Tue Jan 27, 2009 12:27 pm

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.
 
steelcity_ballin
Gerbilus Supremus
Posts: 12072
Joined: Mon May 26, 2003 5:55 am
Location: Pittsburgh PA

Re: Windows scripting help

Tue Jan 27, 2009 12:39 pm

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.

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!
 
emorgoch
Gerbil Elite
Posts: 719
Joined: Tue Mar 27, 2007 11:26 am
Location: Toronto, ON

Re: Windows scripting help

Tue Jan 27, 2009 1: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.

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 4790k @ stock, Asus Z97-PRO(Wi-Fi ac), 2x8GB Crucial DDR3 1600MHz, EVGA GTX 1080Ti FTW3
Samsung 950 Pro 512GB + 2TB Western Digital Black
Dell 2408WFP and Dell 2407WFP-HC for dual-24" goodness
Windows 10 64-bit
 
steelcity_ballin
Gerbilus Supremus
Posts: 12072
Joined: Mon May 26, 2003 5:55 am
Location: Pittsburgh PA

Re: Windows scripting help

Tue Jan 27, 2009 1: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:
 
Flying Fox
Gerbil God
Posts: 25690
Joined: Mon May 24, 2004 2:19 am
Contact:

Re: Windows scripting help

Tue Jan 27, 2009 9:01 pm

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

Gerbils unite! Fold for UnitedGerbilNation, team 2630.
 
titan
Grand Gerbil Poohbah
Posts: 3376
Joined: Mon Feb 18, 2002 7:00 pm
Location: Great Smoky Mountains
Contact:

Re: Windows scripting help

Thu Jan 29, 2009 6: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.
 
Madman
Minister of Gerbil Affairs
Posts: 2317
Joined: Tue Apr 01, 2003 4:55 am
Location: Latvia

Re: Windows scripting help

Thu Jan 29, 2009 6: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
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
 
Master Kenobi
Gerbil First Class
Posts: 127
Joined: Wed Aug 15, 2007 9:08 am

Re: Windows scripting help

Thu Feb 19, 2009 11: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.

'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-6700k @ 4.00GHz | Gigabyte Gaming-5 | GTX 980 4GB | 4x8 (32GB) DDR4-2400 Corsair Vengeance LPX | Windows 10 Pro x64 | 1x 1TB Samsung EVO SSD | 1x 3TB WD Green HDD
 
cygnus1
Gerbil
Posts: 88
Joined: Fri Oct 28, 2005 9:49 pm

Re: Windows scripting help

Thu Apr 09, 2009 4: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.

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On