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

XCOPY directories starting with certain letter

I'm trying to copy directories and their contents to another directory.  The catch is that I want only the directories that start with the letter B to transfer, then be deleted.  My batch file looks like this:

xcopy "C:\Documents and Settings\Mike\My Documents\My Music\Local Test\B*" Z:\Music\Albums\Test\*.* /e /d /r /y
del "C:\Documents and Settings\Mike\My Documents\My Music\Local Test\B*" /q

I've been trying different things and just can't seem to get it.  Any thoughts?

Also, can anyone recommend a good site to reference for batch file creation?  I'm not even sure what all the parameters at the end mean, which could be one of my problems.

Thanks in advance.
1 Solution
For the batch file, try this:
@echo off
set source=C:\Documents and Settings\Mike\My Documents\My Music\Local Test
set dest=Z:\Music\Albums\Test
set prefix=B

for /f "tokens=*" %%a in ('dir /b "%source%\%prefix%*"') do (echo Moving "%%a"
   xcopy "%source%\%%a\*.*" "%dest%\%%a\" /s /h /k /c /y
   rd "%source%\%%a" /q /s)

echo Done.

Just modify the 3 SET lines at the top if you want to make changes to the Source, Destination, or the Directories "start" letter.

For batch file reference ... you can try http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx from Microsoft.  You can get syntax on most commands with the /? parameter ... like:

The xcopy switches I used are:
/s = get subdirectories
/h = get hidden and system files also (since we are going to be deleting the directories we should get everything)
/k = keep attributes of files
/c = continue on error
/y = yes, copy files without prompting
russomrAuthor Commented:
Perfect.  Thank you.
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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