Windows 7 - Xcopy32.exe

PJ_in_FL
5StarLounger
Posts: 1118
Joined: 21 Jan 2011, 16:51
Location: Florida

Re: Windows 7 - Xcopy32.exe

Post by PJ_in_FL »

I found a neat and fast method for checking remote servers. See the TestPing function. Also added capability to use drive-letter mapped locations for the fun of it. Also helped when I was doing debugging to have a local destination..

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
PJ in (usually sunny) FL

BenCasey
4StarLounger
Posts: 495
Joined: 13 Sep 2013, 07:56

Re: Windows 7 - Xcopy32.exe

Post by BenCasey »

Well done, PJ.

I am glad you got it sorted to your satisfaction.

Nothing succeeds like perseverance!!
Regards, Ben

"Science is the belief in the ignorance of the experts."
- Richard Feynman