• Notice regarding image hosts and attachments. See here.

Anyone good with vb scripts?

Man of Honour
Joined
17 Oct 2002
Posts
9,712
Location
Retired Don
Hi guys,

We use the following script to create a new folder within a user's home directory, and then share is foldername$

Would anyone be able to edit it so it skips a step, eg shares the actual user folder as foldername$

Say there is a folder, called D:\Users, within that folder are folders Folder1, Folder2, Folder3 etc etc

We'd want to share thoose folders as Folder1$, folder2$, folder3$ etc

Cheers guys!

Mal

'Change the following line to true or false depending on whether a log file is required
bolCreateLog = true

strInput = inputbox( "Enter the name of the folder where your users' folders are stored eg. D:\Users" )
strServer = inputbox( "Enter the name of the server which holds the users home folders eg. SERVER" )

if bolCreateLog then
strLogFileName = InputBox( "Enter the full path and filename of the logfile to create eg D:\log.txt" )
Dim objFSO, objFile
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
if not objFSO.FileExists( strLogFileName ) then
Set objFile = objFSO.CreateTextFile( strLogFileName, true )
else
msgbox "Log file already exists. Please run this script again and choose another name for the logfile."

end if
Set objFSO = nothing
end if
on error resume next

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

Set folder = objFSO.GetFolder( strInput )

err.clear
for each subfolder in folder.subfolders

for each subf in subfolder.subfolders

if subf.name = "home" then
Set objADSI = GetObject( "WinNT://" & strServer & "/lanmanserver" )

set objHomeShare = objADSI.Create( "FileShare", subfolder.name & "$")
Set objADSI = nothing
objHomeShare.Put "Path", subf.path
objHomeShare.Put "Description", "home share for " & subfolder.name
objHomeShare.SetInfo
Set objHomeShare = nothing
if bolCreateLog then
if Err.number then
objFile.WriteLine subfolder.Name & " home folder sharing failed with error " & err.number
else
objFile.WriteLine subfolder.Name & " home folder shared successfully"
end if
end if
Err.clear
end if

next
next

Would quite appreciate if someone could explain the terms to me as I like to learn!!

Cheers
 
By the looks of it all you need to do is comment out the line where it looks if there is a directory call 'home'.
Code:
'Change the following line to true or false depending on whether a log file is required
bolCreateLog = true

strInput = inputbox( "Enter the name of the folder where your users' folders are stored eg. D:\Users" )
strServer = inputbox( "Enter the name of the server which holds the users home folders eg. SERVER" )

if bolCreateLog then
    strLogFileName = InputBox( "Enter the full path and filename of the logfile to create eg D:\log.txt" )
    Dim objFSO, objFile
    Set objFSO = CreateObject( "Scripting.FileSystemObject" )

    if not objFSO.FileExists( strLogFileName ) then
        Set objFile = objFSO.CreateTextFile( strLogFileName, true )
    else
        msgbox "Log file already exists. Please run this script again and choose another name for the logfile."
    end if

    Set objFSO = nothing
end if

on error resume next

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

Set folder = objFSO.GetFolder( strInput )

err.clear
for each subfolder in folder.subfolders

    for each subf in subfolder.subfolders
        
        'Will not look for home now, will do all Directories.
        'if subf.name = "home" then
            Set objADSI = GetObject( "WinNT://" & strServer & "/lanmanserver" )

            set objHomeShare = objADSI.Create( "FileShare", subfolder.name & "$")
            Set objADSI = nothing
            objHomeShare.Put "Path", subf.path
            objHomeShare.Put "Description", "home share for " & subfolder.name
            objHomeShare.SetInfo
            Set objHomeShare = nothing

            if bolCreateLog then
                if Err.number then
                    objFile.WriteLine subfolder.Name & " home folder sharing failed with error " & err.number
                else
                    objFile.WriteLine subfolder.Name & " home folder shared successfully"
                end if
            end if

            Err.clear

        'end if
    next
next
Give it a bash...

TrUz
 
No joy i'm afraid, it now shares the first sub folder within the user's folder.

Is that something to do with this bit:

for each subfolder in folder.subfolders

for each subf in subfolder.subfolders

Cheers,

Mal

/edit - Not to worry, i've managed to do it now:

Code:
const MaxConnections = 0

set objFSO = createobject("Scripting.FileSystemObject")

RootFolder = inputbox("Please enter the root folder that contains the folders you want to share:")

'***** Perform some basic validation *****
if not objFSO.FolderExists(RootFolder) then
	wscript.echo "Invalid Folder"
	wscript.quit
end if
if mid(RootFolder,1,2)="\\" then
	wscript.echo "UNC Paths are not supported"
	wscript.quit
end if

set objRootFolder = objFSO.GetFolder(rootfolder)
for each fldr in objRootFolder.SubFolders
	sharefolder fldr.path, fldr.name & "$"
next

wscript.echo "Completed"

sub shareFolder(byval folderPath,shareName)
	strComputer = "."
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
							strComputer & "\root\cimv2")
	Set objNewShare = objWMIService.Get("Win32_Share")

	objNewShare.Create folderpath, shareName, MaxConnections
end sub
 
Last edited:
Back
Top Bottom