How to exclude file types in specific subfolders with robocopy while mirroring? - robocopy

I want to mirror one folder to another. However, the source folder will have many *.pdb file, most of which I want to exclude.
Root/Subfolder1/a.pdb
Root/Subfolder2/b.pdb
Root/Subfolder2/Subsubfolder/c.pdb
I want to copy only .pdb files in Subfolder1 Note that there are thousands of .pdb files and thousands folders at various sublevels - I don't want any .pdb files which are within Subfolder2, directly or in sub(subsubsub)folders.
My original attempt is:
robocopy C\:Projects\Root D:\SomewhereElse\ /MIR /XF "C:\Projects\Root\Subfolder2\*.pdb"
This, however, does not work.
If I change it for this:
robocopy C\:Projects\Root D:\SomewhereElse\ /MIR /XF "C:\Projects\Root\Subfolder2\.pdb"
it works, but doesn't do what I want it to do.
After some experimenting, I figured out that the /XF won't work if there are both a slash () and an asterix (*) within the same path.
Why would anyone do this? How can I achieve what I want? Why is this so stupid?

Related

robocopy to preserve some extra files

I have a problem with robocopy and with extra files in particular.
I would like to run robocopy to keep two directories almost in "sync" and thus it would be natural to use the /MIR option along with /MON:x to keep the process alive.
I need to copy a list of files, let's assume for example *.tif, from SRC to DST and it is ok when robocopy is removing from DST extra files that matches the file pattern.
But with the /MIR option, robocopy is removing all extra files even if they are not matching the *.tif
I tried to use the /XX option, but this is actually preserving all extra files. I believed I could add a path or filename to the /XX but it is not accepted.
I tried to use the /XF *.png option in order to exclude the png files, but this is only applied on the SRC and not on the DST.
Does a solution to this problem exist?
Thanks in advance for your help,

Copying source files and retaining folder structures including parent folders (but not files)

Say I have the following source and target.
source: c:\temp\folder1\folder2
target: e:\backup
I want to have an output as shown below. As you can see, it should only copy the files and its subdirectories from the above source path but it should also copy/create the parent folders (no files) leading to the source path. How can I accomplish this with Powershell or robocopy? If Powershell doesnt handle long path names then I prefer using Robocopy.
source:
c:\temp
\folder1
\file1.txt
\folder2
\file2.txt
\folder3
\file3.txt
output:
e:\backup
\temp
\folder1
\folder2
\file2.txt
\folder3
\file3.txt
If I have understood you right you want to create any missing folders in the output on the fly. In powershell this would be s.t. like this:
Copy-Item c:\temp e:\backup\temp\ -recurse

How to perform two ROBOCOPY statements from one source folder to two destination folders in one batch file?

I have an interesting situation where I'd like to move some files from directory X to directory Y, but in order to MOVE the files without deleting the source folder, I created a log statement in said folder. A very simple version of what I'm doing is this:
robocopy
"C:\Desktop\Origin" "C:\Desktop\Destination"
/e /move /XF "C:\Desktop\Origin\test.log"
/log:"C:\Desktop\Origin\test.log"
This results in the moving of all files in "Origin" directory to "Destination" directory, and doesn't get rid of the "Origin" directory, as the "/e /move" flags normally would. Now, if I didn't want that log file, I could simply add this line to the code:
del "C:\Desktop\Origin\test.log"
But, I actually do want to keep this log file. I just want it somewhere else. So what I've been trying to do is to move it to directory Z with "/e /mov" or even just "/s" and then the delete. For example:
robocopy
"C:\Desktop\Origin" "C:\Desktop\Destination"
/e /move /XF "C:\Desktop\Origin\test.log"
/log:"C:\Desktop\Origin\test.log"
robocopy
"C:\Desktop\Origin" "C:\Desktop\Destination 2"
/s
del "C:\Desktop\Origin\test.log"
But after trying multiple times, with multiple little variations, nothing seems to happen. The first robocopy happens, but the second doesn't seem to happen at all. It seems like multiple robocopy statements can run in one batch file (which is what I want) but perhaps not from the same source directory. Does anyone know why this is the case and also how I can achieve my stated goal? Thanks!
Thanks to the suggestion of Ken White, I found a solution to this. If you use MOVE.EXE, it works just as expected.
robocopy
"C:\Desktop\Origin" "C:\Desktop\Destination"
/e /move /XF "C:\Desktop\Origin\test.log"
/log:"C:\Desktop\Origin\test.log"
move
"C:\Desktop\Origin\test.log" "C:\Desktop\Destination 2"
One thing to note is that you can't use /LOG+: with this method, since you'd be writing the log file to a location where no log file exists, since it had been moved previously to "Destination 2". However, even the normal log files include the *EXTRA files already in the directory.

Is there any batch command to move files and delete empty sub folders

Anyone know of a simple batch command to move files and delete empty sub folders? Just got a new external and when I copied everything over it resulted in something like this. F:\Music\John Denver\John Denver\files1-10 F:\Music\TheBeatles\TheBeatles\files1-10 and so on, for say, 350+ folders. I don't want to waste that much time manually moving them up one folder and deleting sub folders... Not all files are same file type, and not all folders are music. Some folders have other sub folders that I want to keep (mostly for music files, such as artist\album\files) Is there a way to do this instead of the old fashioned way? Any help is appreciated.
You can use ROBOCOPY.
ROBOCOPY source destination /S /MOVE
/S flag is used to exclude empty subfolders.
ROBOCOPY "F:\Music\John Denver\John Denver\files1-10" "F:\Music\TheBeatles\TheBeatles\files1-10" /S /MOVE

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.