• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 19319
  • Last Modified:

ROBOCOPY, folders, and wildcards : Files AND folders that begin ONLY with certain letters!

I want to copy everything beginning with the letter A or B over to the root of drive L:. That's it - AS SIMPLE AS THAT. Robocopy copies only the files, and won't touch the folders... Robocopy sure ain't lookin' robust to me. Prove me wrong!

More details for those who like details
Version XP010 used on Windows 2000 Server
the command I used to successfully copy EVERYTHING (not my objective):
robocopy <source dir> L: /COPYALL /E /R:3 /W:5 /ZB /V /FP /LOG:testcopy.log

Open in new window

the command that has not been working for me to get everything starting with the letter "A":
robocopy <source dir> L: a*.* <with the same options as above> 

Open in new window

The result was that I only copied files starting with "a". All the folders beginning with A were untouched.

Will I have to do an /XD with c* d* e* f*.... all the way to z??? This might be the answer. I suppose sometimes we get the answers after taking the effort and time to form the question. If you have a better idea, plz let me know, or if you think that using /XD like this is it, plz let me know that also. Thx.

I know there are alternatives like RichCopy, but I can use only robo at the moment since it works after being copied onto the server (doesn't require installation). I need something fairly reliable since I have to preserve permissions also because I can't have the copy die in the middle leaving me clueless about what went and what didn't.

  • 4
  • 4
2 Solutions
T-DevAuthor Commented:
Please consider that I need to exclude files that have numbers as names also. Thx.
The third argument is indeed a *file* mask, not a folder mask. Robocopy just looks at the files and creates the folders as needed. Unfortunately, there is no "Include Directory" option, so you can only work with XD.
Another option might be to just create a batch wrapper script that kicks off separate robocopy jobs for the folders.
But it's not quite clear which files you actually want to copy: what happens with files that do NOT start with "a" or "b", but are inside a folder that starts with "a" or "b"?
And since you're writing to a log file, add /NP to your options; it keeps the control characters from the progress indicator out of the log.
T-DevAuthor Commented:
I'm glad you brought up the issue of files within folders... I want EVERYTHING inside the folders to get copied. So, the idea of using a*.* b*.* looks like a mistake!! - because it excludes lots of files within the folders getting copied. Argh! So, without a*.* b*.* my command now looks like this:

robocopy H: L: /E /COPYALL /R:3 /W:5 /ZB /V /FP /LOG:test.log /XD c* d* e* f* g* h* i* j* k* l* m* n* o* p* q* r* s* t* u* v* w* x* y* z* 1* 2* 3* 4* 5* 6* 7* 8* 9* 0*

Open in new window

However, now, the unwanted result is that while I successfully copy only folders with names starting with A or B, I end up copying all files from the root of my source as well.

I have an untested solution for copying all files on the root of H: that begin with A or B:. It consists of using a*.* b*.* /XD *.*. But what about the other situation --> How to copy just folders beginning with A or B with ALL their contents AND exclude all files on the root of H:? You see, I have big files on the root of H: beginning with other characters, and waiting for them is not an option.

So, using the robo command twice would be ok. I just don't know how to exclude all files while copying all of the files inside the folders. Thx.
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Here's a little batch script that will run one robocopy job for the files in the root of the drive, and then create separate robocopy jobs for all folders in the source starting with a or b.
@echo off
setlocal enabledelayedexpansion
set Source=H:
set Target=L:
set CopyOptions=/COPYALL /R:3 /W:5 /ZB /V /FP /LOG+:testcopy.log
REM Copy all files starting with a or b in the root of the source folder:
robocopy.exe "%Source%" "%Target%" a*.* b*.* %Options%
REM Copy all folders starting with a or b in the source folder:
for /d %%a in ("%Source%\a*" "%Source%\b*") do (
  robocopy.exe "%%a" "%Target%" /E %Options%

Open in new window

T-DevAuthor Commented:
That's sophisticated. It appears to acknowledge robocopy's limitations as I perceived them.

I see what it does. The first time the robocopy command is issued, it's done without /E or /S, which is why it does just files.

Then DOS handles the wildcard for folders, changing the source again and again, and creates a new robocopy command for each different source since Robocopy doesn't appear able to handle wildcards in the <source> or <dest>.

A minor quibble is that it looks like each folder's contents are sent as files, not as folders. So, L: would become populated with the files, not the folders with the files within them. This is a minor because you really have presented a solution that I think would work with the challenge I presented. The quibble could be solved by adding a line to create the dirs and then use them as destinations. Any other ideas?
Sorry, yes, that can of course be done as well.
Just replace line 10 with this (adds "\%%~nxa" after %Target%):
 robocopy "%%a" "%Target%\%%~nxa" /E %Options%

Open in new window

T-DevAuthor Commented:
2 important comments to add (to give oBda credit, help others, and document this for my own benefit - cuz I'll most likely forget it!! hehe)

1. First of all, oBdA, your last comment was also very useful because I had no idea what %%~nxa was until I realized (after googling) that it's just our %%a again with this ~nx shoved in the middle. It still wasn't clear why this was needed; so I did a test, and saw that ~nx trimmed a folder's path name down from its long path (like c:\rootfolder\nextfolder\anotherfolder\finallythefoldername.yep) to just its Name and eXtension, which comes out to finallythefoldername.yep.

2. Secondly, above, I thought that /XD would do the job if I used it like this: robocopy blah blah /XD c* d* e* f* g* h* i* j* k* l* m* n* o* p* q* r* s* t* u* v* w* x* y* z* _* 0* 1* 2* 3* 4* 5* 6* 7* 8* 9*. The good part of this plan is that it does successfully exclude all directories beginning with letters other than A and B. HOWEVER, the bad part is that it continues to do that inside the folders beginning with A and B as well! Argh!!

So, it truly appears that the DOS batch file is the only approach that succeeds in producing the desired result.
Yes, %%~nxa will expand to name and extension of the path in %%a; enter "help call" in a command prompt for details.
And, yes, robocopy is very thorough; if you only want to the exclude root dirs beginning with certain characters, try specifying full paths: "/XD H:\c* H:\d* ..."
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now