Computing.Net > Forums > Programming > Batch File to find files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch File to find files

Reply to Message Icon

Name: MikeSwim07
Date: October 5, 2008 at 15:54:15 Pacific
OS: Windows XP Home
CPU/Ram: N/A
Product: Dell
Comment:

Hey,

I need help making a batch file that searches the computer for certain filenames, someone helped me make this so far,

@echo off
if exist C:\kresults.txt del /q C:\kresults.txt
FOR %%G IN (
abc
123
tree
) DO (
echo Searching for %%G
dir C:\*.* /L /A /B /S|Find "%%G" >> C:\kresults.txt
)
If not exist C:\kresults.txt echo "Files not found" >> C:\kresults.txt
start notepad C:\kresults.txt
exit

this works but how can I do this,

How can I make it search only in folders with a certain name(s), like "Cheese"

Thank you



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: October 5, 2008 at 20:36:34 Pacific
Reply:

[edit: small fixes to the script]

I made a few improvements.....


@ECHO OFF
SET FLNM=
SET DRV=
SET DIRS=
:NAME
CLS
SET /P FLNM=INPUT [A] FILENAME[S] TO SEARCH FOR SEPERATED BY [A] COMMA[S]. DO NOT CONTAIN ANY FILE NAMES IN "QUOTES", ^
EVEN IF THEY CONAIN SPACES. IF THE FILE NAME[S] CONTAINS [A] SPACE[S] SOME FALSE POSITIVES MAY BE RETURNED. RESULTS WILL ^
ONLY BE RETURNED FROM THE DRIVE[S] AND DIRECTOR[Y/IES] SPECIFIED IN THE NEXT STEPS.
IF NOT DEFINED FLNM GOTO NAME
IF /I "%FLNM%"=="/Q" GOTO :EOF
SET FLNM=%FLNM: =.%
:DV
CLS
SET /P DRV=INPUT THE LETTER[S] OF THE DRIVE[S] YOU WANT SEARCHED, USE THE LETTER ONLY DON'T INCLUDE THE COLON ":". ^
SEPERATE EACH LETTER BY A COMMAS, THEN PRESS ENTER.
IF NOT DEFINED DRV GOTO DV
IF EXIST KRESULTS.TXT DEL KRESULTS.TXT
CLS
SET /P DIRS=INPUT THE DIRECTORY/FOLDER NAMES YOU WOULD LIKE RESULTS RETURNED FROM. SEPERATED EACH ENTRY WITH A COMMA. DO ^
NOT CONTAIN ANY DIRECTORY/FOLDER NAMES IN "QUOTES", EVEN IF THEY CONAIN SPACES. DIRECTORY/FOLDER NAMES THAT CONTAIN [A] ^
SPACE[S] MAY CAUSE SOME FALSE POSITIVES. RESULTS FROM ALL DIRECTORYS WITH THIS NAME CONTAINED WITHIN THE SPECIFIED DRIVE[S] ^
WILL BE RETURNED. IF YOU WANT RESULTS RETURNED FROM [ALL OF] THE DRIVE[S] SELECTED IN THE LAST STEP DON'T ENTERING ANYTHING. ^
AFTER YOU HAVE ENTERED ANY DESIRED FOLDER/DIRECTORY NAME[S] PRESS ENTER TO CONTINUE.
IF DEFINED DIRS SET DIRS=%DIRS: =.%
FOR %%D IN (%DRV%) DO (
FOR %%G IN (%FLNM%) DO (
IF DEFINED DIRS (FOR %%C IN (%DIRS%) DO (DIR %%D:\ /L /B /S|FINDSTR /I /E \%%C.\%%G>>KRESULTS.TXT)) ELSE (^
DIR %%D:\ /L /B /S|FINDSTR /I /E \%%G>>KRESULTS.TXT)
)
)
CALL :ZEROBYTE KRESULTS.TXT
IF NOT EXIST KRESULTS.TXT ECHO:"FILES NOT FOUND">>KRESULTS.TXT
START NOTEPAD KRESULTS.TXT
EXIT /B

