问题
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:
After a few hours of work I have managed to find the right combination of commands in order to make this happen. Here it is for you all and I hope it helps:
SET destination=%CD%
E:
for /f "delims=" %%a in ('dir /b /s ^| find "searchedFile"') do (
cd ..
xcopy "%%a" "%destination%" /D:10-10-2011)
pause
I used the change directory command because the "directory" command returned the entire path, including the file and when trying to copy it.. it thought that the file was in the path that included its name.
For example, if i searched for "myFile.jpg" in "E:\Folder\New Folder\myFile.jpg" it thought that the location of the file was "E:\Folder\New Folder\myFile.jpg\myFile.jpg" and obviously this doesn't work.
回答2:
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
回答3:
Place the file path in quotes
copy "%file%" "%destination%"
or
SET destination = "%CD%"
回答4:
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" .\
来源:https://stackoverflow.com/questions/9849904/windows-command-commands-to-find-file-and-copy-it-to-certain-location-or-directo