Posted on 2004-11-03
Last Modified: 2010-04-10
Scenario: 2 servers- SERV1 & SERV2. SERV1 is the master or template server. The D: drive on SERV1 has 3 directories named BLUE, GREEN, & RED. Inside these 3 directories there are dozens of other files.

GOAL: To create a batch file that when run on SERV2 will create the 3 directories(BLUE, GREEN & RED) and then copy the contents of those 3 directories to the newly created BLUE, GREEN & RED on SERV2 via UNC paths, copy commands or whatever else. In the end , if the batch file works properly it will duplicate the contents of the D: drive onto SERV2. It is safe to assume the D: drive has been sized and partitioned before this batch file is run.

I need the actual commands that a pro would use line by line.
Question by:Brian_Blair
    LVL 95

    Expert Comment

    by:Lee W, MVP
    If NOT Exists "d:\blue" mkdir "blue" "green" "red"
    copy "\\serv1\sharename\blue\*.*" "d:\blue"
    copy "\\serv1\sharename\green\*.*" "d:\green"
    copy "\\serv1\sharename\red\*.*" "d:\red"

    The above should do it... but there are more questions.  Do these folders have sub folders?  If so, you'll probably want to use XCOPY or ROBOCOPY.  Do these files have security that needs to be copied?

    Author Comment

    YES- there are definately sub folders. Never really thought about the security but in a perfect scenario, yes the security would be copied.
    LVL 95

    Assisted Solution

    by:Lee W, MVP
    If NOT Exists "d:\blue" mkdir "blue" "green" "red"
    xcopy "\\serv1\sharename\blue\*.*" "d:\blue" /o /h /e /v /c
    xcopy "\\serv1\sharename\green\*.*" "d:\green" /o /h /e /v /c
    xcopy "\\serv1\sharename\red\*.*" "d:\red" /o /h /e /v /c

    /e = copies folders and subfolders, even empty ones
    /o = copies file ownership and security info
    /h = copies hidden and system files
    /c = continues copying on errors
    /v = verifies each file.

    Author Comment

    leew- Thanks for the help. At the top of the file do I need any "cls" statements or "cd" change directory statements? Also, in some of your text you have "quotation" marks. Is this the correct syntax?

    Get back to me on this and the points are yours.
    LVL 95

    Accepted Solution

    cls only clears the screen.  You can if you like..  Really doesn't have any relevence to the script other than starting on a blank screen.  TECHNICALLY, you don't need the quotes if those are really the folder/file names you are using.  But they don't hurt.  I included them in case you want to modify this and use folder names with spaces.  When you reference shares/folder names with spaces, you get  errors if they are not in quotes.

    No need to CD anywhere, all the paths given are absolute.  Other than perhaps opening a command prompt and CD'ing to the folder you have the script in.  But that can be remedied by put a shortcut on your desktop to wherever you save it.

    Also, if you want to log this to save the results of the copy process, you can do the following.

    1.  Save the script as - for example: COPIER.CMD
    2.  From a command prompt, type COPIER > %temp%\COPIER.LOG

    #2 creates a file in your temp folder that has ALL output from the command.  You can review it after the command completes and verify that ALL files were copied and see which - if any - had errors.

    If the log is unimportant to you, you would simply type "COPIER"
    LVL 95

    Expert Comment

    by:Lee W, MVP
    One more note - to create a log that records errors, I made a mistake - you should use:

    COPIER > %temp%\COPIER.LOG 2>&1

    Otherwise, some programs might not save the errors to the log file.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    What is IRC? IRC (Internet Relay Chat) is a form of communication between multiple users. It is available freely to anyone with inernet access. IRC is a great way to communicate with others e.g. There is an IRC channel for Ubuntu Linux, which is fo…
    If your business is like most, chances are you still need to maintain a fax infrastructure for your staff. It’s hard to believe that a communication technology that was thriving in the mid-80s could still be an essential part of your team’s modern I…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    754 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

    21 Experts available now in Live!

    Get 1:1 Help Now