Batch File Searching

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

MikeSwim07

Thread Starter
Joined
Apr 28, 2007
Messages
4,629
I need help making a batch file that searches the computer for certain filenames, someone helped me make this so far,

Code:
@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
 
Joined
Sep 27, 2008
Messages
116
i'm not sure you can get it to do something like that, batch files are just an automation of commands for the command prompt.. the find command doesn't allow you to specify that, maybe if you do a find for the specific folder first and then put that output into a variable.. and do the rest of the search off of that variable... but then again i'm not to sure that would work either or even the specifics... to tired at the moment to try and mock it up... but i'll give it a go later if it hasn't been solved by then...
 
Joined
Aug 7, 2007
Messages
9,028
Code:
@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:\*%%G* /L /B /S>> C:\kresults.txt
)
If not exist C:\kresults.txt echo "Files not found" >> C:\kresults.txt
start notepad C:\kresults.txt
exit
No need for the find, just let the dir command search for the pattern
The /A switch with no attributes specified does nothing (at least as far as I know), so no need to use it.

To search only folders with "cheese" as part of the name, just add another For loop to search for the folder then the inside loop searches the folders found.

Code:
@echo off
if exist C:\kresults.txt del /q C:\kresults.txt
Pushd C:\
For /F "tokens=*" %%a in ('dir *cheese* /s /b /ad') Do (
 FOR %%G IN (abc 123 tree) DO (
 echo Searching for %%G in %%a
 echo Searching for %%G in %%a>>C:\kresults.txt
 dir "%%a\*%%G*" /L /B /S>> C:\kresults.txt
))
If not exist C:\kresults.txt echo "Files not found" >> C:\kresults.txt
popd
start notepad C:\kresults.txt
exit
I added an echo statement to put the "Searching for..." statement into the results file. This way if no files are found, you'll know what directories were searched. If it finds no directories with "cheese" in the name, then the results file will contain just the "Files not found" statement.

HTH

Jerry
 
Joined
Sep 27, 2008
Messages
116
new to me, looks like i'm taking some notes... didn't realize you could do that without the find cmd.
 

MikeSwim07

Thread Starter
Joined
Apr 28, 2007
Messages
4,629
I have another question now,

How can I make a parser batch file, I could copy the results of the batch file you made, and open another batch file and it would parse the lines that I have specified to remove.

Does that make sense? sorry for the run on.
 
Joined
Aug 7, 2007
Messages
9,028
It can be done. Can even do it in the same file most likely.

"parse the lines that I have specified to remove" is unclear to me though. I don't know if you want to remove (throw away) specified lines, or remove (put into another file for further processing) specified lines, or something else entirely.

Examples are always good to avoid confusion in terminology.

Jerry
 

MikeSwim07

Thread Starter
Joined
Apr 28, 2007
Messages
4,629
So lets say the batch file you made me produced this txt file:

C:\abc.txt
C:\123.exe
C:\tree.bat

So I have already decided that I don't want abc.txt on the root drive, so I don't want the line C:\abc.txt.

I want to be able for the batch file to remove certain lines that I don't want (in my example abc.txt) and have those written to another file in the rootdrive called removed.txt
 
Joined
Aug 7, 2007
Messages
9,028
Ok, the first script searches for files that contain the specified strings anywhere in the file name. Could there be more than one file in a folder that matches one of the search patterns? Say the following:
C:\123.txt
C:\Jerrys123file.txt
C:\today.123

If that is possible, would you want to delete all three of these, or just the exact match 123.txt? The script below assumes there will only be one file that matches a particular pattern in a folder. If there are more than one, it will pick the last one as the one to remove, last usually being last in alphabetical order. As the first script doesn't specify a sort order, the "last" file might not be what you expect, so in that case you'd want to specify a sort order in the first script.

