Sh4dow Posted April 12, 2005 Share Posted April 12, 2005 I need a batch file that will look inside of files for certain words:hat, cat, ratIt can be 3 different searches, but must report the file name if it has TWO of the words. It can have all 3 of the words as well.I know this is probably easy, but I've never written a batch file before. Can anyone assist me?Thank you! Link to comment Share on other sites More sharing options...
purewaveform Posted April 12, 2005 Share Posted April 12, 2005 I dont want to give you the answer, becuase you wouldnt learn anything. but here is how you would set it up. the command to look inside a file for a string is findstr.here is an example to search your entire harddrive for "rat" in all text files and only display the text finle name.c:\>FINDSTR /S /L /M rat *.txtYou start the loop by looking at the above file, maybe something like a pipe or a redirect, you will need to learn the diffrence, into the same command again. This way you will search for the files that have the next word. Or just get the first list of rat and send all the filenames into a tmp file by taking the output of the command > and redirecting it to a file. Then loop through that list.OR if you use something like c:\>FINDSTR /S /L /M .at *.txtThen you would search for any occurance of three letter words that end in at. then just compare the outputOr remember that findstr can read in for console with /F:/Here is an example c:\>FINDSTR /S /L /M a *.txt > a.listC:\>FINDSTR /S /L /M /F:a.list b > a-b.listThe above command will search for all .txt files on the computer with the letter a and then write a file list to the hd and then second command will search that list of files for the letter b and write that result to the file a-b.listNow remember that with three words you have several combinations.words a, b, ccombinations includea - b, a - c,b - c,and then if you want a list of all three you needa - b -cI want you to post the final result when you get it. You should have everything you need here. Link to comment Share on other sites More sharing options...
Sh4dow Posted April 12, 2005 Author Share Posted April 12, 2005 Thank you for your assistance. I will get to work on it Link to comment Share on other sites More sharing options...
suryad Posted April 12, 2005 Share Posted April 12, 2005 purewaveform seems like you know a lot about the Windows command prompt. The findstr function is especially like the Linux grep command isnt it? That is quite interesting. I am sure it would be of a lot of help if you could create a post and sticky it and explain all the details of what is possible with the command prompt in Windows. I know it would benefit me for sure and a host of other people who think that the Windows command prompt is not as powerful as the shells in Linux. Link to comment Share on other sites More sharing options...
DiGGiTY Posted April 12, 2005 Share Posted April 12, 2005 I can add content to the forum so you're not saddled with doing it all yourself... Link to comment Share on other sites More sharing options...
Sh4dow Posted April 12, 2005 Author Share Posted April 12, 2005 I'm having problems still. <~~~worst coder EVER!ok here goes:FINDSTR /s /l /m rat *.txt > a.listFINDSTR /s /l /m hat *.txt > b.listFINDSTR /s /l /m cat *.txt > c.listC:\>FINDSTR /S /L /M /F:a.list b > a-b.listC:\>FINDSTR /S /L /M /F:a.list c > a-c.listc:\>FINDSTR /S /L /M /F:b.list c > b-c.list???????? Link to comment Share on other sites More sharing options...
purewaveform Posted April 12, 2005 Share Posted April 12, 2005 I have been batch scripting since 1983, not kidding. I grew up on it, so when new commands came out, I just added them to my memory. If people want really in depth batch help, not a problem. You can do just about anything from prompt. And with vbs, you can access just about any part of windows that you want. If someone tells me where they want me to put some knowledge then just ask, be happy to help.As for the quesiton.FINDSTR /s /l /m rat *.txt > a.listFINDSTR /s /l /m hat *.txt > b.listFINDSTR /s /l /m cat *.txt > c.listC:\>FINDSTR /S /L /M /F:a.list b > a-b.listC:\>FINDSTR /S /L /M /F:a.list c > a-c.listc:\>FINDSTR /S /L /M /F:b.list c > b-c.listThe first three lines are perfect, you will find all occurances of rat in a.list, hat in b.list, and cat in c.list. Now the next three lines need some help. then you do findstr you need to still tell it what you are looking for. In line 4, you are telling it to look for the letter b in a.list. I think that you want to be looking for hat, not a like this.C:>\FINDSTR /S /L /M /F:a.list hat > a-b.list then just repeat for the other entries, in lines 5, 6 you are looking for the string "c" If you changes the c to cat etc, you will have the right answers!Tell me how it goes! Link to comment Share on other sites More sharing options...
gunsmokingman Posted April 12, 2005 Share Posted April 12, 2005 (edited) Here A Example Cmd It Set Up To Look For CMD And VBS FilesFrom These Location C:\;D:\;E:\;F:\;G:\;I:\, This Took A While To Complete It Search Echo off && CLS && Color 5e && Mode 55,5 && Title Search For CmdFINDSTR /S /L /D:C:\;D:\;E:\;F:\;G:\;I:\ /M a *.cmd > cmd.txtEcho Searching For All CMD && Ping -n 3 127.0.0.1>nulcls && Color 4e && Title Search For VbsFINDSTR /S /L /D:C:\;D:\;E:\;F:\;G:\;I:\ /M a *.vbs > vbs.txtEcho Searching For All VBS && Ping -n 3 127.0.0.1>nulgoto EOFexit Edited April 12, 2005 by gunsmokingman Link to comment Share on other sites More sharing options...
purewaveform Posted April 12, 2005 Share Posted April 12, 2005 what is important to note is that the /D is sor searching a semicolon delimited list of directories. although I believe that the pings are unneccesary, it should suspend while running the command. Link to comment Share on other sites More sharing options...
suryad Posted April 13, 2005 Share Posted April 13, 2005 Well err I thought maybe you could start a post purewaveform and then the mods could have it stickied or something like that. Its pretty cool to know that a lot of things can be accomplished using the command prompt.For starters I found this link that has a reference of the command shell commands here:Command line commands Link to comment Share on other sites More sharing options...
purewaveform Posted April 13, 2005 Share Posted April 13, 2005 Sure, let me organize a couple of my better ones, and I'll post them. If you have any ideas on stuff that would be cool to from prompt, just tell me. Link to comment Share on other sites More sharing options...
Sh4dow Posted April 13, 2005 Author Share Posted April 13, 2005 FINDSTR /s /l /m "rat" *.txt > a.listFINDSTR /s /l /m "hat" *.txt > b.listFINDSTR /s /l /m "cat" *.txt > c.listFINDSTR /S /L /M /F:a.list rat > a-b.listFINDSTR /S /L /M /F:b.list hat > a-c.listFINDSTR /S /L /M /F:c.list cat > b-c.listDo I need the Quotation marks in the first 3 lines ex) "rat"Now I have a new set of problems. I made 3 test files, called test1, test2, test3.In these files I have 1 of the words, 2 of the words, then all three of the words.This script is finding words such as eduCATion and tHAT. I do not want it to do that. I need this script to find an exact match of 2 or 3 of the words in any combination then tell me which files contain those matches. =/ This is going to require much more work. Any hints or suggestions would be GREATLY appreciated!! Link to comment Share on other sites More sharing options...
purewaveform Posted April 13, 2005 Share Posted April 13, 2005 you know, doing a findstr /? will give you the complete syntax, but here is how.If you want to find a string literal, hence " rat " you have to use the /c it works better becuase it allows the use of quites. like this.C:\>FINDSTR /I /M /S /C:" rat " *.txt > rat.listThis will find all txt files that contain " rat " in them and it should be case insensitive, hence the /i for it Specifies that the search is not to be case-sensitive. Integrate this, and you whould be done. Link to comment Share on other sites More sharing options...
Sh4dow Posted April 13, 2005 Author Share Posted April 13, 2005 in order to compare the files, would it be the same code I used before just modifying the words. For ex)FINDSTR /S /L /M /F:rat.list rat > rat-hat.listIs that correct? Link to comment Share on other sites More sharing options...
purewaveform Posted April 13, 2005 Share Posted April 13, 2005 almost, you would want to do the /c:" hat " instead of using the /l on the two words. That way you get to find the actual word. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now