English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hello,

I would like to create a batch file that checks all text files in a directory for "John Smith" and once it found one echoes "found johns file"

2007-04-22 15:10:04 · 4 answers · asked by Netiad 2 in Computers & Internet Programming & Design

4 answers

If you're going to use a DOS batch file, then the "find" command is exactly what you need.

To do your search, this is what you need,

find /i "John Smith" *.txt

The "/i" switch tells the find command to ignore case, so you don't have to worry if the text is capitalized in different ways. If the search string is found, then it is automatically echo'ed to the string just below the filename that it was found in.

If you're running through a long directory, then there's a couple different ways to record what's been done so you can look at the results later.

One option, pipe the output to a file,

find /i "John Smith" *.txt >> myfiles.log

This will pipe the output of the find command into a "myfiles.log" file that you can look at later. It pipes the entire output which may or may not be what you want, depending on your purpose.

A more advanced option is to use the "for" command to take the output of the "dir" command and feed it into the "find" command, then do something with the results. Both of the following examples are on a single line.

== Display to the screen

for /f %i in ('dir *.txt /b') do (find /i "John Smith" %i && @echo "found johns file")

== Append to a file

for /f %i in ('dir *.txt /b') do (find /i "John Smith" %i && @echo %i>>myfiles.log)

2007-04-23 01:08:56 · answer #1 · answered by Kevin 7 · 3 0

Here is a way that you can do it using Excel or some other spreadsheet. Import the file into Excel. If you have one entry per text line, you will get one entry in the first cell of each row. Sort the file so that duplicate rows sort together. In column B, row 2, define the cell to be +IF(A2=A1), 1, 0) Duplicate that formula down all the rows of the spreadsheet. It will be 0 for the first item of each duplicate set. Save the page as a text file. The formulas go away and you save only the text and the 0's and 1's. Reopen the saved file in Excel and sort on column B. All the 0 items will be together. They are your unique items. Delete the other rows, clear column B, and save.

2016-05-21 03:59:21 · answer #2 · answered by ? 3 · 0 0

Again, this is very simple to do if you have the standard unix command line utilities installed (try cygwin if you're on windows)

grep 'John Smith' *.txt

It also has other options that control how it searches and what output it will produce (regular expression or fixed strings, entire lines or parts of lines, surrounding lines, file name/line number etc)

2007-04-22 16:57:51 · answer #3 · answered by undercoloteal 3 · 0 0

Maybe you should consider using a vb script file to accomplish this. This will allow you to utilize the file system object, which is great for this sort of thing. You can then execute the script via the batch file.

2007-04-22 15:21:54 · answer #4 · answered by tapeworm 2 · 0 1

fedest.com, questions and answers