Page 1 of 2

find filename in many folders copying to one folder

Posted: 27 Mar 2014 10:33
by raylward102
Here is what I've got so far.

Code: Select all

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


This works, but it overwrites all copied instances leaving the last file copied (only 1 file).
I would like some help in adding a count feature to this so I'll be left with all copies of the file in the end.
The final result would be:

1test.txt
2test.txt
3test.txt

Re: find filename in many folders copying to one folder

Posted: 27 Mar 2014 10:49
by foxidrive
Single line for the command prompt:

Code: Select all

cmd /v:on /c "for /r %a in (test.txt) do (set /a c+=1 & copy "%a" "d:\!c!%~nxa")"

Re: find filename in many folders copying to one folder

Posted: 27 Mar 2014 10:54
by Aacini
In the command-line?

Code: Select all

set "i="
for /R %i in (test.txt) do @copy "%i" ^& set /A i+=1 ^>NUL ^& call ren test.txt %i%test.txt


Antonio

Re: find filename in many folders copying to one folder

Posted: 27 Mar 2014 11:09
by raylward102
both answers don't seem to include the find?

I had dir /s /b and find to find all instances of this file over many folders.
That is still important

Re: find filename in many folders copying to one folder

Posted: 27 Mar 2014 11:18
by Squashman
raylward102 wrote:both answers don't seem to include the find?

I had dir /s /b and find to find all instances of this file over many folders.
That is still important

That is what the FOR /R is doing.

Re: find filename in many folders copying to one folder

Posted: 27 Mar 2014 12:00
by raylward102
squashman,

I see it traversing the folders (searching as you say), but it only displays 100+ lines of
"The system cannot find the file specified"
I know the file is in 10 different folder trees, extending from the root folder where this script is executed from.
My other command; my first example finds all 10 instances of the file name test.txt

Re: find filename in many folders copying to one folder

Posted: 27 Mar 2014 12:10
by raylward102
actually; foxdrives example works! Sorry! Didn't see where it was outputting the files to

Re: find filename in many folders copying to one folder

Posted: 27 Mar 2014 12:24
by Squashman
raylward102 wrote:actually; foxdrives example works! Sorry! Didn't see where it was outputting the files to

Weird because I can't get his code to work from the cmd line.

Re: find filename in many folders copying to one folder

Posted: 27 Mar 2014 13:49
by raylward102
I can get his to run from the commandline; not from batch :(
What do I need to do to run from batch?

Re: find filename in many folders copying to one folder

Posted: 27 Mar 2014 15:37
by raylward102
Not kidding; Why cant I get this to run from a batch file?

Code: Select all

cmd /v:on /c "for /r %a in (test.txt) do (set /a c+=1 & copy "%a" "d:\!c!%~nxa")"


It's pretty much useless, If I can't schedule it to run from a batch.

Re: find filename in many folders copying to one folder

Posted: 27 Mar 2014 18:11
by foxidrive
raylward102 wrote:Not kidding; Why cant I get this to run from a batch file?


You didn't say that you wanted a batch file - your example was a command line. :D

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /r %%a in (test.txt) do (set /a c+=1 & copy "%%a" "d:\!c!%%~nxa")

Re: find filename in many folders copying to one folder

Posted: 27 Mar 2014 18:14
by foxidrive
Squashman wrote:
raylward102 wrote:actually; foxdrives example works! Sorry! Didn't see where it was outputting the files to

Weird because I can't get his code to work from the cmd line.


It was ok here from the cmd prompt in Win 8.1 and in a batch file when the usual % were doubled.

I did find a cosmetic flaw when I used text.txt as the search term and I only had test.txt files - it returned an error message for both files,
but it wasn't supposed to recognise them in the first place.

Re: find filename in many folders copying to one folder

Posted: 28 Mar 2014 05:33
by Squashman
Foxidrive,
When I run your code on Windows 7 from the cmd prompt it seems to think it finds test.txt in every directory. But if I run the search with a wildcard like test*.txt or test.txt* it only finds test.txt in the one directory I have it in.

Re: find filename in many folders copying to one folder

Posted: 28 Mar 2014 05:54
by foxidrive
Squashman wrote:Foxidrive,
When I run your code on Windows 7 from the cmd prompt it seems to think it finds test.txt in every directory. But if I run the search with a wildcard like test*.txt or test.txt* it only finds test.txt in the one directory I have it in.


I see, ta. That's the error I saw that was cosmetic.
In my test I only had two folders and both had the file, so I didn't see error messages for other folders.

I've tested it in a batch file too and the messages appear for other folders, but it copies the files correctly.

you're right in that a single wildcard after the filename fixes it - such as text.txt?

Re: find filename in many folders copying to one folder

Posted: 28 Mar 2014 09:40
by raylward102
Thanks foxdrive! Works nicely from batch now.