Solved batch script to find files in folder and subfolders

November 9, 2012 at 05:16:54
Specs: Windows 7
Hi,

I am completely new to this and would appreciate any help.
I have a .txt file that contains a list of file names. I need a batch script that will reference this list of file names and search through a folder and it's subfolders to find them. I then need it to generate a .txt that lists those files not found.

It seems like this question has been answered partially before on other posts but not exactly how I need it done and I'm having trouble modifying other scripts to fit my issue.

Thanks.


See More: batch script to find files in folder and subfolders

Report •


✔ Best Answer
November 9, 2012 at 13:27:04
@echo off & setlocal enabledelayedexpansion
type nul> %tmp%\log.txt
set log=%tmp%\log.txt
set list=C:\Users\Username\Documents\Lists\lists.txt
set cnt=0
set number=0
for /f "delims=" %%a in ('type %list%') do (
set /A cnt +=1
set file!cnt!=%%a
)
X:
cd X:\CountyLTmaps\Texas\Grimes\Lease Images
:LOOP
if %number%==%cnt% exit /b
set /A number +=1
dir /s !file%number%! || echo.!file%number%!>> %tmp%\log.txt
Goto :LOOP


#1
November 9, 2012 at 07:24:23
Very easy:
@echo off & setlocal enabledelayedexpansion
type nul> %tmp%\log.txt
set log=%tmp%\log.txt
set list=list.txt
set cnt=0
set number=0
set folder=?
set /P folder=Enter the main directory you want to search in:
for /f "delims=" %%a in ('type list.txt') do (
set /A cnt +=1
set file!cnt!=%%a
)
cd %folder%
:LOOP
if %number%==%cnt% exit /b
set /A number +=1
dir /s !file%number%! || echo.!file%number%!>> %tmp%\log.txt
Goto :LOOP

Report •

#2
November 9, 2012 at 09:24:19
Thanks for the quick response.
If I have the .txt located here C:\Users\Username\Documents\Lists
and the folder and sub-folders to be searched located here X:\State\County\Images.
Is there a format for specifying the directory path?

Report •

#3
November 9, 2012 at 09:49:49
Here, added comments.
@echo off & setlocal enabledelayedexpansion
::::Creates blank log file to store the not found files
type nul> %tmp%\log.txt
:::sets log file as variable
set log=%tmp%\log.txt
:::sets list location to list.txt in %cd% so enter the location of the file in replacement e.g %tmp%\list.txt
set list=list.txt
set cnt=0
set number=0
::::Prompts for directory
set folder=?
set /P folder=Enter the main directory you want to search in:
for /f "delims=" %%a in ('type list.txt') do (
set /A cnt +=1
set file!cnt!=%%a
)
::Moves to directory
cd %folder%
::::Loop to search for each file until limit is reached and it inserts any files not found into log file.
:LOOP
if %number%==%cnt% exit /b
set /A number +=1
dir /s !file%number%! || echo.!file%number%!>> %tmp%\log.txt
Goto :LOOP

In cases of switching drives, you just enter the driver letter before the cd %folder%.
E.g E:.


Report •

Related Solutions

#4
November 9, 2012 at 11:54:53
Please forgive my ignorance. I truly appreciate your help.
I'm not sure where and how to substitute the folder and drive names.
This is what I have from what you have provided.

@echo off & setlocal enabledelayedexpansion
type nul> %tmp%\log.txt
set log=%tmp%\log.txt
set list=C:\Users\username\Documents\BatchScripts\list.txt
set cnt=0
set number=0
set folder=?
set /P folder=X:\CountyLTmaps\Texas\Grimes\Lease Images
for /f "delims=" %%a in ('type list.txt') do (
set /A cnt +=1
set file!cnt!=%%a
)
X:cd %folder%
:LOOP
if %number%==%cnt% exit /b
set /A number +=1
dir /s !file%number%! || echo.!file%number%!>> %tmp%\log.txt
Goto :LOOP

