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

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

Related

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

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?

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.

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

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.

How to use Robocopy to copy a template folder structure to other folders?

I need to copy all the folders within one folder, to multiple other folders. The folder structure I want to copy from is here:
x:\Customer1\Site1\
I want to copy all the folders within Site1, to all the folders within the following directory:
X:\Customer1\
Obviously I don't want to copy the folders back into Site1 again, only every folder within Customer1, excluding Site1.
Site1 contains 19 folders. I would like to end up having those 19 folders within every folder in the Customer1 folder. Can someone please tell me how to achieve this?
I have been looking at the Robocopy MS page to learn about all the switches and options, but there doesn't seem to be anything to help me with this 'copying folder tree from one folder to multiple folders' that I need. Please give me any reference
Many thanks
Naz
As far as I know, this is only possible with xcopy's /t-switch which copies only the folder structure (note that if you also want empty folders to get copied you have to put the '/e' -switch as well)
To copy the structure in every subfolder a for-loop is the way to go:
set customer1="X:\Customer1"
set site1="Site1"
for /f %%d in ('dir %customer1% /b /ad') do (
if %%d NEQ %site1% xcopy %customer1%\%site1%\*.* %customer1%\%%d\*.* /t /e
)