Jump to content

Batch File!


Sh4dow

Recommended Posts

Ok, I think it's like this:

FINDSTR /I /M /S /C:" rat " *.txt > rat.list

FINDSTR /I /M /S /C:" hat " *.txt > hat.list

FINDSTR /I /M /S /C:" cat " *.txt > cat.list

Then to compare the files to see if it contains 2 or 3 of the words I came up with this:

FINDSTR /I /M /S /F:rat.list rat > rat-hat.list

FINDSTR /I /M /S /F:hat.list hat > rat-cat.list

FINDSTR /I /M /S /F:cat.list cat > hat-cat.list

??

Link to comment
Share on other sites


FINDSTR /I /M /S /F:rat.list /C:" hat " > rat-hat.list

That way it will search for the literal " hat " in the rat list, and wala you will have a rat-hat list!

What happens if hat is at the beginning or end of a line, there will not be a space on each side of it?
Link to comment
Share on other sites

your right. But that is you can run several other commands, like redo the search and instead of /c:" hat " you can use /b /c:"hat " that way it will only search at the beginning of the line. I will post the complete answer in a an hours or so.

Link to comment
Share on other sites

findstr does not always work. Unfortunately I do this all time. Here is a better solution... more lines but works all the time. find works better. find /i make the search Not case senstive.

@echo off

find /i "rat" a.txt >nul

if %errorlevel%==0 goto foundarat

if %errorlevel%==1 goto norat

if %errorlevel%==9009 goto wherefind

pause

goto :eof

:foundarat

echo I found a rat.

pause

goto :eof

:norat

if not exist a.txt echo I cannot find a.txt, so there is no rat.

if exist a.txt echo I cannot find a rat.

pause

goto :eof

:wherefind

echo I cannot find find.

pause

goto :eof

Link to comment
Share on other sites

just remember that if you were going to do it that way in the last post, then you need to have sections that when found rat you did the sarch in that file to find all occurances of the next word. Here is a copmlete working solution

FINDSTR /I /M /S /C:" hat " *.* > hat.list
FINDSTR /I /M /S /B /C:"hat " *.* >> hat.list
FINDSTR /I /M /S /E /C:" hat" *.* >> hat.list

FINDSTR /I /M /S /C:" cat " *.* > cat.list
FINDSTR /I /M /S /B /C:"cat " *.* >> cat.list
FINDSTR /I /M /S /E /C:" cat" *.* >> cat.list

FINDSTR /I /M /S /C:" rat " *.* > rat.list
FINDSTR /I /M /S /B /C:"rat " *.* >> rat.list
FINDSTR /I /M /S /E /C:" rat" *.* >> rat.list

FINDSTR /I /M /S /C:" cat " /L:hat.list > cat-hat.list
FINDSTR /I /M /S /B /C:"cat " /L:hat.list > cat-hat.list
FINDSTR /I /M /S /E /C:" cat" /L:hat.list > cat-hat.list

FINDSTR /I /M /S /C:" rat " /L:hat.list > rat-hat.list
FINDSTR /I /M /S /B /C:"rat " /L:hat.list > rat-hat.list
FINDSTR /I /M /S /E /C:" rat" /L:hat.list > rat-hat.list

FINDSTR /I /M /S /C:" rat " /L:cat.list > rat-cat.list
FINDSTR /I /M /S /B /C:"rat " /L:cat.list > rat-cat.list
FINDSTR /I /M /S /E /C:" rat" /L:cat.list > rat-cat.list

FINDSTR /I /M /S /C:" hat " /L:rat-cat.list > hat-rat-cat.list
FINDSTR /I /M /S /B /C:"hat " /L:rat-cat.list > hat-rat-cat.list
FINDSTR /I /M /S /E /C:" hat" /L:rat-cat.list > hat-rat-cat.list

That should find all occurances of hat, cat, rat wether it is at the end, beginning, or in the middle of a line in all files on the computer, not just txt files. (barring any typos) Now this should take a while to run. As for the error in the last post, it should just post it to the command line, not the redirect.

Link to comment
Share on other sites

The above post gets to line 10 and hangs. I then receive errors:

FINDSTR: /: ignored

FINDSTR: /h ignored

FINDSTR: /a ignored

FINDSTR: /t ignored

FINDSTR: /. ignored

FINDSTR: /t ignored

Nothing happens after that, I think it's stuck in a loop?

Link to comment
Share on other sites

Try changing the all instances of /L: to /F: in the example, also I think that the 2nd and 3rd lines in each of the last four sections should have >>instead of > for the output file otherwise they will overwrite the last input to it.

Link to comment
Share on other sites

