Jump to content

Batch File!


Sh4dow
 Share

Recommended Posts

I need a batch file that will look inside of files for certain words:

hat, cat, rat

It 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


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 *.txt

You 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 *.txt

Then you would search for any occurance of three letter words that end in at. then just compare the output

Or remember that findstr can read in for console with /F:/

Here is an example

c:\>FINDSTR /S /L /M a *.txt > a.list
C:\>FINDSTR /S /L /M /F:a.list b > a-b.list

The 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.list

Now remember that with three words you have several combinations.

words a, b, c

combinations include

a - b,

a - c,

b - c,

and then if you want a list of all three you need

a - b -c

I 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

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

I'm having problems still. <~~~worst coder EVER!

ok here goes:

FINDSTR /s /l /m rat *.txt > a.list

FINDSTR /s /l /m hat *.txt > b.list

FINDSTR /s /l /m cat *.txt > c.list

C:\>FINDSTR /S /L /M /F:a.list b > a-b.list

C:\>FINDSTR /S /L /M /F:a.list c > a-c.list

c:\>FINDSTR /S /L /M /F:b.list c > b-c.list

???????? :}

Link to comment
Share on other sites

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.list

FINDSTR /s /l /m hat *.txt > b.list

FINDSTR /s /l /m cat *.txt > c.list

C:\>FINDSTR /S /L /M /F:a.list b > a-b.list

C:\>FINDSTR /S /L /M /F:a.list c > a-c.list

c:\>FINDSTR /S /L /M /F:b.list c > b-c.list

The 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

Here A Example Cmd It Set Up To Look For CMD And VBS Files

From 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 Cmd

FINDSTR /S /L /D:C:\;D:\;E:\;F:\;G:\;I:\ /M a *.cmd > cmd.txt

Echo Searching For All CMD && Ping -n 3 127.0.0.1>nul

cls && Color 4e && Title Search For Vbs

FINDSTR /S /L /D:C:\;D:\;E:\;F:\;G:\;I:\ /M a *.vbs > vbs.txt

Echo Searching For All VBS && Ping -n 3 127.0.0.1>nul

goto EOF

exit

Edited by gunsmokingman
Link to comment
Share on other sites

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

FINDSTR /s /l /m "rat" *.txt > a.list

FINDSTR /s /l /m "hat" *.txt > b.list

FINDSTR /s /l /m "cat" *.txt > c.list

FINDSTR /S /L /M /F:a.list rat > a-b.list

FINDSTR /S /L /M /F:b.list hat > a-c.list

FINDSTR /S /L /M /F:c.list cat > b-c.list

Do 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

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.list

This 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

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.list

Is that correct?

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.


×
×
  • Create New...