Peter Olson
Peter Olson

Reputation: 142921

Copy single file instead of entire directory in windows batch

Let's suppose that I am in some directory with two subdirectories, a and b. a has two files in it: t1.txt and t2.txt. That is, I have the following directory structure:

/.
  /a
    t1.txt
    t2.txt
  /b

I want to copy the file t1.txt from the a directory into the b directory.

I tried the following command

copy /b a/t1.txt b/t1.txt

but it copies the entire a directory into the b directory.

Why does this happen, and how can I make it so that only the t1.txt file is copied?

Upvotes: 1

Views: 1009

Answers (1)

johncip
johncip

Reputation: 2259

When copying to a new directory, you only need to specify the new directory. So

 copy /b a\t1.txt b

should work.

That said, I don't think additionally specifying the file name would cause the error you've described -- the official help text says "Destination can consist of a drive letter and colon, a folder name, a file name, or a combination of these," which to me implies that how you have it is fine.

I've also reversed the slashes -- were you using forward slashes in your batch file or is that a typo in the post? Maybe that was the problem?

Upvotes: 2

Related Questions