Reputation: 1381
I just can't seem to find a way on the command line to say "copy all the files from directory A to directory B, but if the file already exists in directory B, don't overwrite it, no matter which file is newer, and don't prompt me."
I have been through copy, move, xcopy & robocopy, and the closest I can get is that you can tell robocopy "copy A to B, but don't overwrite newer files with older files," but that doesn't work for me. I looked at xxcopy, but discarded it, as I don't want to have a third-party dependency on a Visual Studio post-build event that will require other SVN users to have that tool installed in order to do the build.
I want to add a command line to the post-build event in Visual Studio 2010 so that the files that are generated from the T4 templates for new EF model objects get distributed to the project folders to which they belong, but regenerated files for existing objects don't overwrite potentially edited destination files.
Since the T4 template regenerates, the source file is always newer, and I can't use the "newer" switch reliably, I don't think.
I use partial classes for those items for which I can, but there are other things I generate that can't use partial classes (e.g. generating a default EditorTemplate or DisplayTemplate *.ascx file).
Any one have any similar problems they have solved?
Upvotes: 130
Views: 275175
Reputation: 1
If you are trying to put in files without overwriting existing files, you have a few options.
rename those files by, for example, putting "(1)" or "(2)" or something like it after the file name; that way you can distinguish between the two easier.
completely bypass the command line and use Windows Explorer. Unless if you don't have Windows.
That is all I can think of. Hope this helps.
Upvotes: 0
Reputation: 3188
We can use XCOPY command to copy files from source to destination folder without overriding any existing files in target directory.
First get a list of file names which already existing in destination using xcopy option /L/U/Y and write it in some temporary file say ExcludeFiles.txt.
Then again use XCOPY command with /EXCLUDE: option and provide file path that contain list of file names that need to be excluded. Create a batch file say "Copy-Files.bat" with below content and enjoy! Modify if you want to ask user input for source and destination directory path.
@echo off
set source="C:\Dir1"
set destination="C:\Dir2"
echo Copying non-existing files. NO OVERWRITE!
xcopy %source% %destination% /L/U/Y > %TEMP%\ExcludeFiles.txt
xcopy %source% %destination% /EXCLUDE:%TEMP%\ExcludeFiles.txt
pause
Upvotes: 0
Reputation: 4441
robocopy src dst /MIR /XX
/XX : eXclude "eXtra" files and dirs (present in destination but not source). This will prevent any deletions from the destination. (this is the default)
Upvotes: 3
Reputation: 61016
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.
robocopy c:\Sourcepath c:\Destpath /E /XC /XN /XO
To elaborate (using Hydrargyrum, HailGallaxar and Andy Schmidt answers):
/E
makes Robocopy recursively copy subdirectories,
including empty ones. /XC
excludes existing files with
the same timestamp, but different
file sizes. Robocopy normally overwrites those. /XN
excludes existing files newer
than the copy in the destination
directory. Robocopy normally
overwrites those. /XO
excludes
existing files older
than the copy in the destination
directory. Robocopy normally
overwrites those.With the Changed, Older, and Newer classes excluded, Robocopy does exactly what the original poster wants - without needing to load a scripting environment.
References: Technet, Wikipedia
Download from: Microsoft Download Link (Link last verified on Mar 30, 2016)
Upvotes: 253
Reputation: 1
This is what has worked for me. I use this to "add" files over to the other drive, with no overwrites.
Batch file: robocopy-missingfiles.bat
@echo off
echo Copying
echo "%1"
echo to "%2"
echo.
echo Press Cntr+C to abort
Pause
echo.
@echo on
robocopy %1 %2 /Xo /XN /XC /J /SL /S /MT:8 /R:1 /W:1 /V /DCOPY:DAT /ETA /COPY:DATO /FFT /A-:SH /XD $RECYCLE.BIN "System Volume Information"
Example:
robocopy-missingfiles.bat f:\Working-folder\ E:\Backup-folder\
Do test before implementation.
Upvotes: 0
Reputation: 110
A simple approach would be to use the /MIR option, to mirror the two directories. Basically it will copy only the new files to destination. In next comand replace source and destination with the paths to your folders, the script will search for any file with any extensions.
robocopy <source directory> <destination directory> *.* /MIR
Upvotes: -1
Reputation: 51
It won't let me comment directly on the incorrect messages - but let me just warn everyone, that the definition of the /XN and /XO options are REVERSED compared to what has been posted in previous messages.
The Exclude Older/Newer files option is consistent with the information displayed in RoboCopy's logging: RoboCopy will iterate through the SOURCE and then report whether each file in the SOURCE is "OLDER" or "NEWER" than the file in the destination.
Consequently, /XO will exclude OLDER SOURCE files (which is intuitive), not "older than the source" as had been claimed here.
If you want to copy only new or changed source files, but avoid replacing more recent destination files, then /XO is the correct option to use.
Upvotes: 3
Reputation: 3655
There is an odd way to do this with xcopy:
echo nnnnnnnnnnn | xcopy /-y source target
Just include as many n's as files you're copying, and it will answer n to all of the overwrite questions.
Upvotes: 5
Reputation: 1167
You can try this:
echo n | copy /-y <SOURCE> <DESTINATION>
-y
simply prompts before overwriting and we can pipe n to all those questions. So this would in essence just copy non-existing files. :)
Upvotes: 62
Reputation: 41
I just want to clarify something from my own testing.
@Hydrargyrum wrote:
This is actually backwards. XN does "eXclude Newer" files but it excludes files that are newer than the copy in the destination directory. XO does "eXclude Older", but it excludes files that are older than the copy in the destination directory.
Of course do your own testing as always.
Upvotes: 4
Reputation: 15769
For %F In ("C:\From\*.*") Do If Not Exist "C:\To\%~nxF" Copy "%F" "C:\To\%~nxF"
Upvotes: 42
Reputation: 3806
Belisarius' solution is good.
To elaborate on that slightly terse answer:
/E
makes Robocopy recursively copy subdirectories,
including empty ones. /XC
excludes existing files with
the same timestamp, but different
file sizes. Robocopy normally overwrites those. /XN
excludes existing files newer
than the copy in the source
directory. Robocopy normally
overwrites those. /XO
excludes
existing files older
than the copy in the source
directory. Robocopy normally
overwrites those.With the Changed, Older, and Newer classes excluded, Robocopy does exactly what the original poster wants - without needing to load a scripting environment.
Upvotes: 53
Reputation: 783
Robocopy can be downloaded here for systems where it is not installed already. (I.e. Windows Server 2003.)
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=17657 (no reboot required for installation)
Remember to set your path to the robocopy exe. You do this by right clicking "my computer"> properties>advanced>"Environment Variables", then find the path system variable and add this to the end: ";C:\Program Files\Windows Resource Kits\Tools" or wherever you installed it. Make sure to leave the path variable strings that are already there and just append the addtional path.
once the path is set, you can run the command that belisarius suggests. It works great.
Upvotes: 1
Reputation: 86688
Here it is in batch file form:
@echo off
set source=%1
set dest=%2
for %%f in (%source%\*) do if not exist "%dest%\%%~nxf" copy "%%f" "%dest%\%%~nxf"
Upvotes: 6