Page 1 of 2

Need to find some specific files in removable drive

Posted: 09 Apr 2012 17:19
by prakashr85
Friends I need to find/search for the existence of some files and store the result with filename and existence/non-existence in a txt file.

for example

I need to find new.txt, three.jpg, ty.doc in drive D: and store the result in txt file as filename found/not found.

Thanks in advance.

Re: Need to find some specific files in removable drive

Posted: 09 Apr 2012 18:37
by aGerman
DIR /S should help to find the files. Try:

Code: Select all

@echo off &setlocal
set "drive=D:\"
set "file1=new.txt"
set "file2=three.jpg"
set "file3=ty.doc"

>"found.txt" (
  for /f "delims=" %%i in ('dir /a-d /b /s "%drive%%file1%" "%drive%%file2%" "%drive%%file3%" 2^>nul') do (
    if "%%~nxi"=="%file1%" (
      echo "%%i"
      set "found1=true"
    )
    if "%%~nxi"=="%file2%" (
      echo "%%i"
      set "found2=true"
    )
    if "%%~nxi"=="%file3%" (
      echo "%%i"
      set "found3=true"
    )
  )
)

>"not found.txt" (
  if not defined found1 echo "%file1%"
  if not defined found2 echo "%file2%"
  if not defined found3 echo "%file3%"
)

Regards
aGerman

Re: Need to find some specific files in removable drive

Posted: 09 Apr 2012 23:57
by foxidrive
I'd just change this:

if "%%~nxi"==

to this in three places, to make the match case insensitive.

if /i "%%~nxi"==

Re: Need to find some specific files in removable drive

Posted: 10 Apr 2012 03:43
by prakashr85
Thanks a billion,

But I need to find hundreds of files when I tried it took more than 5 minutes, can we still have any optimized code with getting filename from an ext. text file.

Re: Need to find some specific files in removable drive

Posted: 10 Apr 2012 04:06
by foxidrive
Is two batch files an option?

edited

Re: Need to find some specific files in removable drive

Posted: 10 Apr 2012 05:03
by prakashr85
Thanks, I need files that are not found in the list.txt also

Re: Need to find some specific files in removable drive

Posted: 10 Apr 2012 06:26
by foxidrive
Where are they found? Do you want to pass filenames on the command line to the batch file?

Re: Need to find some specific files in removable drive

Posted: 10 Apr 2012 07:08
by prakashr85
Thanks a lot for your timely help. Like the previous post code I need output files such as not-found.txt and found.txt in your code only found.txt is the only output.
For example I need to find 3 three files named one.txt, two.png, three.jpg in drive D: and drive d: has only one file say one.txt, your code returns only the files which are found in D: but I need both in separate files as found.txt(contains one.txt) and not found.txt (contains two.png, three.jpg) hope its clear now.

Re: Need to find some specific files in removable drive

Posted: 10 Apr 2012 07:20
by foxidrive
Does this work for you? The other first batch file is still needed to produce the filelist.txt

Code: Select all

:: create file list
@echo off
dir d:\ /a-d /b /s >filelist.txt


Code: Select all

@echo off
:: search for filenames
(
echo \new.txt
echo \three.jpg
echo \ty.doc
) > "list.txt

findstr /i /g:"list.txt" "filelist.txt" >"foundfiles.txt"
findstr /v /i /g:"filelist.txt" "list.txt" >"notfoundfiles.txt"


Edit: reversed the last line

Re: Need to find some specific files in removable drive

Posted: 10 Apr 2012 07:33
by prakashr85
notfound.txt lists all files from the drive D: except the one found, To made it simple


Code: Select all

Drive D: contains the following files
------------------------------------
ty.doc
two.jpg

INPUT
-------
List.txt (files that i need to search/find in D:)
---------------------------------------------
ty.doc
two.jpg
three.png

OUTPUT:
-----------
Found.txt contents
----------------------
ty.doc
two.jpg

Notfound.txt Contents
---------------------
three.jpg

Re: Need to find some specific files in removable drive

Posted: 10 Apr 2012 08:20
by foxidrive
Try the batch above again - I edited the post.

Re: Need to find some specific files in removable drive

Posted: 10 Apr 2012 08:44
by prakashr85
Tried notfound.txt displays all the contents of list.txt file

Re: Need to find some specific files in removable drive

Posted: 10 Apr 2012 08:59
by foxidrive
Ok, sorry for the false leads. This has been lightly tested. Try the batch file at the bottom which simulates a disk with three files, one of which is not in the list to be found.

Code: Select all

@echo off
:: create file list
@echo off
dir d:\ /a-d /b /s >filelist.txt

del "filelist2.txt"  2>nul
for /f "delims=" %%a in (
'type "filelist.txt"'
) do echo>>"filelist2.txt" \%%~nxa



Code: Select all

@echo off
:: search for filenames
(
echo \new.txt
echo \three.jpg
echo \ty.doc
) > "list.txt

findstr /i /g:"list.txt" "filelist.txt" >"foundfiles.txt"
findstr /v /i /g:"filelist2.txt" "list.txt" >"notfoundfiles.txt"




Code: Select all

@echo off
:: create file list
(
echo d:\folder\new.txt
echo d:\folder 3\threeB.jpg
echo d:\a place here\ty.doc
) > "filelist.txt

del "filelist2.txt"  2>nul
for /f "delims=" %%a in (
'type "filelist.txt"'
) do echo>>"filelist2.txt" \%%~nxa



@echo off
:: search for filenames
(
echo \new.txt
echo \three.jpg
echo \ty.doc
) > "list.txt

findstr /i /g:"list.txt" "filelist.txt" >"foundfiles.txt"
findstr /v /i /g:"filelist2.txt" "list.txt" >"notfoundfiles.txt"

Re: Need to find some specific files in removable drive

Posted: 10 Apr 2012 21:49
by prakashr85
Still I get all files in notfoundfiles.txt

Re: Need to find some specific files in removable drive

Posted: 11 Apr 2012 12:36
by foxidrive
Try the last batch file above which demonstrates the technique:

I only get this filename in the notfound file.

\three.jpg