Windows command/commands to FIND file and COPY it

2019-08-20 19:38发布

问题:

This question already has an answer here:

  • Some Copy command in windows to copy from current location to specified location 1 answer

So, I have started with this:

copy | dir /s /b | find "myFile" C:\Destination

but the problem is that the destination is not visible in this command. It only sees the first part of the command up until C:\Destination.

Is there a way I can search for a file and copy it?

I have also tried this:

SET source = dir /s /b | find "myFile"
SET destination = %CD%
copy %file% %destination%

but it doesn't work.

At some point even trying to set a variable that points to the current directory (%CD%) doesn't work.

Thanks in advance!

PS: I'm looking for a solution that would work without installing anything new on the computer, that's why I'm thinking of batch files.

I think I could do this with VBscript but I'm not sure. If anyone thinks it's a better option please post that answer too.

回答1:

Place the file path in quotes

copy "%file%" "%destination%"

or

SET destination = "%CD%"


回答2:

How about this?

dir/s/b|for /f %i in ('find "myFile"') do copy %i .\


Guess %i should be quoted, too...

dir/s/b|for /f %i in ('find "myFile"') do copy "%i" .\



回答3:

dir/s/b|for /f %i in ('find "myFile"') do copy "%i" .\

Works very nicely for me. Anyone know how to use the same line to copy same named files to a directory with new names. Example: file name is: text.txt the above command line searches many folders and copies all instances found like below: 1text.txt, 2text.txt, 3text.txt