'========================================================================== ' ' VBScript Source File ' ' NAME: copySources.vbs ' ' AUTHOR: Joe Erskine ' ' ' DATE: 18/07/2006 ' ' VERSION: 1.0 ' ' COMMENT: SMS script to copy sources files. Set path for destination in strTargetPath and place ' fiels/folders to be copied to location in a sub-folder called SOURCE in the package source directory ' E.g. If package source is C:\Test, place this script in C:\Test and files/folders to transfer in C:\Test\Source ' ' USAGE: cscript copySources.vbs ' '============== 'Version Control '=============== ' 'Ver #: 'Modified By: 'Date Modified: 'Details: '=================== 'End Version Control '=================== '========================================================================== Option Explicit On Error Resume Next '====================== 'User Defined Variables '====================== Dim strTargetPath '<- Path to copy files/folders to, Created if it doesn't exist Dim strWinDir '<- Windows Installation Directory 'Get the Windows Installation Directory path strWinDir = fGetWindowsDirectory() '<- If you need to copy to Windows directory then use: ' strTargetPath = strWindir & "Your Path Here" ' E.g. strTargetPath = strWindir & "\System32\MyFiles" strTargetPath = "C:\Install\SMS-SCCM-Migrate\" '============== 'Global Objects '============== Dim objFS Dim objItem Dim objFolder Dim objShell Dim objNetwork Dim colItems Dim strScriptPath Dim strCacheRoot Dim strSource Dim intError : intError = 0 Dim strComment Const FOR_READING = 1 Const FOR_WRITING = 2 Const FOR_APPENDING = 8 Const CMD_MINIMIZED = 2 Const CMD_WAIT = True Const OVERWRITE_EXISTING = True '===== 'START '===== strScriptPath = Left(WScript.ScriptFullName,_ Len(WScript.ScriptFullName) - Len(WScript.ScriptName)) strSource = strScriptPath & "Source" strCacheRoot = Left(strScriptPath,(Len(strScriptPath)) - 1) strComment = "SMS Source Files Transfer Script" & vbNewLine strComment = strComment _ & "************************************************************" & vbNewLine strComment = strComment & "Start Time:" & vbTab & Now & vbNewLine strComment = strComment & "Source Folder:" & vbTab & strSource & vbNewLine strComment = strComment & "Target Folder:" & vbTab & strTargetPath & vbNewLine strComment = strComment _ & "************************************************************" & vbNewLine Set objFS = CreateObject("Scripting.FileSystemObject") Set objShell = CreateObject("WScript.Shell") Set objNetwork = CreateObject("WScript.Network") If objFs.FileExists(WScript.ScriptFullName) Then objFs.DeleteFile(WScript.ScriptFullName) WScript.Echo strScriptPath WScript.Echo strCacheRoot If Len(strTargetPath) > 0 Then If objFS.FolderExists(strTargetPath) Then Else 'Target folder doesn't exists so create it strComment = strComment & "Creating Folder:" & vbTab & strTargetPath & vbNewLine objShell.Run "%comspec% /c MD " & """" & strTargetPath & """",CMD_MINIMIZED,CMD_WAIT WScript.Sleep 2000 If Not objFS.FolderExists(strTargetPath) Then intError = intError + 1 strComment = strComment & "ERROR: Unable to create target folder -> " & strTargetPath & vbNewLine End If End If If intError = 0 Then If Right(strTargetPath,1) = "\" Then Else strTargetPath = strTargetPath & "\" End If Set objFolder = objFS.GetFolder(strSource) For Each objItem In objFolder.Files If objFS.FileExists(strTargetPath & "\" & objItem.Name) Then strComment = strComment & "ERROR: Target file already exists -> " _ & strTargetPath & "\" & objItem.Name & vbNewLine strComment = strComment & vbTab & "- Skipping move operation" & vbNewLine Else strComment = strComment & "Moving -> " _ & objItem.Path & vbNewLine WScript.Echo objItem.Path Err.Clear objFS.MoveFile objItem.Path,strTargetPath If Err <> 0 Then strComment = strComment & vbTab _ & " - ERROR: " & Err.Number & Err.Descripton & vbNewLine intError = intError + 1 End If End If Next For Each objItem In objFolder.SubFolders If objFS.FolderExists(strTargetPath & "\" & objItem.Name) Then strComment = strComment & "ERROR: Target folder already exists -> " _ & strTargetPath & "\" & objItem.Name & vbNewLine strComment = strComment & vbTab & "- Deleting target folder" & vbNewLine objFS.DeleteFolder(strTargetPath & "\" & objItem.Name) Else strComment = strComment & "Moving -> " _ & objItem.Path & vbNewLine WScript.Echo objItem.Path Err.Clear objFS.MoveFolder objItem.Path,strTargetPath If Err <> 0 Then strComment = strComment & vbTab _ & " - ERROR: " & Err.Number & Err.Descripton & vbNewLine intError = intError + 1 End If End If Next Set objFolder = Nothing Else intError = intError + 1 End If Else strComment = strComment & "ERROR:" & vbTab _ & "No Target path specified" & vbNewLine End If strComment = strComment _ & "************************************************************" & vbNewLine strComment = strComment & "Exit Code:" & vbTab & intError & vbNewLine strComment = strComment & "************************************************************" Call fLogEvent(strComment) Set objShell = Nothing Set objFS = Nothing Set objNetwork = Nothing WScript.Quit(intError) '=== 'END '=== '========== 'Functions '========= '****************************************************************************** '* Name: fLogEvent(strventInfo) '* Function: Write Script run time log to the Application Event Log '****************************************************************************** Function fLogEvent(strEventInfo) objShell.LogEvent 4,strEventInfo,"\\" & objNetwork.ComputerName End Function '****************************************************************************** '* Name: fGetWindowsDirectory() '* Function: Returns a string with the Windows Installation directory '****************************************************************************** Function fGetWindowsDirectory() Dim colItems Dim objItem Dim objWMIService Dim strValue Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\.\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * From Win32_OperatingSystem") For Each objItem in colItems strValue = objItem.WindowsDirectory Next Set objWMIService = Nothing fGetWindowsDirectory = strValue End Function