How to redirect all the results from multiple findstr commands to same file using batchfile - batch-file

Hi I am using findstr command to filter a file based on string and redirecting that output to seperate file.
findstr /C:"C=EODRevaluationProcess Temp table:TMP_EOD_REVAL_DATA" "C:\Users\Desktop\abc.txt" > C:\Users\Desktop\xyz.txt
pause
findstr /C:"sendSPPJMSMessage><Finished publishing [0] messages" "C:\Users\Desktop\abc.txt" > "C:\Users\Desktop\xyz.txt"
pause
findstr /C:"POS_HOURLY_DATA : actualTable : POS_HOURLY_DATA_" "C:\Users\Desktop\abc.txt" > "C:\Users\Desktop\xyz.txt"
I want all the filtered lines to be saved in same file xyz.
But only output of first findstr command is saving in xyz.
Can anyone please suggest any idea
Thank you.

You are redirecting the output to a file using the > operator which will create the file if it doesn't exist and replace its contents with the output.
If you want to append to the file, rather than replacing its contents, you can use the >> operator.

There's no reason to run FindStr three times against the same file. Putting all the search strings in a single command would also prevent your overwrite issue.
#FindStr /I^
/C:"some string"^
/C:"another string"^
/C:"and a bigger string"^
"source.txt" > "output.txt"
You'll note that I have used carets, ^, to escape the line returns so that your strings are easier to maintain, (each to their own line)

Related

Batch Script - findstr overwrite csv file with result

I am using batch scripting.
I want to filter out the rows containing a specific string in my sss_output.csv file and overwrite that same file with the result.
Below, I have the following code:
#echo
cd C:\apache-jmeter-3.2\bin
jmeter.bat -n -t C:\apache-jmeter-3.2\bin\SessionManagement.jmx -l C:\apache-jmeter-3.2\bin\sss_output.csv
findstr /c:"WebDriver Sampler" sss_output.csv > sss_output.csv
After running executing this script, the jmeter.bat statement gets executed and gets the expected results. However, the second line fails to filter out the rows containing the string "WebDriver Sampler".
If I run the jmeter.bat and findstr line separately in the command prompt, it works fine. What am I doing wrong?
Redirection is handled before commands are executed, so sss_output.csv is emptied before findstr even starts.
Write the output to a temporary file, then move this onto the target file afterwards:
findstr /C:"WebDriver Sampler" > "%TEMP%\sss_output.tmp"
move /Y "%TEMP%\sss_output.tmp" "sss_output.csv" > nul

batch command that will loop through files ending in “.txt” and display file contents

I'm pretty sure I am just missing something small, but I am trying to figure out how to loop through the contents of a directory, identify those with a .txt extension, and display the contents of the .txt files to the display.
I searched for an answer to this but couldn't find it specifically. I found this one that shows how to find all the .txt and use echo to display the file name.
I also found this one that shows how to use Type to display the file contents, and this one that combines them sort of. But I keep getting errors when I try different variations on using type in a for loop.
I've tried:
for %%i in (*.txt) do type %%i
and
for %%i in (*.txt) do (
type %%i
echo.
)
But both of those are giving errors. I'm sure it's something simple I am missing.
Update:
The problem was spaces in the filenames. Adding quotes around the %%i following Type fixed it.
No need for any FOR loop. type *.txt will do the trick. Each file name will be printed to stderr, followed by the content on stdout. By default, both stderr and stdout appear on the console. There will be one or two line gaps between each file name and file content.
If you don't want to see the file names, then type *.txt 2>nul. But then there is no gap between each file.

Output from FINDSTR into a string in batch command

I would be really grateful for anyone that could save me some time on this.
I have a Windows batch file that I am using to match two different file types with different extensions and write the results out to a file.
It reads through a table of filenames (called matched_filenames.txt) and, for each line in it, tries to locate the filename in two other tables.
I have this code (which works to an extent).
for /f "tokens=*" %%z in (%Ourhome%\matched_filenames.txt) do (
rem -------
rem ------- now (for each line), find the photo name in both the list of raw and list of jpg files
rem -------
findstr /C:"%%z" raw_photos_directory.txt >>located_matches.txt
findstr /C:"%%z" jpg_photos_directory.txt >>located_matches.txt
)
But it writes separate lines out to the located_matches.txt file.
I would really like to assign the results of the findstr's to variables so that I can then concatenate them and then write the concatenated line as a single line to the file.
Can anyone recommend a more elegant way of doing this?

avoid overwriting the same text file in batch script for loop

I'm really new at writing batch script, but I'm trying to find all the occurrences of a string in multiple files and write these strings to a txt file by using a for loop. Here's what I have:
for %%i in (a.log b.log c.log) do
(
find /I "error" %%i > check.txt
)
But somehow, check.txt only contains the strings from c.log and not from the previous two, probably overwrote.
Is there a way to avoid overwriting?
Thanks!!
> creates a file anew.
>> appends to an existing file or creates it if it doesn't already exist.

write results into a text file

I created a batch that will use windows FINDSTR to search for my selective input.
I am trying to log my results of my search term in a text file called results.txt
So I do have something like this so results are kept not overwritten:
>>Results.txt
I've created the txt file so it'll write to it, this is what I tried and won't work:
findstr "\<%X%\>" *.txt
echo >>results.txt
That is what I have for trying to log my results of my search term, however nothing happens.
And when I have findstr "\<%X%>" *.txt >>results.txt
It tries to search for >>results.txt and it's not cooperating.
Anyone know what to do?
I'm doing this because FINDSTR will work in the cmd prompt but if I get too many results it cuts off the top, so I want it to write all the results into the results.txt so I can view the whole results with nothing cut off.
Thanks for the help =)
Try using /c:
findstr /c:"<%X%>" *.txt >> results.txt
Edit: didn't need the ^ escaping here.
Have you tried putting a space in between >> and results.txt
There is an example of it here
The position of the redirection clause shouldn't matter, so I'd give this a shot:
>>results.txt findstr "\<%X%\>" *.txt
I've also put a \ before the > because I'm assuming you want start and end word markers rather than a literal >. If I'm mistaken, just take it out again.
But I have to mention that, if all you want to do is stop the voluminous output from scrolling off the top of the console, you could just pipe the whole thing through more:
type honkin_big_file | more
will act as a pager so you can view it bit by bit.
For your single line example, you have this:
findstr "\<%X%>" *.txt >>results.txt
Did you mean this:
findstr "\<%X%\>" *.txt >>results.txt
The difference being in the first example it is searching for any occurrence of "%X%>" on a beginning word boundary and the second example is searching for "%X%" on a beginning word boundary with an end word boundary (e.g. single word " %X% ").
It works for me.

Resources