XCOPY directories starting with certain letter

Posted on 2006-04-22
Last Modified: 2012-06-27
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.
Question by:russomr
    LVL 10

    Accepted 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 from Microsoft.  You can get syntax on most commands with the /? parameter ... like:
    XCOPY /?

    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

    Author Comment

    Perfect.  Thank you.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
    YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT ( The crux of this batch f…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now