Link to home
Start Free TrialLog in
Avatar of Moey_G
Moey_GFlag for Australia

asked on

How to create a folder using VB Script

Hi All,

I need to create an additional folder in the Documents folder called "SharePoint" using an existing  VB script. I am pretty much new to VB , but this will be a logon script using Group Policy. I pretty much can do this using a batch file by UNC (\\servername\users$\%username%\documents\Sharepoint ).

Can this be done using the existing VB Script ?

Moey
Avatar of Bill Prew
Bill Prew

Yes, as long as you have permissions you can do this using something like this example:

Function CreateFolderDemo
   Dim fso, f
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.CreateFolder("c:\New Folder")
   CreateFolderDemo = f.Path
End Function

Reference is here:

http://msdn.microsoft.com/en-us/library/7kby5ae3(v=VS.85).aspx

~bp
I assume this are you homedrive folder for the users on a server.
Sou you could execute the following script one time directly on the server to create the sharepoint folder in each user folder:
Set objFSO = CreateObject("Scripting.FileSystemObject")
set objFolder = objFSO.GetFolder("D:\Users")

'Now loop throught each user folder
For each subfolder in objFolder.SubFolders
	objFSO.CreateFolder subfolder &"\Sharepoint"
Next

Open in new window

If you need to use the username variable you can use the ExpandEnvironmentStrings to access the username variable and use that in your folder path:


Dim objFso, ofolder

Set oShell = CreateObject( "WScript.Shell" )
user=oShell.ExpandEnvironmentStrings("%UserName%")
Set objFso = CreateObject("Scripting.FileSystemObject")
Set ofolder = objFso.CreateFolder("C:\Users\" & user & "\Sharepoint")

Open in new window

%USERPROFILE% delivers the full path directly ;)
Avatar of Moey_G

ASKER

sorry for the late reply all . I have tried all the recommendations except for running the vb script on the server.

I am still unable to see the folder once I run the logon script on a test machine. Any ideas what will be the cause?
Avatar of Moey_G

ASKER

Corrected the script which now creates the folder. Ideally , the folder needs to be created in the Documents folder which is re-directed to the File server. The script is creating a folder in the
%useprofile% folder. This is not re-directed to the server. The Documents folder is re-directed user Group Policy (\\fileserver\users$\%username%\Documents ).

Any other ideas ?

Sorry merowinger , your script will need to be run everyday to make sure all users have the folder. I would prefer this to be part of the logon script.
this should then do it
Set objShell = CreateObject( "WScript.Shell" ) 
strUserProfile = objShell.ExpandEnvironmentStrings("%USERPROFILE%") 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set ofolder = objFSO.CreateFolder(strUserProfile &"\Documents\Sharepoint")

Open in new window

Avatar of Moey_G

ASKER

Hi merowinger,

Thank you for your help thus far. I am still not having the folder created to the right directory. When you enable Group policy for folder redirection , it creates another path pointing to the server on the network via the properties of documents on a windows 7 machine.

The above works great if you are trying to create a directory locally on the machine ie ( C:\users\%username%\Documents ).

I need the vb script to create a folder on the network (\\fileserver\users$\%username%\Documents ).

Apologies if I have not explained this clearly.
ASKER CERTIFIED SOLUTION
Avatar of merowinger
merowinger
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Moey_G

ASKER

Works a treat !!! Thanks for all your help.