Solved

VB Script Running Terribly Slow

Posted on 2009-07-01
1
448 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:b4sherx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 38

Accepted Solution

by:
PaulHews earned 500 total points
ID: 24760347
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question