Jump to content

Batch File!


Sh4dow

Recommended Posts


The problem I see with the FINDSTR script is the /F command only looks at the file name, not WITHIN the actual file. I need to search for the word " cat " in the files that are in the hat-rat.list. This is getting way more complicated than I originally planned. I do not think that I will be able to complete this task without a lot of help.

I also found that if you run the first 9 lines seperately then do not give a correct ouput.

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

The cat list is the only one that gives true results. The other lines are exactly the same but they do not. I'm so confused!!

Link to comment
Share on other sites

The problem I see with the FINDSTR script is the /F command only looks at the file name, not WITHIN the actual file.
No it doesn't, it gets the list of files to be searched from a specified file, (in this case hat.list, or whatever.).

Anyway back to your problem, I have used regular expressions in the following examples. Basically they mean, e.g. Search for any word beginning with, (\<), xyz and ending with, (\>), xyz.

  • "\<xyz\>"

Try these lines, obviously changing the search directory C:\WINDOWS\ to suit!

FINDSTR /SIM "\<hat\>" "C:\WINDOWS\*.*" >hat.list
FINDSTR /SIM "\<cat\>" "C:\WINDOWS\*.*" >cat.list
FINDSTR /SIM "\<rat\>" "C:\WINDOWS\*.*" >rat.list
FINDSTR /IM "\<cat\>" /F:"hat.list" >hat-cat.list
FINDSTR /IM "\<rat\>" /F:"hat.list" >hat-rat.list
FINDSTR /IM "\<rat\>" /F:"cat.list" >cat-rat.list
FINDSTR /IM "\<rat\>" /F:"hat-cat.list" >hat-cat-rat.list

And to give the desired result in accordance with your original question you could make do with just this

FINDSTR /SIM "\<hat\>" "C:\WINDOWS\*.*" >hat.list
FINDSTR /SIM "\<cat\>" "C:\WINDOWS\*.*" >cat.list
FINDSTR /IM "\<cat\>" /F:"hat.list" >hat-cat.list
FINDSTR /IM "\<rat\>" /F:"hat.list" >hat-rat.list
FINDSTR /IM "\<rat\>" /F:"cat.list" >cat-rat.list

Edited by Yzöwl
Link to comment
Share on other sites

The script works perfectly for finding cat, hat, and rat. I just need to come up with a way to find cat in the hat list, rat in the hat list, etc..

Would it correct the problem if we changed the output of the files to another directory, then ran the search again within that directory?

For ex)

FINDSTR /SIM "\<hat\>" "*.*" >"C:\My Documents\Results\hat.list"

FINDSTR /SIM "\<cat\>" "*.*" >"c:\My Documents\Results\cat.list"

FINDSTR /SIM "\<rat\>" "*.*" >"c:\My Documents\Results\rat.list"

Now I have a list of cat, hat, rat. But they are just the file names! So if I searched for cat in hat.list I wouldn't find anything because hat.list just contains the names of the files! :(

Link to comment
Share on other sites

I keep on telling you, it is searching through all the files from the list, hat.list, for the string cat, not looking for the string cat in the file hat.list!

Are you performing the task by adding all the lines together in a batch if so don't, there is a good chance that the findstr command on the hat.list will start before the hat.list is finished. Just try each of the following lines one by one directly into a cmd window, (waiting for the output files to finish before the next line is entered)

FINDSTR /SIM "\<hat\>" "C:\WINDOWS\*.*" >%TEMP%\hat.list
FINDSTR /SIM "\<cat\>" "C:\WINDOWS\*.*" >%TEMP%\cat.list
FINDSTR /SIM "\<rat\>" "C:\WINDOWS\*.*" >%TEMP%\rat.list
FINDSTR /IM "\<cat\>" /F:"%TEMP%\hat.list" >%TEMP%\hat-cat.list
FINDSTR /IM "\<rat\>" /F:"%TEMP%\hat.list" >%TEMP%\hat-rat.list
FINDSTR /IM "\<rat\>" /F:"%TEMP%\cat.list" >%TEMP%\cat-rat.list
FINDSTR /IM "\<rat\>" /F:"%TEMP%\hat-cat.list" >%TEMP%\hat-cat-rat.list

You should see that it does work!

Edited by Yzöwl
Link to comment
Share on other sites

C:\>FINDSTR /SIM "\<hat\>" "*.*" >hat.list

FINDSTR: Cannot open Documents and Settings\LocalService\Local Settings\Application Data\Microsoft\Windows\UsrClass.dat

FINDSTR: Cannot open Documents and Settings\LocalService\Local Settings\Application Data\Microsoft\Windows\UsrClass.dat.LOG

FINDSTR: Cannot open Documents and Settings\LocalService\NTUSER.DAT

FINDSTR: Cannot open Documents and Settings\LocalService\ntuser.dat.LOG

