cmd shell copy

Posted on 2014-08-09
Last Modified: 2014-08-09
I have a batch file that copies some files on the network, but its not behaving the same way with files on network drives as it does on test folders on my local machines:

copy \\Share\file.out + \\Share\*.txt  \\Share\file.out
Del *.txt

what I expect to see is that all the txt files appended to file.out, but what I see is that the contents of file.out is duplicated 1 time in file.out for each time the file runs.  In short file.out is being appended with "file.out + *.txt" not replaced with ?
Question by:sidwelle
    LVL 43

    Accepted Solution

    Personally I tend to use:

    type \\server\share\*.txt > newfile.txt

    You can also do:

    copy \\server\share\*.txt newfile.txt


    [ Edit - re-read, to append I'd use TYPE with >> instead, i.e.

    TYPE \\server\share\*.txt >> newfile.txt
    LVL 43

    Expert Comment

    by:Steve Knight
    With what you have I would expect you'd get

    (contents of existing file.out)
    contents of file1.txt
    contents of file2.txt
    etc. which is I assume what you are after.

    The other way I use if I want a specific order, e.g. date/time is for loop over files and append one by one using output of dir command - change the /od paramter to sort by date+time, name etc.

    @echo off
    pushd \\server\share
    set outfile="file.out"
    for /f %%a "tokens=*" %%a in ('dir /b /a-d /od *.txt') do TYPE %%~a >> %outfile%


    Author Comment

    Good solution !

    Question: When I watch what it executed, I see the command line was executed with a "1", what does the 1 represent ?

    //Batch file
    type \\server\share\*.txt >> newfile.txt

    //Cmd window
    type \\server\share\*.txt  1>> newfile.txt
    LVL 43

    Expert Comment

    by:Steve Knight
    Good q. 1 means standard output or Stdout. 2 means errors and 0 means input.

    Do dir > test.text.  is short for 1>test.text


    MD xyz 2>nul

    Means mske dir xyz and any errors send to nul I.e. they disappear, normal output goes to screen.

    Can redirect both to two different files or to same:

    Dir > output.text 2> errors.text
    Or both to same file... &1 means to redirect errors to stream 1 which is normal output...

    Dir > output.text 2>&1

    That is why sometimes if you redirect output some things show on screen or miss from the log unless you use above.

    That'll do typing on phone... Something dlto do while waiting boy going to sleep!


    Author Closing Comment

    Good Info, Thanks for the help.
    LVL 43

    Expert Comment

    by:Steve Knight

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This is a little timesaver I have been using for setting up Microsoft Small Business Server (SBS) in the simplest possible way. It may not be appropriate for every customer. However, when you get a situation where the person who owns the server is i…
    You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
    This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

    745 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

    17 Experts available now in Live!

    Get 1:1 Help Now