find filename in many folders copying to one folder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
raylward102
Posts: 9
Joined: 26 Mar 2010 12:30

find filename in many folders copying to one folder

#1 Post by raylward102 » 27 Mar 2014 10:33

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: find filename in many folders copying to one folder

#2 Post by foxidrive » 27 Mar 2014 10:49

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")"

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: find filename in many folders copying to one folder

#3 Post by Aacini » 27 Mar 2014 10:54

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

raylward102
Posts: 9
Joined: 26 Mar 2010 12:30

Re: find filename in many folders copying to one folder

#4 Post by raylward102 » 27 Mar 2014 11:09

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: find filename in many folders copying to one folder

#5 Post by Squashman » 27 Mar 2014 11:18

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.

raylward102
Posts: 9
Joined: 26 Mar 2010 12:30

Re: find filename in many folders copying to one folder

#6 Post by raylward102 » 27 Mar 2014 12:00

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

raylward102
Posts: 9
Joined: 26 Mar 2010 12:30

Re: find filename in many folders copying to one folder

#7 Post by raylward102 » 27 Mar 2014 12:10

actually; foxdrives example works! Sorry! Didn't see where it was outputting the files to

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: find filename in many folders copying to one folder

#8 Post by Squashman » 27 Mar 2014 12:24

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.

raylward102
Posts: 9
Joined: 26 Mar 2010 12:30

Re: find filename in many folders copying to one folder

#9 Post by raylward102 » 27 Mar 2014 13:49

I can get his to run from the commandline; not from batch :(
What do I need to do to run from batch?

raylward102
Posts: 9
Joined: 26 Mar 2010 12:30

Re: find filename in many folders copying to one folder

#10 Post by raylward102 » 27 Mar 2014 15:37

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: find filename in many folders copying to one folder

#11 Post by foxidrive » 27 Mar 2014 18:11

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")

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: find filename in many folders copying to one folder

#12 Post by foxidrive » 27 Mar 2014 18:14

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.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: find filename in many folders copying to one folder

#13 Post by Squashman » 28 Mar 2014 05:33

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: find filename in many folders copying to one folder

#14 Post by foxidrive » 28 Mar 2014 05:54

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?

raylward102
Posts: 9
Joined: 26 Mar 2010 12:30

Re: find filename in many folders copying to one folder

#15 Post by raylward102 » 28 Mar 2014 09:40

Thanks foxdrive! Works nicely from batch now.

Post Reply