How do you make a subroutine for findstr and make the string as an argument for a loop - batch-file

I Have a batch file that I made myself that search for strings inside a text file but for the command I use its to time consuming to make a lot of commands for a lot of strings.
So basically I am making a tool that finds strings from a certain text that the user puts in himself the text file I sent the users text file as a variable that is called %Link% in my batch file and search using this type of command.
>nul findstr /c:"string" %link% && (
echo - Name of string is then string
echo.
)
I have strings like this string:::name
then I can get them from that.
Is there any way to get something like that or any examples of a subroutine for findstr and make the string as an argument for a loop.

Related

Unable to check if a string contains a specific string in a for loop of a text file

I have a batch script that reads line by line a text file (ProcessedFiles.txt). The text has many lines that represent filenames of a directory. They can be either JSON or HTML files. I would like to check if the string "json" exists in that line. I have already searched and tried a few answers like
if not x%%a:json=%==x%%a% echo It contains JSON
or even extracting the last 4 characters from the %%a
SET var=%%a:~-4%
but I must be doing something really wrong here because I cannot get it to work.
I apologize for my very limited knowledge on batch script.
Basically I want to know if each line of the text file I am reading contains the string "json". This is my for loop code i am trying to correct
for /F "tokens=*" %%a in (%jsonDir%ProcessedFiles.txt) do (
echo Processing file: %%a
SET var=%%a:~-4%
)
The echo Processing file: %%a displays the line correctly and this is what I am aiming to search if it contains the string "json".
For example, if text file ProcessedFiles.txt contains the lines
ABCD.json
BCDF.json
ABDR.html
CVBF.json etc.
when the loop reads the line ABCD.json or BCDF.json or CVBF.json then I need to know that it has encountered a JSON file, but for line ABDR.html it is not a JSON file.
Thank you in advance

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

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)

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.

Search and Edit Text Files w/ Batch

I'm fairly new with batch scripting, and currently trying to make my work easier (which will please my boss) with a script. Is there a way for a batch file to search a specific given input inside a text file, and echo into the text file?
For example, the text file looks like this:
4/11/2016
01530594
Time In: 1:30
01565832
Time In: 2:32
The given string is, in this example, 01530594 and I need to search for it inside the text file, and record the time out to make it look like this:
4/11/2016
01530594
Time In: 1:30
Time Out: 3:21
01565832
Time In:
If other stuff is needed to make it easier (say, already have Time Out: from the beginning, without a time) is required, that's more than acceptable.
I know search/replace/edit stuff with a batch file is tedious, is this possible?
set "timevalue=3:21"
(
for /f "delims=" %%i in (infile.txt) do (
echo %%i
echo %%i|find "Time In:" >nul && echo Time Out: %timevalue%
)
)>outfile.txt
The for loop reads the file line by line. The first echo writes the line to the outfile.
The second echo line is a bit more complicated:
echo %%i|find "Time In:" looks, if the line contains the string "Time In:". >nul writes its output to nirvana (we don't need it, we only want to know if it is there).
&& means "execute the next command, if the previous command was successful (that was find). If so, write an additional line Time Out: %timevalue%
Put the whole for loop into a block (between ( and ) and redirect it's output to a file

Tell Batch 'find' string has to end with newline

I'm currently trying to determine if a string is in a certain file using batch find. Therefore I'm using the following code:
>nul find "stringToSearch" file.txt && (
REM String exists.
) || (
REM String does not exist.
)
The structure of my file.txt can be seen as the following:
randomString
randomString2
stringToSearch
randomString3
stringToSearch_additional
The Problem here is that find will always return true when stringToSearch_additional is in there, even when just stringToSearch is not. So is there any way to search for stringToSearch only, ending the line right after the last letter? I also cannot check for a whole line because there might be some other words in front of every line (that I don't have control over).
Thanks in advance.
Use findstr with the /e /b switches or regular expressions.

Resources