Overall, I've come up with a solution that may be general enough for others to learn from my simple blundering to a solution. I'll be trying this on large scale folders over the weekend and report any changes I find are needed, and possibly implementing some of the error checking and validation I noted as future enhancement opportunities.
Code: Select all
'===========================================================================================
' ROBOCOPIES FOLDERS TO SERVER / SHARE LOCATION. DESTINATION IS PASSED IN AS FIRST PARAMETER (Arg(0))
'
' USE DRAG AND DROP ONTO THE DESKTOP SHORTCUT TO PASS THE FOLDERS TO THE SCRIPT
'
' DESKTOP SHORTCUT TARGET:
' Wscript.exe "ThisScript.vbs" "\\Server\Folder"
' Wscript.exe "ThisScript.vbs" "S:\Folder" where "S:" is any mapped drive letter
'
' DEBUGGING:
' SET DBUG TRUE, RUN WITH CSCRIPT
'
' BACKGROUND OPERATION WITHOUT CONSOLE WINDOWS
' SET DBUG TO FALSE, SHOWCONSOLE TO FALSE, RUN WITH WSCRIPT
'===========================================================================================
Option Explicit
dim DBUG, SHOWCONSOLE, i, strLogFile, objArgs, objWS, objFSO, fldDest, fldSrc, strArg, strCmd, strSrv, lngCmdLine
' Set up debugging and/or error window flag to show the screen if an error occurs
DBUG = false ' SET TO TRUE TO SHOW ALL THE DEBUGGING AID WRITE STATEMENTS. BEST TO RUN WITH CSCRIPT.
SHOWCONSOLE = FALSE ' SET TO TRUE TO SHOW THE OUTPUT WINDOWS FOR EACH COMMAND. NOTE THAT WITH A LOGFILE
' SPECIFIED, THERE WILL BE LITTLE SHOWN IN THE CONSOLE WINDOW
' Set up a log file for the output (optional)
strLogFile = "C:\tmp\CopyToServer_Log.Txt"
' Store the arguments in an object variable:
Set objArgs = WScript.Arguments
Set objWS = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Get the destination as the first argument passed
fldDest = objArgs(0)
' Is it mapped or UNC? Is is available?
if mid(fldDest,2,2) = ":\" then
if DBUG then WScript.Echo "Is MAPPED? " & fldDest
if NOT objFSO.DriveExists(left(fldDest,3)) then
MsgBox " Bad server name !!! " & fldDest & " drive not found.", vbOK + vbExclamation
WScript.Exit
end if
elseif left(fldDest,2) = "\\" then
if DBUG then WScript.Echo "Is UNC? " & fldDest
' Extract the root server location from the destination
strSrv = mid(fldDest,3,InStr(3,fldDest,"\")-3)
if DBUG then WScript.Echo "UNC? " & strSrv
if TestPing(strSrv) then
if DBUG then WScript.Echo "Server " & strSrv & " is OK"
else
MsgBox " Bad server name !!! " & fldDest & " drive not found.", vbOK + vbExclamation
if DBUG then WScript.Echo " Bad server name !!! " & fldDest & " drive not found.", vbOK + vbExclamation
WScript.Exit
end if
else
MsgBox " Bad server name entered as first parameter!!! " & fldDest, vbOK + vbExclamation
WScript.Exit
end if
lngCmdLine = 0
' Show Count of arguments
if DBUG then WScript.Echo "Count = " & objArgs.Count
i = 1
for i = 1 to objArgs.Count - 1
strArg = objArgs(i)
lngCmdLine = Len(strArg) + lngCmdLine
if DBUG then WScript.Echo " Input: " & strArg
' Only process folders. Ignore any files dropped onto the command...
if objFSO.FolderExists(strArg) then
' ### ADD ABORT/RETRY CODE HERE TO CHECK THE SERVER STATUS BEFORE EACH TRANSFER STARTS
fldSrc = Right(strArg,Len(strArg)-InStrRev(strArg,"\"))
' USE /K TO SEE THE OUTPUT
strCmd = "cmd.exe /C ROBOCOPY " & Chr(34) & strArg & Chr(34) & " " & Chr(34) & fldDest & "\" & fldSrc & Chr(34) & " /R:0 /W:0 /E"
if len(strLogFile) > 0 then strCmd = strCmd & " /LOG+:" & Chr(34) & strLogFile & Chr(34)
if DBUG then ' Just show details during debugging
WScript.Echo " Source folder: " & fldSrc
WScript.Echo " Destination folder: " & fldDest & "\" & fldSrc
WScript.Echo " Command: " & strCmd
else ' Run the command when debugging is over!
if SHOWCONSOLE THEN
objWS.Run strCmd , 1, true
else
objWS.Run strCmd , 0, true
end if
' ### ADD ERROR CHECKING / FILE VERIFICATION HERE TO CONFIRM ALL THE FILES WERE COPIED -- MAYBE CHECK FILE COUNTS AT SRC AND DEST
end if
end if
Next
if DBUG then WScript.Echo "Total arguments passed = " & cstr(objArgs.Count) & " with total length = " & (lngCmdLine)
'added line to keep the CSCRIPT window open when debugging -- fails when using WSCRIPT (no handle created)
if DBUG then WScript.stdin.readline
'=========================================================
Function TestPing(sName)
Dim cPingResults
Set cPingResults = GetObject("winmgmts:").Get("Win32_PingStatus.Address='" & sName & "'")
TestPing = (cPingResults.StatusCode = 0)
End Function