Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using wildard with DOS COPY command corrupts destination file

Tags:

copy

wildcard

dos

I don't understand the behaviour of the COPY command when using a wildcard.

I have a single text file in C:\Source called mpt*.asm and I want to copy it to C:\Dest. This is needed from a batch script, and I can't be sure of the exact name of mpt*.asm (it may be mpt001.asm for example). The destination name should be exactly mpt.asm.

If I use:

COPY C:\Source\mpt*.asm C:\Dest\mpt.asm

The file file is copied, but has a extra (0x1A) character appended to the end of the file.

If I use:

COPY C:\Source\mpt*.asm C:\Dest\mpt.asm /B

I don't get this spurious character.

If I don't use a wildcard, I don't get the spurious character either. It seems unlikely there is a bug in COPY, but this behavior seems unexpected.

Is there a way of doing this copy without resorting to using /B?

like image 719
Steve Coates Avatar asked Feb 05 '13 09:02

Steve Coates


People also ask

How do you copy a wildcard character in command?

You can use the wildcard characters asterisk ( * ) and question mark ( ? ) as part of the file name argument. For example, part* loads the files part-0000 , part-0001 , and so on. If you specify only a folder name, COPY attempts to load all files in the folder.

Does xcopy create destination folder?

If source is a directory or contains wildcards and destination does not exist, xcopy assumes destination specifies a directory name and creates a new directory. Then, xcopy copies all specified files into the new directory. By default, xcopy prompts you to specify whether destination is a file or a directory.

Which wildcard is used for copying all files?

As an alternative to entering a new name for each file, you can use a simple wildcard system to rename all copied or moved files at once. This is not a full pattern matching (or regular expressions) system; instead, the only wildcard character that's recognised is * (the asterisk).


1 Answers

I have never seen that before, but it does append an extra arrow character for me too.

You can work around the issue using xcopy instead.

echo f| xcopy C:\Source\mpt*.asm C:\Dest\mpt.asm

If you read copy /? it says

To append files, specify a single file for destination, but multiple files for source (using wildcards or file1+file2+file3 format).

So by using a single filename as the the dest, and using a wildcard in the source, it may interpret that as appending, which may be what the extra character is for, but as you aren't appending anything you can see it.

I'm only guessing but that may explain it.

like image 60
Bali C Avatar answered Nov 15 '22 10:11

Bali C