Tell Batch 'find' string has to end with newline - batch-file

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.

Related

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

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.

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)

Stripping part of a filename and using it for a comparison

I'm building a script for Windows command line in which I try to check some filenames in a FOR loop, and then stripping off part of the filename into a variable for further use. Basically, what I want to happen is this:
List all files in a certain directory, splitting of the extension like .osm.pbf in this case.
Assign the filename to a variable.
Out the last 7 characters of the filename in another variable.
Compare this new variable to "-latest".
If the compare is true, cut a part of the variable containing the filename.
If the compare is false, take over the complete variable into another variable.
Through some trial and error and some searching online, I've arrived at this point (which still isn't doing what I want):
FOR /F "tokens=1-2 delims=." %%M IN ('DIR /b %VECTOR_WORKDIR%\*.osm.pbf') DO (
SET VECTOR_CURRENT_MAP2=%%M
ECHO !VECTOR_CURRENT_MAP2! >> %VECTOR_LOGFILE%
SET LAST_BIT_TEMP=!VECTOR_CURRENT_MAP2:~-7!
ECHO !LAST_BIT_TEMP! >> %VECTOR_LOGFILE%
SET LAST_BIT=!LAST_BIT_TEMP: =!
ECHO !LAST_BIT! >> %VECTOR_LOGFILE%
IF !LAST_BIT!=="-latest" (
SET VECTOR_CURRENT_MAP3=!VECTOR_CURRENT_MAP2:~0,-8!
ELSE
SET VECTOR_CURRENT_MAP3=!VECTOR_CURRENT_MAP2!
)
ECHO !VECTOR_CURRENT_MAP3! >> %VECTOR_LOGFILE%
)
This results in these lines in the log file, for the file basse-normandie-latest.osm.pbf:
basse-normandie-latest
-latest
-latest
ECHO is on.
The first echo is correct, although the filename has a trailing space. (So actually it's "basse-normandie-latest ".)
The second echo doesn't seem to take this training space into account, as it correctly gives "-latest" as the last 7 characters. This echo also has a trailing space (So actually it's "-latest ".)
The third echo is an attempt to clear the spaces from the variable (by using ": ="), but this results in another trailing space. (So actually it's "latest ".)
The final echo after the IF statement (where I try to cut the "-latest" part from the filename), results in "ECHO is on".
I have SETLOCAL enabledelayedexpansion enableextensions declared at the top of my script.
Any thoughts on how to make this work, i.e. get rid of the trailing spaces to make the comparison work?
Thanks in advance for any pointers in the right direction!
A line like
ECHO !VECTOR_CURRENT_MAP2! >> %VECTOR_LOGFILE%
results in appending the value of the environment variable VECTOR_CURRENT_MAP2 to file with file name stored in environment variable VECTOR_LOGFILE with a trailing space because there is a space before redirection operator >> which is interpreted by Windows command processor as part of the string to output by command ECHO. This space must be removed to get the file name redirected into the log file without a trailing space.
In general it is critical on redirecting a variable string into a file without a space between the variable string and the redirection operator in case of the variable string ends with a space and a number being a valid handle number like  1 or  2 or  3. There are several solutions to workaround this problem like specifying the redirection left to command ECHO, i.e.
>>%VECTOR_LOGFILE% ECHO !VECTOR_CURRENT_MAP2!
But on using delayed expansion as simply necessary here, it is safe to append the redirection at end without a space between exclamation mark and >>, i.e.
ECHO !VECTOR_CURRENT_MAP2!>> %VECTOR_LOGFILE%
The space after redirection operator is ignored by Windows command processor and therefore can be kept although many batch file programmers (like me) with good syntax highlighting don't insert a space after a redirection operator.
On comparing strings with command IF and enclosing one string in double quotes which is always a good idea, it must be made sure that the other string is also enclosed in double quotes. The command IF does not remove the double quotes before comparing the strings. The double quotes are parts of the compared strings.
The condition
IF !LAST_BIT!=="-latest"
is only true if the string assigned to environment variable LAST_BIT would be with surrounding quotes which is never the case with your batch code and therefore the condition is never true.
Correct would be:
IF "!LAST_BIT!"=="-latest"
There is no need to use command DIR to search for files with a pattern in a directory as command FOR is designed for doing exactly this task. Processing of output of command DIR is an extension of FOR available only if command extensions are enabled as by default.
The file extension is defined by Microsoft as everything after last dot in name of a file. Therefore the file extension for your files is pbf respectively .pbf and .osm belongs to the file name.
Command FOR offers several modifiers to get specific parts of a file or directory name. Those modifiers are explained in help output into console window on running in a command prompt window for /?. Help of command CALL output with call /? explains the same for processing parameters of a batch file or subroutine (batch file embedded within a batch file).
Your code with all mistakes removed:
FOR %%M IN (*.osm.pbf) DO (
SET "VECTOR_CURRENT_MAP2=%%~nM"
SET "VECTOR_CURRENT_MAP2=!VECTOR_CURRENT_MAP2:~0,-4!"
ECHO !VECTOR_CURRENT_MAP2!>>%VECTOR_LOGFILE%
SET "LAST7CHARS=!VECTOR_CURRENT_MAP2:~-7!"
ECHO !LAST7CHARS!>>%VECTOR_LOGFILE%
IF "!LAST7CHARS!" == "-latest" (
SET "VECTOR_CURRENT_MAP3=!VECTOR_CURRENT_MAP2:~0,-7!"
) ELSE (
SET "VECTOR_CURRENT_MAP3=!VECTOR_CURRENT_MAP2!"
)
ECHO !VECTOR_CURRENT_MAP3!>>%VECTOR_LOGFILE%
)
Easier would be using this code with using string substitution feature of command SET, i.e. search within a string case-insensitive for all occurrences of a string and replace them with another string which can be also an empty string.
FOR %%M IN (*.osm.pbf) DO (
SET "VECTOR_CURRENT_MAP2=%%~nM"
SET "VECTOR_CURRENT_MAP2=!VECTOR_CURRENT_MAP2:~0,-4!"
ECHO !VECTOR_CURRENT_MAP2!>>%VECTOR_LOGFILE%
SET "VECTOR_CURRENT_MAP3=!VECTOR_CURRENT_MAP2:-latest=!"
ECHO !VECTOR_CURRENT_MAP3!>>%VECTOR_LOGFILE%
)
%%~nM is replaced on execution by Windows command processor by the name of the file without drive, path and file extension resulting for your example in basse-normandie-latest.osm.
The unwanted file name part .osm is removed with the next line in both batch code blocks which chops the last 4 characters from the file name string.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
for /?
if /?
set /?
Read the answer on question Why is no string output with 'echo %var%' after using 'set var = text' on command line? for an explanation why I used set "variable=value" on every line which assigns a value string to an environment variable because trailing whitespaces are critical for your task.

Checking for specific character in a batch file

I want to check a string in a batch file to make sure it doesn't include any of the following characters: /:*?"<>|
I don't know whether it would be easier to check to see if it contains valid characters, as the batch file might fail with there being the above characters in the code.
This is one way:
double quotes are difficult to handle in batch and I didn't add it.
If a double quotes exists in the string variable then this can fail too.
#echo off
set "string=1234abcd"
for /f "delims=/:*?<>|" %%a in ("%string%") do if "%%a"=="%string%" echo characters were not found
pause

Find String in log files, and copy whole line into a new file

Good Day All.
I have a bunch of log files that contains a certain folder in a folder path, that is "/input", that is part of a pathname that varies - "/input" being the only constant here...
How do you scan all the log files (*.log), look for all sub-string instances of "/input" and write the whole line, containing this part of the path, that is "/input", to a new text file?
Example of one line in a log file:
2014-01-16 00:33:57 10.0.1.169 ddca 192.168.34.11 21 CWD /DDAA/Input/ 250 0 0
How do I write all the lines, containing this part "/input" to a new text file?
Thanks.
#echo off
setlocal enableextensions
set "source=c:\where\the\log\files\are"
set "target=c:\output\folder\newFile.log"
pushd "%source%"
(for /f "tokens=1,* delims=:" %%a in ('findstr /i /l /c:"/input/ " "*.log"') do (
echo(%%b
)) > "%target%"
popd
Search for the string using findstr. It will return a string containing the file name where the string was found, followed by a colon and the full line in the log file. Then, using for command this line is splitted using the colon as a delimiter and the right part of the line (the original line in log file) is echoed. The output of the for command is then redirected to the final file.
pushd and popd are used to ensure that references to the log files, and the names in the output of the findstr command, does not contain aditional colons that interfere in the splitting part.
findstr /i /c:"/input" *.log >output.txt
The /i switch makes the search case insensitive. The /c: switch is used so that the leading / is not treated as a switch indicator.
Because a wildcard was used, it will prefix each line with the name of the file, followed by a colon, like in the following
filename.log:2014-01-16 00:33:57 10.0.1.169 ddca 192.168.34.11 21 CWD /DDAA/Input/ 250 0 0
Lots of options. You can create a small tool that does this for you, but my guess is somebody somewhere already done this. Did you google for something like this?
If you are up to some manual work, I know Notepad++ has the functionality to search through txt-files in folder(s) matching a (sub)string.
Also relevant:
https://superuser.com/questions/110350/how-to-use-notepad-to-find-files-in-a-directory-that-do-not-contain-string

Resources