I am trying to run a script where I am trying to find a value from a file. File is :
8009 [main] INFO com.utilities.task.ICSTask - Submitted run of the tas
There will be only one taskId but I am getting this is multiple lines. I want to retrive the taskId which is same for a flow.
you have only one taskId, but you have two lines in the text file, which means, your loop is executed two times. And as taskid
isn't overwritten the second time, it still holds the value from the first time.
Change ... in ('type abc.txt') do ...
to ... in ('type abc.txt^|find "taskId="') do ...
to filter abc.txt
to the relevant line only.
Also, you do echo !taskID! >> def.txt
, which adds two trailing spaces. I slightly changed the redirection syntax to avoid that.
That changes your complete code to:
@echo off
SetLocal EnableDelayedExpansion
for /f "tokens=2 delims=:," %%i in ('type abc.txt^|find "taskId=') do (
@set%%i
REM To remove Space into Variable
Set "taskID=!taskID: =!"
>>def.txt echo !taskID!
)