Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • September 25, 2016, 07:13:11 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Find all files in a directory that match a list of names and make copy of each  (Read 1647 times)

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 499
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
I have the list of names of missing pdf files that are all mixed up in a folder of other files.

I need a way to :
Search through the entire directory recursively looking for a match to each name in the list
if it finds one, make a copy of it and put it in a folder on c:
I can put them where they belong by copying that folder to a different system

I tried Robocopy as this>      "for /f %%f in (list.txt) do robocopy c:\source d:\dest %%f  "

But it dies so fast i barely see a flash.

I could have sworn i already had a program or script to do this but can't find it.

Thanks for help.

Thanks "4wd"
Problem solved :)
« Last Edit: February 28, 2015, 07:12:06 PM by questorfla »

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 4,394
    • View Profile
    • Donate to Member
Doesn't like '&' in the file names but it seems to work:

Code: Text [Select]
  1. @echo off
  2. set source=C:\source
  3. set dest=D:\dest
  4. set list=C:\list.txt
  5. pushd %source%
  6. for /f "tokens=*" %%a in (%list%) do (echo.%%a && call :FindIt "%%~a")
  7. goto :End
  8.  
  9.  
  10. :FindIt
  11. for /f "tokens=* usebackq" %%b in (`dir /b /s /a-d-s-h "%~1"`) do (call :DoIt "%%~dpb" "%~1")
  12. goto :EOF
  13.  
  14. :DoIt
  15. set str=%~1
  16. set str=%str:~0,-1%
  17. set file=%~2
  18. rem NOTE: omit /MOV to COPY instead
  19. robocopy "%str%" "%dest%" "%file%" /MOV
  20. goto :EOF
  21.  
  22. :End
  23. popd

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 499
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
this is my own addition to the mix.  I still don't have it working as needed but the windows command "Findstr" is made to search for files using a list of names.
I have not yet figured out how to get it to look only for file NAMES though instead of looking through the contents of the files.
Also, the default options of what to do with a match do not include sending a copy to another folder.
I just thought if I threw this in, someone might know of a combination of switches to get it to work with a name like:
 "Herman, George a List of Favorite Publications.pdf" and over 1000 more like it.

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 499
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
Doesn't like '&' in the file names but it seems to work:

Code: Text [Select]
  1. @echo off
  2. set source=C:\source
  3. set dest=D:\dest
  4. set list=C:\list.txt
  5. pushd %source%
  6. for /f "tokens=*" %%a in (%list%) do (echo.%%a && call :FindIt "%%~a")
  7. goto :End
  8.  
  9.  
  10. :FindIt
  11. for /f "tokens=* usebackq" %%b in (`dir /b /s /a-d-s-h "%~1"`) do (call :DoIt "%%~dpb" "%~1")
  12. goto :EOF
  13.  
  14. :DoIt
  15. set str=%~1
  16. set str=%str:~0,-1%
  17. set file=%~2
  18. rem NOTE: omit /MOV to COPY instead
  19. robocopy "%str%" "%dest%" "%file%" /MOV
  20. goto :EOF
  21.  
  22. :End
  23. popd

WORKED  and true about the &  many thanks! :D :Thmbsup:
4wd wins again.

tomos

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 10,238
    • View Profile
    • Donate to Member
^ I'm confused: does this mean your other thread on the topic is no longer relevant?
If so could you update the other thread? (If not, apologies for the confusion.)
Tom

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 499
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
sort of
It turns out there is one small slitch as stated above.
In the end, i stil have several hundred fiules that DO have the & sign in them
If i had a way to compare hases of the files instead of names i could get by this
otherwise it is back to "by hand" on those that have the "&"

Thanks for reminding me i had another post  which forum was it is so i can move/remove/merge the two

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 4,394
    • View Profile
    • Donate to Member
You could probably fix the & in file names thing by escaping them, (^&) - trouble is DOS is so convoluted when doing something like that, (even though string substitution is relatively easy), that it's usually quicker and easier to write something in AHK or AutoIt.

I just do it in DOS for the intellectual exercise ... or because I'm a masochist  ;D

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 499
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
My vote is for Masochist!~! :D

I just loaded the Gnu Win toolbox and am trying to state the issue is "sed" or some other Gnu tool
The FIRST thing I did which i am now very glad of was to take the 100 websites using the files in their DB;s Off-line long enough to extract a copy of every pdf file so i could search without fear of loss.

All the files that were missing are in the Websites which while active I cannot do much.
Working with a "copy" gives me a lot more confidence :)
So I am considering doing the same thing to the actual file NAMES that I did to the file LIST
change all the & to any other "unique" character or phrase so i can run those last ones and be done with it.

If there WAS a way to do a quick hash of the filename and run it against the hash of the names in the list, that SHOULD also be a working solution
you wrote you code so tight though i Dont know where i can insert the hash conversion to use the results for comparison rather than the name itself.
Can you "spread it out a little" for "Scripting for Dummies?"  Give me room to insert the conversion into the loop?  it runs so fast now that I see only a blur.  I removed the @echo off due to the run time with no response and i needed to see what was going on in order to find that "_" that he had used a " " instead.
« Last Edit: March 01, 2015, 10:32:55 PM by questorfla, Reason: add »