I hate to make you spell it out for me but apparently that is what I need.
The C: drive contains the list.txt in the path above, and the folder "Lease Images" contains the multiple sub-folders and is on the X: drive.

No rush on this.

Thanks again.


Report •

#5
November 9, 2012 at 13:27:04
✔ Best Answer
@echo off & setlocal enabledelayedexpansion
type nul> %tmp%\log.txt
set log=%tmp%\log.txt
set list=C:\Users\Username\Documents\Lists\lists.txt
set cnt=0
set number=0
for /f "delims=" %%a in ('type %list%') do (
set /A cnt +=1
set file!cnt!=%%a
)
X:
cd X:\CountyLTmaps\Texas\Grimes\Lease Images
:LOOP
if %number%==%cnt% exit /b
set /A number +=1
dir /s !file%number%! || echo.!file%number%!>> %tmp%\log.txt
Goto :LOOP

Report •

#6
November 9, 2012 at 15:29:31
Aah, this line needs /D switch:
cd /D X:\CountyLTmaps\Texas\Grimes\Lease Images

(unless it's a seven thing... my seven os is down - can't test)
but pushd will eliminate that:
pushd X:\CountyLTmaps\Texas\Grimes\Lease Images

Here's a version using "findstr"

::===== begin script1
for /f "tokens=*"%%a in ('dir /s /b /a-d "X:\CountyLTmaps\Texas\Grimes\Lease Images" do @echo %%~nxa | findstr /i /L /v /g:"C:\Users\username\Documents\BatchScripts\list.txt
" >> notfound
::===== end script1
for /f "tokens=*" %%a in ('dir /s /b /a-d "X:\State\County\Images" do @echo %%~nxa | findstr /i /L /v /g:"C:\Users\Username\Documents\Lists\list.txt" >> notfound
::===== end script2

file "notfound" will be in whichever location the batchscript is run from. I made two scripts because I could not follow which set of path specs was the correct one. The second script was based on your original post, and the first was based on the latest version. Just make sure whatever paths used in the batchfile are the correct ones.


Report •

#7
November 9, 2012 at 23:08:51
@nbrane The /d isn't needed in this case since i've already switched drives. Anyway it works as it should. Tested your version, not getting results.

Report •

#8
November 9, 2012 at 23:41:44
Removed unecessary parts:
@echo off & setlocal enabledelayedexpansion
type nul> %tmp%\log.txt
set cnt=0
for /f "delims=" %%a in ('type C:\Users\Username\Documents\Lists\lists.txt') do (
set /A cnt +=1
set file!cnt!=%%a
)
:LOOP
if %cnt%==0 exit /b
set /A cnt -=1
dir /s X:\CountyLTmaps\Texas\Grimes\Lease Images\!file%number%! || echo.!file%number%!>> %tmp%\log.txt
Goto :LOOP

Report •

#9
November 10, 2012 at 22:21:20
Missed that drive-swap (X:), I guess it was the next line that fooled me. And yes, I hate to depend on findstr for ANYthing. Whenever I do, it fails me. I thought I had this one nailed, but apparently not...

@echo off >%tmp%\log.txt & setlocal enabledelayedexpansion
for /f "delims=" %%a in (C:\Users\Username\Documents\Lists\lists.txt) do (
dir /s /a-d "X:\CountyLTmaps\Texas\Grimes\Lease Images\%%a" || >>%tmp%\log.txt echo %%a
)


Report •

#10
November 12, 2012 at 06:08:17
Thanks to you both for continuing to help me with this. I've hit a snag though.

When I run this:

@echo off & setlocal enabledelayedexpansion
type nul> %tmp%\log.txt
set log=%tmp%\log.txt
set list=C:\Users\Username\Documents\Lists\lists.txt
set cnt=0
set number=0
for /f "delims=" %%a in ('type %list%') do (
set /A cnt +=1
set file!cnt!=%%a
)
X:
cd X:\CountyLTmaps\Texas\Grimes\Lease Images
:LOOP
if %number%==%cnt% exit /b
set /A number +=1
dir /s !file%number%! || echo.!file%number%!>> %tmp%\log.txt
Goto :LOOP

I get the following message:

Volum in drive C has no label.
Volume serial Number is bla bla bla
File Not Found

and it just repeats.

When I run this:

@echo off & setlocal enabledelayedexpansion
type nul> %tmp%\log.txt
set cnt=0
for /f "delims=" %%a in ('type C:\Users\Username\Documents\Lists\lists.txt') do (
set /A cnt +=1
set file!cnt!=%%a
)
:LOOP
if %cnt%==0 exit /b
set /A cnt -=1
dir /s X:\CountyLTmaps\Texas\Grimes\Lease Images\!file%number%! || echo.!file%number%!>> %tmp%\log.txt
Goto :LOOP

I get this:

The system cannot find the path specified

Any ideas?


Report •

#11
November 12, 2012 at 09:38:35
It looks like script #1 is working, but getting lots of "not found"s. If you don't want to see this output, change this line:
dir /s /b !file%number%! >nul 2>&1|| echo.!file%number%!>> %tmp%\log.txt

for the second script, add quotes here:
dir /s /b "X:\CountyLTmaps\Texas\Grimes\Lease Images\!file%number%!" || echo.!file%number%!>> %tmp%\log.txt


And, you never said what appeared in log.txt. It should have had a bunch of not-found filenames. If so, have you verified that they are indeed "among the missing"?
And, can you please post the content (or a small sample of the content) of the "list" file? It should look something like this:
file1.dat
file2.xls
file3.jpg


Report •

#12
November 12, 2012 at 11:22:10
My list.txt is simply a column of numbers i.e.

0021395
0025871

It is searching for .tif files that are named these numbers amongst various folders.

The generated log.txt is an exact copy of my list.txt


Report •

#13
November 12, 2012 at 17:18:12
oh. then you need to add the .tif extension into the 'dir' command:
dir /s /b !file%number%!.TIF >nul 2>&1|| echo.!file%number%!>> %tmp%\log.txt

into either or both scripts, whichever you want to use.


Report •

#14
November 13, 2012 at 05:52:50
Well that did it. Thanks. However I could never get it to read that X: drive so I had to move the search folder to the C:

@echo off & setlocal enabledelayedexpansion
type nul> %tmp%\log.txt
set log=%tmp%\log.txt
set list=C:\Users\username\Documents\BatchScripts\list.txt
set cnt=0
set number=0
for /f "delims=" %%a in ('type %list%') do (
set /A cnt +=1
set file!cnt!=%%a
)
C:
cd C:\Users\username\Documents\Temp Image Folder\Grimes\LeaseImages\
:LOOP
if %number%==%cnt% exit /b
set /A number +=1
dir /s !file%number%!.TIF >nul 2>&1|| echo.!file%number%!>> %tmp%\log.txt
Goto :LOOP

How do I change the destination of the generated log.txt? Also, as you can see I kept the "cd" in the script even though it isn't changing drives. I wasn't sure how to delete that. Suggestions?

Thanks


Report •

#15
November 13, 2012 at 07:59:16
At the top, change to something like this:

set tmp=c:\whatever\blah
type nul> %tmp%\log.txt

(i moved the "type nul" down so the location is only defined once).

and for the other, it's purely cosmetic, but:
cd \Users\username\Documents\Temp Image Folder\Grimes\LeaseImages\


Report •

#16
November 13, 2012 at 10:57:26
I can't thank you both enough. I appreciate your help and patience.

Cheers


Report •

#17
November 16, 2012 at 05:15:37
One quick addendum.

If I am still using the list.txt which contains the list of numbers i.e.
085465
097554

but the naming convention for the .TIF is R085465V42p126

Will the script be able to find the number from the list.txt in this naming convention?
Are wildcards needed? If so, how do you place those?

Thanks


Report •

#18
November 16, 2012 at 08:02:11
yeh, maybe like this:
dir /s R!file%number%!*.TIF >nul 2>&1|| echo.!file%number%!>> %tmp%\log.txt

Report •


Ask Question