Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1599
  • Last Modified:

Visual Basic Script Create Folder With User Variable

Hi,

I am attempting to create a visual basic script (yes VB is what I want LOL), I have a sample script included below that checks if a folder does not exist and if so creates it however what I need to do and I am lost on this part is how to check if:

\\fileserver\USERNAME\My Documents doesnt exist, if it does not then create it.

This needs to be in this format because of our thin client project, and will be redirecting My Documents etc to a share drive so that users will not lose their files when they log out.

Thanks


Sample code:

Dim objFSO, strFolder
strFolder = "C:\Temp"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(strFolder) Then
   objFSO.CreateFolder(strFolder)
End If
0
smyers051972
Asked:
smyers051972
  • 7
  • 5
  • 4
1 Solution
 
mlongohCommented:
I think part of your challenge will be the creation of the intermediate folders.  Try this:

On Error Resume Next
Set wsShell = CreateObject("WScript.Shell")
Dim objFSO, strFolder
strFolder = "C:\Frank\Bob\Test"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(strFolder) Then
   wsShell.Run "cmd /c md """ & strFolder & """", 0, 1
End If
0
 
mlongohCommented:
It will create the whole directory structure without having to manually create each folder individually.
0
 
smyers051972Author Commented:
Well the users folder already is present via UNC:

\\fileserver\users\Bob\ for example.

We need to do:

\\fileserver\users\%username%\My Documents

Like that.
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
mlongohCommented:
OK.  Is this something that will be run at user logon?
0
 
smyers051972Author Commented:
Yea, everytime they log on, if they are a member of "VDI" this script would run in GP.
0
 
mlongohCommented:
OK.  Try this.

On Error Resume Next
Set wsShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("Wscript.Network")
Dim objFSO, strFolder
strUser = objNetwork.UserName
strFolder = "\\fileserver\users\" & strUser & "\My Documents"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(strFolder) Then
   wsShell.Run "cmd /c md """ & strFolder & """", 0, 1
End If
0
 
yo_beeDirector of ITCommented:
Can I ask why are you not using GP to handle this?
If create a TEST OU that has a GPO with Folder Rediection enabled this should create the directory structure you are looking for.

For Windows XP users and Active Directory 2003 Schema
http://technet.microsoft.com/en-us/library/cc786749(v=ws.10).aspx

Best Practices for Folder Redirection in User Data and Settings Management
http://technet.microsoft.com/en-us/library/cc784630(v=ws.10)

Folder Redirection for Window (Vista,7 & 8)
http://technet.microsoft.com/en-us/library/cc732275.aspx
0
 
smyers051972Author Commented:
@yo_bee: its mainly my personal preference what I am used too however we arent running server 2003 we run 2008 and have XP for clients still. Too many hassles and its easier for me to manage 1 script.
0
 
yo_beeDirector of ITCommented:
I hear you, but you are still able to use Server 2008 Folder redirection on XP.
You will also need to deal with NTFS security which is taken care of my GP when creating the folder structure.
0
 
smyers051972Author Commented:
hmmm so it would work in XP?

I am asking because we are using kaviza as our VDI environment.

What happens is machines are spawned off templates, the user gets a fresh desktop every time, when they log out, the desktop they were using is destroyed so any files saved on their desktop or my documents would be lost, I am just trying to use redirection to a NAS share so every time they log back in, the desktop redirection takes place restoring their original files in place.

Am I way off base here or ?
0
 
yo_beeDirector of ITCommented:
I would create a test OU with a test user or two and see the results and if it works you are golden.

If not then you go back to your other method, but I think you will be fine.
0
 
smyers051972Author Commented:
Ok ill try this out and let you know.
0
 
smyers051972Author Commented:
One question I do have would the profiles be stored there too? I am asking because the initial logons are always so long on a computer for the first time and since these are virtual desktops they would always be new...
0
 
yo_beeDirector of ITCommented:
You will need roaming profile enbled to have that work.
So with a combo of Roaming and Folder Redirection you will accomplish your goal
0
 
smyers051972Author Commented:
Not completed but I did some testing and I know this will work as per what he is saying, so I will close this case knowing it is a solid solution as per his recommendation.
0
 
yo_beeDirector of ITCommented:
Ok.
Thanks for the points and if you  need further assistance just post back here and I will respond.

-Mike B-
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

  • 7
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now