Search code examples
vbscriptxcopyfilesystemobject

VBScript XCopy to local disk not working


I am trying to develop a simple script that copies files to specific drives.

the script does the following:

  1. Check for physical drive with letter
    • If drive exists copy files.
    • If no drive move to step 2.
  2. Check for network drive with letter
    • If drive exists copy files.
    • If no drive move to step 3.
  3. Create new network mapping
    • Copy files to drive

When a network drive exists, or a network mapped drive is created via the script the XCopy commands works perfectly. The problem is with step 1 and a local drive already exists, no files are copied to the drive after calling XCopy.

Here is my code:

strLocalDrive = "E:"
strRemoteShare = "\\127.0.0.1\c$\Program Files (x86)\MyFolder\EDrive"
bolFoundExisting = False
source = "C:\Program Files (x86)\MyFolder\EDrive\*" 
destination = "E:\" 

' Check parameters passed make sense
If Right(strLocalDrive, 1) <> ":" OR Left(strRemoteShare, 2) <> "\\" Then
 'wscript.echo "Usage: cscript MapDrive.vbs drive fileshare //NoLogo"
  WScript.Quit(1)
End If

'wscript.echo " - Mapping: " + strLocalDrive + " to " + strRemoteShare

'Set objNetwork = WScript.CreateObject("WScript.Network")
Set objNetwork = CreateObject("WScript.Network")
Set oShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Loop through the network drive connections and disconnect any that match strLocalDrive
Set objDrives = objNetwork.EnumNetworkDrives

' first check that physical drive does not exist
If objFSO.DriveExists(strLocalDrive) Then 
    WScript.echo "Physical Drive Found"
    bolFoundExisting = True
ElseIf objDrives.Count > 0 Then
  For i = 0 To objDrives.Count-1 Step 2
    If objDrives.Item(i) = strLocalDrive Then
      strShareConnected = objDrives.Item(i+1)
      'objNetwork.RemoveNetworkDrive strLocalDrive, True, True
      i=objDrives.Count-1
      bolFoundExisting = True
    End If
  Next
End If



If bolFoundExisting <> True Then
    WScript.echo "Drive DOES NOT exists"
  Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
  objReg.GetStringValue HKCU, "Network\" & Left(strLocalDrive, 1), "RemotePath", strShareConnected
  If strShareConnected <> "" Then
    Set objReg = Nothing
    bolFoundRemembered = True
  End If

  'Now actually do the drive map (not persistent)
    Err.Clear
    On Error Resume Next
    objNetwork.MapNetworkDrive strLocalDrive, strRemoteShare, False

Else
    ' Drive exists copy files
    WScript.echo "Drive exists"
    oShell.Run "xcopy.exe " & source & " " & destination & " /C /D /E /H /I /K /R /S /Y"
    Set oShell = Nothing 
End IF

I would really appreciate it if some one could explain why the XCOPY command only copies files to network drives and not to local drives! TIA!

UPDATE I have realized that the problem is being caused by spaces in the path name. What is strange is that copying to network drives does work, but not to physical drives. How can I handle the spaces in the path name?


Solution

  • Your paths have spaces in them which wont expand well for running the xcopy command. Much in the same way that you need to use quotes on the command line you need to quote your strings for .Run. My preferred way of inserting double quotes is using Chr(34). 34 being the ANSI code for double quotes

    A simple example would be chr(34) & "quotedstring" & chr(34). For your case you could use something like this:

    oShell.Run "xcopy.exe " & chr(34) & source & chr(34) & " " & chr(34) & destination & chr(34) & " /C /D /E /H /I /K /R /S /Y"
    

    For readabilty you can always use the underscore to break the lines up

    oShell.Run "xcopy.exe " & chr(34) & source & chr(34) & " " & _
        chr(34) & destination & chr(34) & _
        " /C /D /E /H /I /K /R /S /Y"