FINDSTR: Cannot open Documents and Settings\NetworkService\Local Settings\Application Data\Microsoft\Windows\UsrClass.dat

FINDSTR: Cannot open Documents and Settings\NetworkService\Local Settings\Application Data\Microsoft\Windows\UsrClass.dat.LOG

FINDSTR: Cannot open Documents and Settings\NetworkService\NTUSER.DAT

FINDSTR: Cannot open Documents and Settings\NetworkService\ntuser.dat.LOG

can I ignore these?

Link to comment
Share on other sites

FINDSTR /SIM "\<hat\>" "C:\WINDOWS\*.*" >%TEMP%\hat.list

FINDSTR /SIM "\<cat\>" "C:\WINDOWS\*.*" >%TEMP%\cat.list

FINDSTR /SIM "\<rat\>" "C:\WINDOWS\*.*" >%TEMP%\rat.list

FINDSTR /IM "\<cat\>" /F:"%TEMP%\hat.list" >%TEMP%\hat-cat.list

FINDSTR /IM "\<rat\>" /F:"%TEMP%\hat.list" >%TEMP%\hat-rat.list

FINDSTR /IM "\<rat\>" /F:"%TEMP%\cat.list" >%TEMP%\cat-rat.list

FINDSTR /IM "\<rat\>" /F:"%TEMP%\hat-cat.list" >%TEMP%\hat-cat-rat.list

I tried it in cmd and it works.

Lines 4-7 give no output.

I just want to find cat, hat, rat within one folder. Then find any of the files that have 2 of those words in it. Feel free to write a VB script, I don't care, I just want it to work!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Link to comment
Share on other sites

Solution:
findstr /R /M "hat.*cat hat.*rat cat.*hat cat.*rat rat.*hat rat.*cat" *.txt

Nice try, but it gives false results and will only work if the search strings are on the same line.

It would pick up father as fat, (which mine isn't), polecat as cat (which they aren't, although they do eat catfood) and hatchet as hat, (and I'm not wearing one on my head).

I have added a few additional test files below to show you, what I mean

findstr2.zip

Link to comment
Share on other sites

Yes soulin, the tidiest way to do it, (for a match all strings), would be to take the search strings, write them to a temp file and use that file with the /G switch in a FOR statement. Then you would CALL another FOR statement with that as the input. It does become more tricky in a any two from three permutation, but that would be the way forward.

I have a fully working solution, using a single FOR statement utilising similar code as originally suggested by purewaveform. The code although not polished was actually posted in this thread for the entire weekend, but I decided to remove it, as the idea was to allow Sh4dow and other interested parties here to learn something. Although I do feel a little mean about doing it!

A problem with writing the script is not that it's hard, it is just that the poster hasn't really told us what it is for? Noone needs to search for those three words in a text file, he's not even checking txt files in his code. If we were to know the layout of the files he's searching and their contents, and what was really being searched, it would possibly make the effort worthwhile. They will most probably be logs of some sort, and possibly be comma, tab or line delimited, tokens and delimiters in the code will very probably save a lot of time and speed up the search process in too.

To write what is required would really be better suited to a dedicated scripting forum as opposed to a general Windows XP group, and to be honest with you after yesterdays statement to 'just write one for me!' I'm kind of glad I took down, the solution I had posted.

Link to comment
Share on other sites

2Yzowl: Ah, I see... But we could try to kick them to right direction, right? :)

Ok, guys, you can try to do this using few lines of code, we will JUST guide you, ok? :)

This is little bit CHANGED and UNCOMPLETE part of code:

<CODE>

Set strSearchFor=test.txt

for /f "usebackq " %%i IN (`dir /b`) DO (

Set strCount=0

for /f "usebackq delims=; tokens=1-4" %%a IN (`type %strSearchFor%`) DO (

find /i "%%a" %%i

If %%errorlevel%% EQU 0 Call :Count

echo %strCount%

)

pause

)

:Count

echo %strCount%

Set /a strCount=%strCount% + 1

echo %strCount%

</CODE>

You must find what is wrong AND add something to count subprocedure...

Lets make this more interesting :)

Link to comment
Share on other sites

The problem is this, I cannot give the words that I am searching for. I am looking for these words in any file within 300GB of data. I do not want someone to "write one for me." I have been using every possible combination of switches from this page to attempt to do this myself.

http://www.fsref.com/pr/dosxp.shtml#findstr

This stuff isn't easy, as I mentioned before. I have never scripted anything. So if I seem frustrated it's only because I have been attempting to do this for 2 weeks and I have not been able to come up with a solution. I feel pretty stupid that you guys can write this script in like 5 minutes yet I've been trying for 2 weeks and can't even figure out a FOR loop.

I'm posting here because I need help, and I appreciate everything that you have posted. My boss is on my a** everyday, and I'm working to try to find a solution.

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