Currently trying to see if a string, in this case the current line of a text file, contains a substring #. I am new to batch, so I am not sure exactly how I would do something like this. Here is the code
set substring = #
for /f "delims=," %%a in (Text.txt) do (
set string = %%a
//check substring method
echo %string%
)
echo %%a|find "substring" >nul
if errorlevel 1 (echo notfound) else (echo found)
Batch is sensitive to spaces in a SET statement. SET FLAG = N sets a variable named "FLAGSpace" to a value of "SpaceN"
The syntax SET "var=value" (where value may be empty) is used to ensure that any stray trailing spaces are NOT included in the value assigned. set /a can safely be used "quoteless".
As an alternative to find, you can use string substitution, like this:
@echo off
setlocal EnableDelayedExpansion
set "substring=#"
for /f "delims=," %%a in (Text.txt) do (
set "string=%%a"
if "!string:%substring%=!"=="!string!" (
rem string with substring removed equals the original string,
rem so it does not contain substring; therefore, output it:
echo(!string!
)
)
endlocal
This approach uses delayed environment variable expansion. Type setlocal /? in command prompt to find out how to enable it, and set /? to see how it works (read variables like !string! instead of %string%) and what it means. set /? also describes the string substitution syntax.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With