Need to find some specific files in removable drive

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
prakashr85
Posts: 8
Joined: 09 Apr 2012 17:13

Need to find some specific files in removable drive

#1 Post by prakashr85 » 09 Apr 2012 17:19

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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Need to find some specific files in removable drive

#2 Post by aGerman » 09 Apr 2012 18:37

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

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

Re: Need to find some specific files in removable drive

#3 Post by foxidrive » 09 Apr 2012 23:57

I'd just change this:

if "%%~nxi"==

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

if /i "%%~nxi"==

prakashr85
Posts: 8
Joined: 09 Apr 2012 17:13

Re: Need to find some specific files in removable drive

#4 Post by prakashr85 » 10 Apr 2012 03:43

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.

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

Re: Need to find some specific files in removable drive

#5 Post by foxidrive » 10 Apr 2012 04:06

Is two batch files an option?

edited
Last edited by foxidrive on 10 Apr 2012 07:21, edited 1 time in total.

prakashr85
Posts: 8
Joined: 09 Apr 2012 17:13

Re: Need to find some specific files in removable drive

#6 Post by prakashr85 » 10 Apr 2012 05:03

Thanks, I need files that are not found in the list.txt also

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

Re: Need to find some specific files in removable drive

#7 Post by foxidrive » 10 Apr 2012 06:26

Where are they found? Do you want to pass filenames on the command line to the batch file?

prakashr85
Posts: 8
Joined: 09 Apr 2012 17:13

Re: Need to find some specific files in removable drive

#8 Post by prakashr85 » 10 Apr 2012 07:08

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.

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

Re: Need to find some specific files in removable drive

#9 Post by foxidrive » 10 Apr 2012 07:20

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
Last edited by foxidrive on 10 Apr 2012 08:19, edited 1 time in total.

prakashr85
Posts: 8
Joined: 09 Apr 2012 17:13

Re: Need to find some specific files in removable drive

#10 Post by prakashr85 » 10 Apr 2012 07:33

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

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

Re: Need to find some specific files in removable drive

#11 Post by foxidrive » 10 Apr 2012 08:20

Try the batch above again - I edited the post.

prakashr85
Posts: 8
Joined: 09 Apr 2012 17:13

Re: Need to find some specific files in removable drive

#12 Post by prakashr85 » 10 Apr 2012 08:44

Tried notfound.txt displays all the contents of list.txt file

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

Re: Need to find some specific files in removable drive

#13 Post by foxidrive » 10 Apr 2012 08:59

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"

prakashr85
Posts: 8
Joined: 09 Apr 2012 17:13

Re: Need to find some specific files in removable drive

#14 Post by prakashr85 » 10 Apr 2012 21:49

Still I get all files in notfoundfiles.txt

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

Re: Need to find some specific files in removable drive

#15 Post by foxidrive » 11 Apr 2012 12:36

Try the last batch file above which demonstrates the technique:

I only get this filename in the notfound file.

\three.jpg

Post Reply