MSFN Forum: Batch File! - MSFN Forum

Jump to content



Unattended CD/DVD Guide Homepage · MSFN Forum Rules

If you have questions about customizing Windows XP that are nLite-specific, please post them in the nLite forum, not here. If you have questions regarding the unattended installation of Windows XP, please post them in the Unattended Windows 2000/XP/2003 section.
  • 3 Pages +
  • 1
  • 2
  • 3
  • You cannot start a new topic
  • You cannot reply to this topic

Batch File! Scripting Rate Topic: -----

#1 User is offline   Sh4dow 

  • Newbie
  • Group: Members
  • Posts: 45
  • Joined: 30-March 05

  Posted 12 April 2005 - 09:08 AM

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!


#2 User is offline   purewaveform 

  • Member
  • PipPip
  • Group: Members
  • Posts: 142
  • Joined: 24-March 05

Posted 12 April 2005 - 10:32 AM

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.

#3 User is offline   Sh4dow 

  • Newbie
  • Group: Members
  • Posts: 45
  • Joined: 30-March 05

Posted 12 April 2005 - 11:17 AM

Thank you for your assistance. I will get to work on it

#4 User is offline   suryad 

  • MSFN Expert
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 1,216
  • Joined: 17-February 05

Posted 12 April 2005 - 12:31 PM

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.

#5 User is offline   DiGGiTY 

  • Member
  • PipPip
  • Group: Members
  • Posts: 159
  • Joined: 04-April 05

Posted 12 April 2005 - 12:55 PM

I can add content to the forum so you're not saddled with doing it all yourself...

#6 User is offline   Sh4dow 

  • Newbie
  • Group: Members
  • Posts: 45
  • Joined: 30-March 05

  Posted 12 April 2005 - 01:05 PM

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

???????? :}

#7 User is offline   purewaveform 

  • Member
  • PipPip
  • Group: Members
  • Posts: 142
  • Joined: 24-March 05

Posted 12 April 2005 - 02:09 PM

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.

Quote

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!

#8 User is offline   gunsmokingman 

  • MSFN Addict
  • Group: Super Moderator
  • Posts: 1,991
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 12 April 2005 - 02:11 PM

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

Quote

 
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

This post has been edited by gunsmokingman: 12 April 2005 - 02:22 PM


#9 User is offline   purewaveform 

  • Member
  • PipPip
  • Group: Members
  • Posts: 142
  • Joined: 24-March 05

Posted 12 April 2005 - 02:18 PM

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.

#10 User is offline   suryad 

  • MSFN Expert
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 1,216
  • Joined: 17-February 05

Posted 12 April 2005 - 11:12 PM

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

#11 User is offline   purewaveform 

  • Member
  • PipPip
  • Group: Members
  • Posts: 142
  • Joined: 24-March 05

Posted 13 April 2005 - 12:44 AM

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.

#12 User is offline   Sh4dow 

  • Newbie
  • Group: Members
  • Posts: 45
  • Joined: 30-March 05

Posted 13 April 2005 - 07:34 AM

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

#13 User is offline   purewaveform 

  • Member
  • PipPip
  • Group: Members
  • Posts: 142
  • Joined: 24-March 05

Posted 13 April 2005 - 09:17 AM

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.

#14 User is offline   Sh4dow 

  • Newbie
  • Group: Members
  • Posts: 45
  • Joined: 30-March 05

Posted 13 April 2005 - 12:22 PM

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?

#15 User is offline   purewaveform 

  • Member
  • PipPip
  • Group: Members
  • Posts: 142
  • Joined: 24-March 05

Posted 13 April 2005 - 12:46 PM

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.

#16 User is offline   Sh4dow 

  • Newbie
  • Group: Members
  • Posts: 45
  • Joined: 30-March 05

Posted 13 April 2005 - 01:14 PM

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

??

#17 User is offline   purewaveform 

  • Member
  • PipPip
  • Group: Members
  • Posts: 142
  • Joined: 24-March 05

Posted 13 April 2005 - 02:09 PM

Quote

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

should be
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!

#18 User is offline   Sh4dow 

  • Newbie
  • Group: Members
  • Posts: 45
  • Joined: 30-March 05

Posted 13 April 2005 - 03:09 PM

Purewaveform, not only did you greatly assist me, but I've learned something new. Thank you very much for all of your time.

#19 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,120
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 13 April 2005 - 03:17 PM

purewaveform, on Apr 13 2005, 09:09 PM, said:

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?

#20 User is offline   purewaveform 

  • Member
  • PipPip
  • Group: Members
  • Posts: 142
  • Joined: 24-March 05

Posted 13 April 2005 - 03:50 PM

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.

Share this topic:


  • 3 Pages +
  • 1
  • 2
  • 3
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy