VB Script Running Terribly Slow

I am writing a script to go through and migrate all users to a new server in a certain folder on a remote path. The procedure order is this:
1. List the folder to migrate
2. User selects the folder
3. Enumerate the users (folders)  in the folder by outputting a dir command to a text file.
4. Read the text file into a loop then:
        a. Unshare the folder
        b. xcopy their data (without the /X permissions copy)
        c. Set the correct permissions with cacls
        d. Then make the changes in AD.
5. Remove the dir.txt file originally made.

Currently, it is running horrifically slow, as I am testing it with some shared folders i made on the server. I think the problem might be with the "delete share" section and getting a list of all the shares since there are about...over 1000 user shares on this server. I'm just get my hands on VB Script so if I'm over complicating something, let me know.

Any suggestions?

Const WINDOW_HANDLE = 0
Const OPTIONS = 0
strServer = "fileserver1"
strLett = "D$"
 
'Prompt user to select a folder to migrate
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace("\\" & strServer & "\" & strLett)
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path
 
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder _
	(WINDOW_HANDLE, "Select a folder to migrate to NewStorageServer:", OPTIONS, strPath) 
      
If objFolder Is Nothing Then
    Wscript.Quit
End If
 
Set objFolderItem = objFolder.Self
objPath = objFolderItem.Path
 
'Get the last folder in Path
arrServ = Split(objPath, "\")
strIdx = UBound(arrServ)
strFold = arrServ(strIdx)
 
strContin = InputBox("This will unshare and migrate all users in the " & strFold & " folder to \\NewStorageServer\Students\ directory. Enter Yes to continue otherwise press enter to quit.", "Continue with migration?")
If strContin = "Yes" then
 
'Create the folder list to migrate
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd /c dir " & objPath & " /b > c:\dir.txt",, True
WshShell.Run "cmd /c c:"
strFilename = "C:\dir.txt"
 
Dim arrFileLines()
i = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("" & strFilename & "", 1)
Do Until objFile.AtEndOfStream
	ReDim Preserve arrFileLines(i)
	arrFileLines(i) = objFile.ReadLine
	strUsrShare = arrFileLines(i) & "$"
	strSrcPath = objPath & "\" & arrFileLines(i)
    strDestinationPath = "\\NewStorageServer\Students\" & arrFileLines(i)
    
    'Delete the share
    Set objWMIService = GetObject("winmgmts:" _
    	& "{impersonationLevel=impersonate}!\\" & strServer & "\root\cimv2")
	Set colShares = objWMIService.ExecQuery _
    	("Select * from Win32_Share Where Name = '" & strUsrShare & "'")
	For Each objShare in colShares
    	objShare.Delete
	Next
 
    'Copy the data and set the permissions
    WshShell.Run "xcopy """ & strSrcPath & """ " & strDestinationPath & " /E /I /V /H /Y",, True
    WshShell.Run "cacls " & strDestinationPath & " /T /E /G domain\" & arrFileLines(i) & ":C ""domain\Domain Admins"":F",, True
	'WshShell.Run "cacls " & strDestinationPath & " /T /E /R Everyone",, true
    WScript.Echo arrFileLines(i) & " has been copied."
 
    i = i + 1
 
Loop
 
WshShell.Run "cmd /c del c:\dir.txt"
WScript.Echo objPath & " has been migrated."
 
Else
WScript.Quit
End If

Open in new window

LVL 1
b4sherxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PaulHewsCommented:
It may just be a slow process, especially if there's a lot of files to xcopy.  But you can enumerate the folders with the filesystemobject and you really don't need an array to store the folder names in because you don't need to save them anywhere.
Const WINDOW_HANDLE = 0
Const OPTIONS = 0
strServer = "fileserver1"
strLett = "D$"
 
'Prompt user to select a folder to migrate
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace("\\" & strServer & "\" & strLett)
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path
 
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder _
        (WINDOW_HANDLE, "Select a folder to migrate to NewStorageServer:", OPTIONS, strPath) 
      
If objFolder Is Nothing Then
    Wscript.Quit
End If
 
Set objFolderItem = objFolder.Self
objPath = objFolderItem.Path
 
'Get the last folder in Path
arrServ = Split(objPath, "\")
strIdx = UBound(arrServ)
strFold = arrServ(strIdx)
 
strContin = InputBox("This will unshare and migrate all users in the " & strFold & " folder to \\NewStorageServer\Students\ directory. Enter Yes to continue otherwise press enter to quit.", "Continue with migration?")
If strContin = "Yes" then
 
    'Create the folder list to migrate
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(objPath)
    Set colSubfolders = objFolder.Subfolders
    For Each objSubfolder in colSubfolders
        strUsrShare = objSubfolder.Name & "$"
        strSrcPath = objPath & "\" & objSubfolder.Name
        strDestinationPath = "\\NewStorageServer\Students\" & objSubfolder.Name
        'Delete the share
        Set objWMIService = GetObject("winmgmts:" _
            & "{impersonationLevel=impersonate}!\\" & strServer & "\root\cimv2")
            Set colShares = objWMIService.ExecQuery _
            ("Select * from Win32_Share Where Name = '" & strUsrShare & "'")
        For Each objShare in colShares
            objShare.Delete
        Next
     
        'Copy the data and set the permissions
        WshShell.Run "xcopy """ & strSrcPath & """ " & strDestinationPath & " /E /I /V /H /Y",, True
        WshShell.Run "cacls " & strDestinationPath & " /T /E /G domain\" & arrFileLines(i) & ":C ""domain\Domain Admins"":F",, True
            'WshShell.Run "cacls " & strDestinationPath & " /T /E /R Everyone",, true
        WScript.Echo arrFileLines(i) & " has been copied."
        
    Next
     
 
 
    WScript.Echo objPath & " has been migrated."
 
Else
    WScript.Quit
End If

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.