:ZEROBYTE
IF %~Z1==0 DEL KRESULTS.TXT
GOTO :EOF

This one will return a few false positives, for example say you have three files "txt file.txt", "txttfile.txt" "txtyfileytxt" they would all get pulled up by searching for "txt.file.txt". This happens because findstr treats "." as a wild card meaning it can be any character. I think it shouldn't cause much if any trouble unless you have a lot of files with sequenced names and your searching for one of them.

It will also be touchy about certain characters(but so would yours). It's probably not as efficient as it could be but it was still quite fast for me, so I didn't bother optimising.


Note: the carat's at the ends of a few of the lines in the script are to keep the page from stretching too much, it still works with them there but they can safley be removed provided that the line break folowing them is also removed.


0

Response Number 2
Name: AK12
Date: October 6, 2008 at 03:02:37 Pacific
Reply:

Hi,

I am also trying to do the same thing.

But instead of entering the filename to search for, can the batch read a list of filenames from a .txt file?

So I can search for 30 files in one go.

Thank you,


0

Response Number 3
Name: Judago
Date: October 6, 2008 at 03:36:48 Pacific
Reply:

Just remove the set /p flnm=<all of this text> statment and change this:


FOR %%G IN (%FLNM%) DO (

to:


FOR /F "DELIMS=" %%G IN (YOURTEXTFILE) DO (

File names with spaces need special care as always......


0

Response Number 4
Name: AK12
Date: October 6, 2008 at 04:21:29 Pacific
Reply:

Hi,

Works just as I expected.

I can't believe that such an easy tool is so hard to find on the net. All I was able to find, where tools that search FOR a list of strings IN multiple files...


Thanks a lot.


0

Response Number 5
Name: pball
Date: October 6, 2008 at 05:36:02 Pacific
Reply:

sorry to intrude but I have a question about the carat's that isn't worth a new thread.

So the carat lets you take a really long line and break it to multiple lines? Cause I've been wondering how to do that, since I have some annoyingly long lines for program settings.


0

Related Posts

See More



Response Number 6
Name: Judago
Date: October 6, 2008 at 14:02:36 Pacific
Reply:

pball, Yes I do remember your thread, it's something I have picked up since then. It does work, but it can be a little touchy, it doesn't like certain characters starting the new line(like the left barchet "(") and I think there in a limit of something like four or five per line. I'm also not sure if it works with every command, I'm very new to it.

If you use it it may cause you to tear out your hair a little. I wrote the above batch with long lines, got it working, then used the carats before a final test then posting. The carats didn't work first time....


0

Response Number 7
Name: pball
Date: October 6, 2008 at 14:29:24 Pacific
Reply:

ok thanks for the info

I tried it with an echo command and it worked, but I'll use it with caution with your warning.


0

Response Number 8
Name: Nor (by NNoo)
Date: November 3, 2008 at 07:54:17 Pacific
Reply:

Hello,

I am new at this and I have to create a batch file to find a set of filenames with all the path like C:\Users\pc\Documents\ok.txt, etc. The batch file must say if they exist or not.
It seems that something similar was posted but
I don't understand. Can somebody help me?


0

Response Number 9
Name: Mechanix2Go
Date: November 3, 2008 at 18:28:15 Pacific
Reply:

@echo off

for /f "tokens=* delims= " %%a in (filelist.txt) do (
if exist %%a echo %%a is there
)


=====================================
If at first you don't succeed, you're about average.

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch File to find files

batch script to upload to ftp www.computing.net/answers/programming/batch-script-to-upload-to-ftp/16987.html

batch script to replace filenames h www.computing.net/answers/programming/batch-script-to-replace-filenames-h/16301.html

Batch file to find drive letter www.computing.net/answers/programming/batch-file-to-find-drive-letter/15558.html