Fun (or perhaps not) with COPY and wild cards

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Fun (or perhaps not) with COPY and wild cards

#1 Post by dbenham » 19 Jun 2012 15:37

I kind of already knew about the second COPY result below.

But I was moderately surprised that the 1st COPY overwrote the same output file without giving any warning or error message.

The last COPY result that appends all the source files into a single file was a complete surprise. :shock:

Code: Select all

D:\test>>a1.txt echo 1

D:\test>>a2.txt echo 2

D:\test>>a3.txt echo 3

D:\test>dir /b
a1.txt
a2.txt
a3.txt

D:\test>copy a*.txt test*.txt
a1.txt
a2.txt
a3.txt
        3 file(s) copied.

D:\test>dir /b test*
test.txt

D:\test>type test.txt
3

D:\test>copy a*.txt b*.txt
a1.txt
a2.txt
a3.txt
        3 file(s) copied.

D:\test>dir /b b*
b1.txt
b2.txt
b3.txt

D:\test>type b1.txt
1

D:\test>type b2.txt
2

D:\test>type b3.txt
3

D:\test>copy a*.txt c.txt
a1.txt
a2.txt
a3.txt
        1 file(s) copied.

D:\test>dir /b c*
c.txt

D:\test>type c.txt
1
2
3

D:\test>


Dave Benham

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Fun (or perhaps not) with COPY and wild cards

#2 Post by Squashman » 19 Jun 2012 15:43

I use the last copy example all the time at work to quickly merge all the text files in a folder into a single file. Lot easier to upload one file to the mainframe then two dozen.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Fun (or perhaps not) with COPY and wild cards

#3 Post by aGerman » 19 Jun 2012 16:14

dbenham wrote:But I was moderately surprised that the 1st COPY overwrote the same output file without giving any warning or error message.

copy /?
Default is to prompt on overwrites unless COPY command is being executed from within a batch script.

How did you test it? Batch script with echo on?
More surprising is the different behavior in the 1st and 2nd example.

dbenham wrote:The last COPY result that appends all the source files into a single file was a complete surprise. :shock:

No, that's defined as well.
To append files, specify a single file for destination, but multiple files for source (using wildcards or file1+file2+file3 format).

Regards
aGerman

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Fun (or perhaps not) with COPY and wild cards

#4 Post by Fawers » 19 Jun 2012 16:22

It's true that COPY presented a strange behaviour when copying a*.txt to test*.txt - at least to my eyes. "test*.txt" used a wildcard as well, so COPY was supposed to create test1.txt, test2.txt, and test3.txt.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Fun (or perhaps not) with COPY and wild cards

#5 Post by dbenham » 19 Jun 2012 16:57

aGerman wrote:
dbenham wrote:But I was moderately surprised that the 1st COPY overwrote the same output file without giving any warning or error message.

copy /?
Default is to prompt on overwrites unless COPY command is being executed from within a batch script.

How did you test it? Batch script with echo on?

A batch script was not used. I simply typed the commands into the command line. I just discovered that my default environment has COPYCMD=/Y. That explains the lack of any prompts.


aGerman wrote:
dbenham wrote:The last COPY result that appends all the source files into a single file was a complete surprise. :shock:

No, that's defined as well.
To append files, specify a single file for destination, but multiple files for source (using wildcards or file1+file2+file3 format).

Plain as the nose on my face, but I never noticed "using wildcards" before :oops: :lol:


Fawers wrote:It's true that COPY presented a strange behaviour when copying a*.txt to test*.txt - at least to my eyes. "test*.txt" used a wildcard as well, so COPY was supposed to create test1.txt, test2.txt, and test3.txt.

Yes, but the "test" literal in the rename consumes characters from the original name. Only the excess characters from the original rename are preserved.

Code: Select all

D:\test>dir /b a*.txt
a123456.txt

D:\test>ren a*.txt test*.txt

D:\test>dir /b test*.txt
test456.txt


All in all... much ado about nothing I guess. :D

Dave Benham

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Fun (or perhaps not) with COPY and wild cards

#6 Post by aGerman » 19 Jun 2012 17:31

Thanks Dave. That last information was new to me. I admit I never use wildcards in the destination name since I never found an official statement how it behaves.

Regards
aGerman

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Fun (or perhaps not) with COPY and wild cards

#7 Post by Fawers » 19 Jun 2012 22:41

You're right, Dave. I hadn't tested that until now, and it works the way you said.
Unexpected, I must say.

ThinAir
Posts: 3
Joined: 31 May 2012 16:25

Re: Fun (or perhaps not) with COPY and wild cards

#8 Post by ThinAir » 21 Jun 2012 11:30

How would enabledelayedexpansion effect these copies, particularily the fiirst?

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Fun (or perhaps not) with COPY and wild cards

#9 Post by Liviu » 21 Jun 2012 20:33

dbenham wrote:Yes, but the "test" literal in the rename consumes characters from the original name. Only the excess characters from the original rename are preserved.

Indeed. Besides the builtin 'copy' and 'ren', the external 'comp.exe' works the same. Thanks for pointing, or repointing - thought I knew that but don't remember if I forgot ;-)

ThinAir wrote:How would enabledelayedexpansion effect these copies, particularily the fiirst?

There were no variable expansions in Dave's example. Delayed or not makes no difference at all.

Liviu

Post Reply