robocopy keep smaller file - robocopy

Is there a way to have robocopy copy files from one directory into another overwriting files with the same name so that the destination directory will contain the smaller file.
so for example
dirA
-- file1 (12Kb)
-- file2 (2Kb)
dirB
-- file1 (11Kb)
-- file2 (3Kb)
I want to robocopy from dirB to dirA such that dirA would have file1 (11Kb) and file2 (2Kb)
after robocopy
dirA
--file1(11Kb)
--file2(2Kb)
Is there a way to do this?

As far as I am aware this is possible with:
robocopy src dest /XC
The XC will eXclude Changed files. Robocopy considers a file to be changed based on its file size.

Related

Copy files to destination folder and also delete any that are not in source folder?

I want to copy the folder Prod from source to destination, however I want to update the contents of destination with the contents of the source.
Prod
|--file 1
|--file 2
Destination:
Prod
|--file a
|--file b
The problem is that since the name of the files are different, when I do copy paste I end up with 4 files: file 1, file2, file a, file b
When I only want the two files from source: file 1 & file 2
I'd use robocopy with the MIR (Mirror) switch, this will copy files/folders and remove anything in destination that does not exist in source.
robocopy C:\source\Prod D:\destination\Prod /MIR
Quote from Technet robocopy wiki:
/MIR specifies that Robocopy should mirror the source directory and the destination directory. Note that this will delete files at the destination if they were deleted at the source.

How to copy a file from source to a destination and recreate the folder structure

I have some files which needs to be copied through deployment process often to a destination. This is my folder folder structure:
SOURCE:
c:\
folder1
sub1
subsub1
file1
file2
I need something where i can tell my "script" something like this
mycopy c:\folder1\sub1\subsub1\file1 h:\
That means that i dont want to
check if folder structure exist
provide on bot sides the complete structure for each file on destination side
I want to
provide the full path and filename on source side
create folder structure if not exist
overwrite file if exist
How can i achieve this?
See the robocopy help page on MS Technet
https://technet.microsoft.com/en-us/library/cc733145%28v=ws.11%29.aspx
In your case you would do something like
robocopy c:\folder1\sub1\subsub1 h:\ file1 <options ...>
Notice that the first two parameters are the source and dest paths only, with no filename at the end
Read the linked help page and test on your desktop ...
Ok it works like this:
You can use Robocopy for this task. Thanks you Rick716 for this direction. I am not marking it as answer cause it is only a direction not the solution.
Lets Assume that we have the following source folder structure:
N:\source\a1\b1\c1\d1\e1\f1
and we want to recreate the hole structure under n:\source within h:\destination. Then we have to use Robocopy in this way:
robocopy N:\source h:\destination /e
The option /e will create the folder structure even create empty folders. Additionally you can append the files which should be copied. For example
file.txt
*.jpg
*.bkp
etc. But these files will be even copied when they exist somewhere within the folder strcuture! For example you have the file n:\source\a1\file.txt and the file n:\source\a1\b1\c1\file.txt then both will be copied by using
robocopy N:\source h:\destination file.txt /e

Using Windows commandline, how can I completely replace one folder with another?

So far I have it to where I can copy all the files from c:\Users\John\Folder1 to c:\Users\John\Folder2.
But I am looking to completely swap the folders.
e.g. Replace c:\Users\John\Folder1 with c:\Users\John\SomeFolder\Folder1.
I have this right now: xcopy c:\Users\John\SomeFolder\* c:\Users\John\Folder1 /s /i
This just copies all the files from the c:\Users\John\SomeFolder\Folder1 to c:\Users\John\Folder1 but leaves the files that had been there prior. I want the entire folder to be replaced. If the new folder I am copying no longer has those files, I want them deleted.
Sorry if this is confusing - any help is greatly appreciated.
I think you can create a batch file to do this.
The pseudo-code:
Erase contents of directory 1
Copy the contents from directory 1 to directory 2
The code:
Create a file called swapFiles.bat in your notepad, and enter the following code:
rd /s %1
mkdir %1
xcopy /s /i %2\* %1
How to use it:
swapFiles c:\Users\directory1 c:\Users\directory2
directory1 is the old directory (i.e. the one that will be wiped out)
Hope this helps you
Maybe I'm completely missing your point, but would this not do the job? (example):
rename Folder1 transit
rename Folder2 Folder1
rename transit Folder2
This will mirror the first folder to the second.
Be very careful that the paths are correct.
#echo off
robocopy "c:\Users\John\SomeFolder\Folder1" "c:\Users\John\Folder1" /mir
Do you want to delete this folder c:\Users\John\SomeFolder before copying the folder1 if it is so this code may wok for you
#echo off
robocopy /s c:\Users\John\Folder1 c:\Users\John\SomeFolder\Folder1
rmdir /s /q c:\Users\John\Folder1

How to exclude subdirectories in the destination while using /mir /xd switch in robocopy