2nd, found a slight bug in the first script. c:\kresults.txt will contain a list of files that match the search strings (in the example abc,123, and tree). If you have a folder that contains one of those strings, the folder will also appear in the file and there won't be an easy way to tell if the line is a folder name or a file name without an extension.
Several solutions depending on what you need. Easiest is to modify the first script so c:\kresults.txt will only contain file names. If you need to check for folder names and perhaps do some processing on folder names, best to create a separate file for just folder names.

So modify the first one to this:
Code:
@echo off
if exist C:\kresults.txt del /q C:\kresults.txt
Pushd C:\
For /F "tokens=*" %%a in ('dir *cheese* /s /b /ad') Do (
 FOR %%G IN (abc 123 tree) DO (
 echo Searching for %%G in %%a
 echo Searching for %%G in %%a>>C:\kresults.txt
 dir "%%a\*%%G*" [COLOR=Red][B]/a-d[/B][/COLOR] /L /B /S>> C:\kresults.txt
))
If not exist C:\kresults.txt echo "Files not found" >> C:\kresults.txt
popd
start notepad C:\kresults.txt
exit
This will process the first file and create two more, remove.txt with the file name you want to remove, and keepers.txt which is kresults.txt with that one file name removed.

Code:
@echo off
Set _UnwantedFilePatteren=123
Set _UnwantedInFolder=c:\
Set _SF=C:\kresults.txt
@For /F "Tokens=*" %%I In ('findstr /I /C:%_UnwantedFilePatteren% %_SF%') Do (
  If /I "%%~dpI"=="%_UnwantedInFolder%" Set _UnwantedFile=%%I
   )
Set _OF1=C:\removed.txt
Set _OF2=C:\keeper.txt
If EXIST %_OF1% del %_OF1%
If EXIST %_OF2% del %_OF2%
findstr /i /C:"%_UnwantedFile%" "%_SF%">>%_OF1%
findstr /i /v /C:"%_UnwantedFile%" "%_SF%">>%_OF2%
:: Now delete the file that is listed in c:\removed.txt
For /F "usebackq tokens=*" %%I in ("%_OF1%") Do Del /F "%%I"
For /F "tokens=1 delims==" %%I In ('Set _') do Set %%I=
Jerry
 

MikeSwim07

Thread Starter
Joined
Apr 28, 2007
Messages
4,629
I guess my example was bad,

I want the parser batch to remove certain folders/files that I have specified before. Like I want it so the batch file (searcher one) searches for the files, creates the text file. Then another batch file reads that text file and removes the lines that I have spcified in another text file.
 
Joined
Aug 7, 2007
Messages
9,028
Still hanging in here. Sorry for the delay.

Code:
@echo off
Set _Remove=C:\ToBeRemoved.txt
Set _SF=C:\kresults.txt
Set _OF1=C:\removed.txt
Set _OF2=C:\keeper.txt
If EXIST %_OF1% del %_OF1%
If EXIST %_OF2% del %_OF2%
findstr /I /L /G:"%_Remove%" "%_SF%">>%_OF1%
findstr /I /L /V /G:"%_Remove%" "%_SF%">>%_OF2%
:: Now delete the file that is listed in c:\removed.txt (_OF1)
For /F "usebackq tokens=*" %%I in ("%_OF1%") Do Del /F "%%I"
For /F "tokens=1 delims==" %%I In ('Set _') do Set %%I=
If ToBeRemoved.txt is a list of file names, they will be removed from all folders. If you only want to remove files from specific folders, you need to include the folder as well, enough to be unique.

You can also just put say 123 in the ToBeRemoved.txt file, and all files with 123 anywhere in the name would be removed. If 123 exists in a folder name as well though, everything in that folder will be removed as well. You may want to use an echo statement to test first:
replace
Del /F "%%I"For /F
with
@echo File to delete is "%%I"
in the 2nd to last line.
This keeps the original file kresults.txt intact, creating a new file I called keeper.txt, and a file listing what was removed called removed.txt. You can always delete kresults.txt and rename keeper.txt to kresult.txt and delete removed.txt if you don't need to keep the original.

Jerry
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top