Remember what I am trying to accomplish is not to find a single word, but 2 or 3 of the words within a file then report which files those 2 or 3 words are in. No matter which way I try, I cannot script a solution that works yet :(

Another question. What if the words are not in that order? Will that make a difference?

I have 3 test files that read:

test1.txt "The cat is named garfield. He is a fat cat."

test2.txt "The cat likes to wear a funny hat."

test3.txt "The fat cat who likes to wear a funny hat ate the skinny rat."

I feel like Dr. Suess!

Link to comment
Share on other sites

/L -Uses search strings literally.

/F:file -Reads file list from the specified file

> -Directs output creating a new entry

>> -Directs output appending to an entry

Just try what I said, and see what happens!

Link to comment
Share on other sites

That being said, I edited purewaveform's script to this:

FINDSTR /I /M /S /C:" hat " *.* > hat.list

FINDSTR /I /M /S /B /C:"hat " *.* >> hat.list

FINDSTR /I /M /S /E /C:" hat" *.* >> hat.list

FINDSTR /I /M /S /C:" cat " *.* > cat.list

FINDSTR /I /M /S /B /C:"cat " *.* >> cat.list

FINDSTR /I /M /S /E /C:" cat" *.* >> cat.list

FINDSTR /I /M /S /C:" rat " *.* > rat.list

FINDSTR /I /M /S /B /C:"rat " *.* >> rat.list

FINDSTR /I /M /S /E /C:" rat" *.* >> rat.list

FINDSTR /I /M /S /C:" cat " /F:hat.list > cat-hat.list

FINDSTR /I /M /S /B /C:"cat " /F:hat.list >> cat-hat.list

FINDSTR /I /M /S /E /C:" cat" /F:hat.list >> cat-hat.list

FINDSTR /I /M /S /C:" rat " /F:hat.list > rat-hat.list

FINDSTR /I /M /S /B /C:"rat " /F:hat.list >> rat-hat.list

FINDSTR /I /M /S /E /C:" rat" /F:hat.list >> rat-hat.list

FINDSTR /I /M /S /C:" rat " /F:cat.list > rat-cat.list

FINDSTR /I /M /S /B /C:"rat " /F:cat.list >> rat-cat.list

FINDSTR /I /M /S /E /C:" rat" /F:cat.list >> rat-cat.list

FINDSTR /I /M /S /C:" hat " /F:rat-cat.list > hat-rat-cat.list

FINDSTR /I /M /S /B /C:"hat " /F:rat-cat.list >> hat-rat-cat.list

FINDSTR /I /M /S /E /C:" hat" /F:rat-cat.list >> hat-rat-cat.list

Within my test files, it still doesn't find all instances.

Link to comment
Share on other sites

I do not Know If This will Help But This VBS Script might be better For You To Use. I made this script to test for the word cat in a text file called Cat.txt , If the word was there it will add a confirm line in the text file.

I tested this in one folder with the file in there, I have included a if not so you dont get the standard error a vbs script makes when it cant find something.

I am not a scripter this is from a template on the MSDN site So If There Are Some People Who Have more Knowledge Of VBS Please Help Him Modify It To Suit His Needs.

My Brief Break Down

Red Is The File It Searches For

Blue Are The Objects needed For The script These are like the things cmd.exe uses for variuos task.

Green Is Now The Functions Of The Object made

Orange Is Set objFile to A New Name For The Script To Use It.

Purple If InStr(strWordList, strSearchWord) = 0 Then

These Are The Condition That must Be True For The Script To Work

Grey Is The Error If The File Is Missing

Const ForReading = 1 ' As The name Says

Const ForAppending = 8 'Adds To the File With Out Removing The Existing Entries

Const ForWriting = 2 'This Will Over Right The File With The New Entry

Sfile1 = "Cat.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")

If ObjFSo.FileExists(Sfile1) Then

Set objFile = objFSO.OpenTextFile(Sfile1, ForReading)

    strWordList = objFile.ReadAll

    objFile.Close

  strSearchWord = Vbcrlf & "Cat" & vbCrLf

  strSearchWord = Vbcrlf & "Hat" & vbCrLf

  strSearchWord = Vbcrlf & "Rat" & vbCrLf

   wscript.sleep 125

If InStr(strWordList, strSearchWord) = 0 Then

msgbox "The Search Word Found",0 +32,"Confirm Word Was There" 

Set objFile = objFSO.OpenTextFile(Sfile1, ForAppending)

    objFile.WriteLine Vbcrlf & "Confirm Word Cat Was There"

     objFile.Close

Else

If InStr(strWordList, strSearchWord) = 0 Then

msgbox "The Search Word Hat Was Found",0 +32,"Confirm Word Was There" 

Set objFile = objFSO.OpenTextFile(Sfile1, ForAppending)

     objFile.WriteLine Vbcrlf & "Confirm Word Hat Was There"

     objFile.Close

  End If

 

  If InStr(strWordList, strSearchWord) = 0 Then

msgbox "The Search Word Rat Was Found",0 +32,"Confirm Word Was There" 

Set objFile = objFSO.OpenTextFile(Sfile1, ForAppending)

     objFile.WriteLine Vbcrlf & "Confirm Word Rat Was There"

     objFile.Close

  End If

End If

End If  

If Not ObjFSo.FileExists(Sfile1) Then

MsgBox "The Cat.txt Is Missing"

  End If

Hope This Helps

Edited by gunsmokingman
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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...