-1
Set oShell = CreateObject("WScript.Shell")
strCommand = oShell.Run("Xcopy ""p:\"" ""d:\12"" /S /Y", 0, True)
If strCommand <> 0 Then
MsgBox "Error: Not_Copy" & strCommand
Else
MsgBox "Copy_Done"

End If

I use above vb script to copy all my data from a pen drive to "d:\12" folder. I have one problem with it.

Many times, I have same files with same folder path in the pendrive, like "java-setup.exe" in "p:\" also and also in "d:\12". When I play this VBScript file, it replaces all the files, and lots of time is wasted.

Is there any option which makes it it stop replacing files (skip already copied files), and focus on only those files which are not copied?

I am new in scripting, beginner level. so please give direct coding, not like this,

$srcdir = "\\server\source\";
$destdir = "C:\destination\";
$files = (Get-ChildItem $SrcDir -recurse -filter *.* | where-object {-not ($_.PSIsContainer)});
$files|foreach($_){
if (!([system.io.file]::Exists($destdir+$_.name))){
            cp $_.Fullname ($destdir+$_.name)
};

Many times i can't understand it.

My pen drive path is "p:\" and files where i have to copy is "d:\12"

please, provide script.

user3593505
  • 9
  • 1
  • 3
  • Use MS documentation http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx?mfr=true – Francis Ducharme May 01 '14 at 17:16
  • I think with the option **/D** it can be done ! So try my vbscript here ==> http://stackoverflow.com/questions/11832500/vb-script-copy-files-and-progress-bar/23177626#23177626 – Hackoo May 03 '14 at 23:47

2 Answers2

1

I couldn't find an XCOPY switch that does what you need. This VBScript should work, though.

With CreateObject("Scripting.FileSystemObject")
    For Each File In .GetFolder("P:\").Files
        If Not .FileExists("D:\12\" & File.Name) Then
            .CopyFile File.Path, "D:\12\" & File.Name
        End If
    Next
End With
Bond
  • 16,071
  • 6
  • 30
  • 53
  • please read my question above, i edited it. and try to give answer is you know. Thank You. – user3593505 May 05 '14 at 14:05
  • We're not here to write scripts for you. We're here to help *you* write a script. But if you knew anything at all about scripting you would have realized that *this is an entire script!* – Bond May 05 '14 at 14:20
1

Yeah, xcopy doesn't support that as others have stated. But there are batch commands that do.

Place this in ("CopyWithoutReplace.bat")

For %%F In ("P:\*.*") Do If Not Exist "D:\12\%%~nxF" Copy "%%F" "D:\12\%%~nxF"

And your modified script:

Set oShell = CreateObject("WScript.Shell")
strCommand = oShell.Run("P:\CopyWithoutReplace.bat", 0, True)
If strCommand <> 0 Then
MsgBox "Error: Not_Copy" & strCommand
Else
MsgBox "Copy_Done"
End If

Also you can use Robocopy:

Robocopy, or "Robust File Copy", is a command-line directory replication command. It has been available as part of the Windows Resource Kit starting with Windows NT 4.0, and was introduced as a standard feature of Windows Vista, Windows 7 and Windows Server 2008.

Or you could use Powershell

$srcdir = "\\server\source\";
$destdir = "C:\destination\";
$files = (Get-ChildItem $SrcDir -recurse -filter *.* | where-object {-not ($_.PSIsContainer)});
$files|foreach($_){
if (!([system.io.file]::Exists($destdir+$_.name))){
            cp $_.Fullname ($destdir+$_.name)
};

And finally: Here is an entire SO Thread on copying without replacing.

Community
  • 1
  • 1
Rich
  • 4,134
  • 3
  • 26
  • 45