Jump to content

Recommended Posts

Posted

Hey all, I'm in need of a script that I can use to share alot of folders all in one directory. I have the following for example:

net share = /CACHE:no /GRANT:domain\user,READ|CHANGE|FULL

 

but obviously this requires me to type in each folder name, anyone have a script for automating this? Any help appreciated.

Posted (edited)

how good are you with VBS?

 

enumerate all subfolders in a folder:

Scripting Files and Folders using VBScript

 

use a for each loop that runs a command for each returned folder name

How to: Execute a file/program in VBScript - CRM Developer

 

[color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#15326b]dim folderpath
folderpath = "C\folder"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder([/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#15326b]folderpath[/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#15326b])
Set colSubfolders = objFolder.Subfolders
For Each objSubfolder in colSubfolders
   Wscript.Echo objSubfolder.Name
   [/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][color=#000080]Run "[/color]net share " & [color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#15326b]objSubfolder.Name[/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color] & "=" & [color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#15326b]folderpath [/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color]& "\" & [color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#15326b]objSubfolder.Name[/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color] & " /CACHE:no /GRANT:domain\user,READ|CHANGE|FULL[color=#000080]"[/color]
[color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#000020][color=#15326b]Next[/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color][/color]
[color=#000080]Sub Run(ByVal sFile)
Dim shell

   Set shell = CreateObject("WScript.Shell")
   shell.Run Chr(34) & sFile & Chr(34), 1, false
   Set shell = Nothing
End Sub
[/color]

Haven't tested it but it should be a good start if it does not work straight away.

Edited by SYNACK
Posted

Why don't you just share the root of all these folders? With the correct permissions, people would only be able to get into what they should be able to anyway.

 

If mapping drives off these shares, it is possible to map a subdirectory of a share as a drive letter too.

Posted

i use this:

 

FOR /D %%G IN (*.*) DO NET SHARE "%%G=%CD%\%%G" /GRANT:Everyone,FULL /CACHE:None

 

yours might look like this:

 

FOR /D %%G IN (*.*) DO net share = /CACHE:no /GRANT:domain\user,READ|CHANGE|FULL

 

run this bat file from the folder containing folders to be shared.

or *.* probably will not work

 

BoX

  • Thanks 2
Posted
i use this:

 

FOR /D %%G IN (*.*) DO NET SHARE "%%G=%CD%\%%G" /GRANT:Everyone,FULL /CACHE:None

 

yours might look like this:

 

FOR /D %%G IN (*.*) DO net share = /CACHE:no /GRANT:domain\user,READ|CHANGE|FULL

 

run this bat file from the folder containing folders to be shared.

or *.* probably will not work

 

BoX

 

Thanks for that BoX, worked a treat :)

  • 8 years later...
Posted (edited)

This has been extremely helpful, thank you! I'm looking to take it one step further...

 

I have a file server with multiple folders on it that need to be shared out. Occasionally, the shares vanish, and need to be re-shared (beyond the scope of this question, I'm dealing with that as a separate issue). For now, I am looking for a way to easily automate the re-sharing of folders via a single BAT file when this happens, so that I am not manually re-sharing several dozen folders by hand.

 

There are a limited number of specific top-level folders that need to be shared - Users, Jobs, Operations, etc. - this is not a problem so far, I can manually specify them with a NET SHARE command.

 

Where I am running into an issue is for a handful of folders that need all of their sub-folders also shared. For example, every folder under D:\Shares\00_Users also needs to be shared as well.

 

The above command will share all files in the current directory, but not ones in another directory - meaning I need to leave a copy of the BAT in the share, which is not a good idea. Is there a way I can automate this so that the BAT file will share each sub-folder in another folder?

 

Here is my current code:

 

NET SHARE "ShareName1=D:\Shares\Share1" /GRANT:Everyone,FULL /CACHE:None

NET SHARE "ShareName2=D:\Shares\Share2" /GRANT:Everyone,FULL /CACHE:None

NET SHARE "ShareName3=D:\Shares\Share3" /GRANT:Everyone,FULL /CACHE:None

FOR /D %%G IN (D:\Shares\00_Users\*.*) DO NET SHARE "%%G=%%G" /GRANT:Everyone,FULL /CACHE:None

FOR /D %%G IN (D:\Shares\Jobs\*.*) DO NET SHARE "%%G=%%G" /GRANT:Everyone,FULL /CACHE:None

 

Where I am running into issues is the "%%G=%%G" - I want to replace the first part of the parameter with JUST the name of the folder, not the whole path.

 

Ideally this would share "D:\Shares\00_Users\JSmith" as a share named "JSmith". Currently, it's trying to set the share name to be the full path!

 

Thank you for your help!

Edited by GJSchaller
Posted

I found the solution on another site, I am sharing it here for future users. Here's my current BAT file with all the commands spelled out:

 

NET SHARE "D:\Business=Business" /GRANT:Everyone,FULL /CACHE:None

NET SHARE "D:\Engineering=Engineering" /GRANT:Everyone,FULL /CACHE:None

NET SHARE "D:\Jobs=Jobs" /GRANT:Everyone,FULL /CACHE:None

NET SHARE "D:\Operations=Operations" /GRANT:Everyone,FULL /CACHE:None

NET SHARE "D:\Sales=Sales" /GRANT:Everyone,FULL /CACHE:None

NET SHARE "D:\Service=Service" /GRANT:Everyone,FULL /CACHE:None

NET SHARE "D:\Scans=Scans" /GRANT:Everyone,FULL /CACHE:None

NET SHARE "D:\Users=Users" /GRANT:Everyone,FULL /CACHE:None

FOR /D %%G IN (D:\Engineering\*.*) DO NET SHARE "%%~nG%%~xG=%%G" /GRANT:Everyone,FULL /CACHE:None

FOR /D %%G IN (D:\Operations\*.*) DO NET SHARE "%%~nG%%~xG=%%G" /GRANT:Everyone,FULL /CACHE:None

FOR /D %%G IN (D:\Sales\*.*) DO NET SHARE "%%~nG%%~xG=%%G" /GRANT:Everyone,FULL /CACHE:None

FOR /D %%G IN (D:\Users\*.*) DO NET SHARE "%%~nG%%~xG=%%G" /GRANT:Everyone,FULL /CACHE:None

  • Thanks 1
Posted

If its server 2012 > new-smbshare, so something like new-smbshare -Name Myshare1 -path c:\myshare\share1 -fullaccess yourdomain\Administrator, yourdomain\user1

 

$fldrlist = gci c:\mysharedfolder\

$fldrlist | %{

New-smbshare -name $_.Name -path $_.FullName -fullaccess youdomain\Administrator, yourdomain\user

}

 

this will get all the folders and then we can pipe them into a foreach loop

 

Before windows 2012/ 8 no such thing as new-smbshare so would have to use the Win32_Share WMI Class

 

$wmiShare[WMICLASS]"WIN32_Share"

 

a method of the Win32_Share class is create so it would look something like this

$wmiShare.create("c:\myshare\share1","MyShare1",0)

 

this would do about the same as new-smbshare, the same looping methodology could be applied like above need to think about how to pull the right bits in to it.

 

I am not sat at a device that has PowerShell on so the above code is from memory and has not been run! oh and just to save trouble aliases in the above are GCI = Get-ChildItem and % = ForEach-Object

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...