0

I used this script:

rem @echo off
SETLOCAL enableDelayedExpansion
Set crs=
REM Get the result from sl.txt
FIND "LISTENER_SCAN1" <sl.txt >sl_temp_01.txt
FIND "running" <sl_temp_01.txt >sl_final.txt
DEL sl_temp_01.txt
for /F "tokens=8 delims= " %%j in (sl_final.txt) do (
IF %%J EQU "40" (SET crs=crs1)
IF %%J EQU "41" (SET crs=crs2)
IF %%J EQU "42" (SET crs=crs3)
IF %%J EQU "43" (SET crs=crs4)
Echo %%j
Echo !crs!
Pause
)

I can get echo %%j as 42 this time, but I cannot set my variable crs as crs3. Could you please help? Thanks,

0

1 Answer 1

2
  • If %%J already contained enclosing double quotes your if could be valid, if not this is never true.
  • To be sure use the ~ modifier to strip possible double quotes and set them yourself.

IF "%%~J" EQU "40" (SET crs=crs1)
  • Also %%J and %%j are distinct meta variables - in your batch you use them once in lowercase and then uppercase.
  • If variables are possibly empty put a different command separator behind the echo (= for example - not a space. This would return echo is on/off then.
  • There is no need for intermediate files, stack two find commands and parse the output with the for /f

:: Q:\Test\2018\06\27\SO_51067292.cmd
@echo off & SETLOCAL enableDelayedExpansion
Set "crs="

for /F "tokens=8 delims= " %%J in (
    'FIND "LISTENER_SCAN1" ^<sl.txt ^| FIND "running"'
) do (
    IF "%%~J" EQU "40" (SET crs=crs1)
    IF "%%~J" EQU "41" (SET crs=crs2)
    IF "%%~J" EQU "42" (SET crs=crs3)
    IF "%%~J" EQU "43" (SET crs=crs4)
    Echo=%%J
    Echo=!crs!
    Pause
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.