There is a script running which mirrors a bunch of folders from one volume to another. The problem is that now there are going to be subdirectories within those folders at the destination which are not part of the original mirroring script. They are standalone subdirectories and I don't want them purged once the mirroring kicks in. Is there a way for me to use the /xd switch in robocopy wherein I'd be able to exclude the destination subdirectories.
Example:
robocopy "\\hq04t2fis202\archive\dr" "\\hq04t3fis202\archive\dr" /mir /xd "\\hq04t3fis202\archive\dr\*\hq04s2dba301"
In the above example, there are several directories under \\hq04t2fis202\archive\dr that are being mirrored. And at the destination, once these directories are mirrored from the source, there is another script which dumps separate subdirectories within each of those directories. So what I want to do is somehow use the /xd switch to avoid purging those subdirectories at the destination.
Also, in the above example, in the /xd switch, I'm using the "*" wildcard to mean that I want to include all the directories that fall under the "\\hq04t3fis202\archive\dr" root folder at the destination.
Need help and suggestions as to whether this is possible, and if it is then how can I do it. So far I've tried and tested many other switches like /xo, /xx, but none of them solve my purpose.
Also, /xx would work except that now it won't delete any folders at the destination at all and it wouldn't be mirroring.
Hopefully I'm not overly confusing everyone here. Let me know if you have any questions.
When i tried the solution with /XD i found, that the path to exclude should be the source path - not the destination.
e.g. this Works
robocopy c:\test\a c:\test\b /MIR /XD c:\test\a\leavethisdiralone\
Rather than creating empty directories in source to exclude, you can supply the full destination path to the /XD switch to have the destination directories untouched
robocopy "%SOURCE_PATH%" "%DEST_PATH%" /MIR /XD "%DEST_PATH%"\hq04s2dba301
The issue is that even though we add a folder to skip list it will be deleted if it does not exist.
The solution is to add both the destination and the source folder with full path.
I will try to explain the different scenarios and what happens below, based on my experience.
Starting folder structure:
d:\Temp\source\1.txt
d:\Temp\source\2\2.txt
Command:
robocopy D:\Temp\source D:\Temp\dest /MIR
This will copy over all the files and folders that are missing and deletes all the files and folders that cannot be found in the source
Let's add a new folder and then add it to the command to skip it.
New structure:
d:\Temp\source\1.txt
d:\Temp\source\2\2.txt
d:\Temp\source\3\3.txt
Command:
robocopy D:\Temp\source D:\Temp\dest /MIR /XD "D:\Temp\source\3"
If I add /XD with the source folder and run the command it all seems good the command it wont copy it over.
Now add a folder to the destination to get this setup:
d:\Temp\source\1.txt
d:\Temp\source\2\2.txt
d:\Temp\source\3\3.txt
d:\Temp\dest\1.txt
d:\Temp\dest\2\2.txt
d:\Temp\dest\3\4.txt
If I run the command it is still fine, 4.txt stays there 3.txt is not copied over. All is fine.
But, if I delete the source folder "d:\Temp\source\3" then the destination folder and the file are deleted even though it is on the skip list
1 D:\Temp\source\
*EXTRA Dir -1 D:\Temp\dest\3\
*EXTRA File 4 4.txt
1 D:\Temp\source\2\
If I change the command to skip the destination folder instead then the folder is not deleted, when the folder is missing from the source.
robocopy D:\Temp\source D:\Temp\dest /MIR /XD "D:\Temp\dest\3"
On the other hand if the folder exists and there are files it will copy them over and delete them:
1 D:\Temp\source\3\
*EXTRA File 4 4.txt
100% New File 4 3.txt
To make sure the folder is always skipped and no files are copied over even if the source or destination folder is missing we have to add both to the skip list:
robocopy D:\Temp\source D:\Temp\dest /MIR /XD "d:\Temp\source\3" "D:\Temp\dest\3"
After this no matters if the source folder is missing or the destination folder is missing, robocopy will leave it as it is.
The way you can exclude a destination directory while using the /mir is by making sure the destination directory also exists on the source. I went into my source drive and created blank directories with the same name as on the destination, and then added that directory name to the /xd. It successfully mirrored everything while excluding the directory on the source, thereby leaving the directory on the destination intact.
The argument order seems to matter... to exclude subdirectories, I used
robocopy \\source\folder C:\destinationfolder /XD * /MIR
...and that works for me (Windows 10 copy from Windows Server 2016)
Try my way :
robocopy.exe "Desktop\Test folder 1" "Desktop\Test folder 2" /XD "C:\Users\Steve\Desktop\Test folder 2\XXX dont touch" /MIR
Had to put /XD before /MIR while including the full Destination Source directly after /XD.

Diff using filenames only

Is it possible using any sort of diff utility to diff based on filename only, ignoring all folders and subfolders?
So if I have
/folder_1/a/1243.txt
and
/folder_2/b/1243.txt
or
/folder_2/1234.txt
It would match the files when doing a diff between folder_1 and folder_2?
Are you trying to do a diff on lists of filenames, to see which filenames one folder contains that the other doesn't? If so, do find folder_1 -type f in Linux or dir /s /b /ad folder_1 in DOS and pipe the output into text files, then diff the contents of the text files.
In Vim I'd skip the temp files, and do :.!find folder_1 -type f in one window, :.!find folder_2 -type f in second window, then :windo diffthis to diff them.
diff -r directory another
compares the files in directories and outputs if they differs, or is missing from either of the directory.