0
votes
@ECHO off
setlocal EnableDelayedExpansion
set "VAR=da da da YES123123"
echo %VAR% > testing.txt

FOR /F %%a in ('findstr "YES" .\testing.txt') do (
                                                 set BLAH=%%a
                                                 set "BLAH2=%BLAH: =%"
                                                 set "FINAL=%BLAH2:~15%"
                                                 echo %FINAL%
                                                 )

endlocal

Whether WITH or WITHOUT "setlocal EnableDelayedExpansion" the batch file simply does not work.

But that's all I have so far. However I also want to strip the preceding characters from the FINDSTR string, but set a variable to the FINDSTR string AND IT'S SUCCEEDING 123123 characters. Unfortunately, it doesn't work. It only outputs "ECHO is off."

I have used FOR correctly in other ways, but I can't figure it out this time. Any help would be greatly appreciated.

3
You need to invoke delayedexpansion [hundreds of SO articles about that - use the search feature] in order to display or use the run-time value of any string variable that's changed within a parenthesised series of instructions (aka "code block").Magoo
Search for delayed expansion on this site; alternatively, do the sub-string extraction after the loop as your file contains only a single line anyway...aschipfl
yeah I've tried it with setlocal EnableDelayedExpansion already. It's the same result, even if I change out the % to the ! (except for the cookie)Tomcat Cruiser
What do you want to get back? YES123123? Is it always the last "word"?Stephan

3 Answers

1
votes

to get the last word, use a plain for to split your string by default delimiters (space, tab, comma, =). No need to know, how many tokens there are. The following works, even if there are more lines with YES:

for /f "delims=" %%a in ('findstr "YES" .\testing.txt') do (
  for %%b in (%%a) do set final=%%b
  echo !final!
)

EDIT to find YES until end of line:

There is a token * for "all the rest", so you could do:

for /f "tokens=3,* delims= " %%a in ('findstr "YES" .\testing.txt') do echo %%b

but I recommend another method (replacing *YES with just YES). set can do limited wildcard replacement (*string works, but string* does not). Advantage: you don't need to know, which token YES... is:

for /f "delims=" %%a in ('findstr "YES" testing.txt') do (
  set line=%%a
  echo !line:* YES=YES!
)
3
votes

Understand first meaning
Tokens mean take only the column 1 or 2 or ...... of stdout

skip mean skip line 1,2........... from the list of text of stdout

delims mean the way this columns seperated between each other with what (space or dot or slash ..........)

0
votes

Ok so i figured it out. Had to use the "token=1,2,3,4, delim= " thing, which I hate using and it's confusing but have used it before for other purposes. I was just really confused on how to echo or set the variable to the token at the end of the command. Here is the corrected code.

FOR /F "tokens=1,2,3,4 delims= " %%a in ('findstr "YES" .\testing.txt') do echo %%d