1

I have a subroutine that runs in my batch file, during which I output to a textfile the success of each operation. An example is this...

set Tasks=One Two Three
set LogFile=Log.txt

for %%T in (%Tasks%) do call :Operation %%T

:Operation
set LogEntry=%1
echo %LogEntry%>> %LogFile%
goto :EOF

Using this I can get one, two and three written into the text file but I also get a final entry with an empty variable.

Can anyone see what the issue is?

1 Answer 1

4

:operation is just a label. When the for command ends its work, the batch file continues its execution, enters the code after the label and the code inside it gets executed, but this time without any passed parameter.

Place a goto :eof or a exit /b after the for command to avoid it

set Tasks=One Two Three
set LogFile=Log.txt

for %%T in (%Tasks%) do call :Operation %%T
goto :eof    

:Operation
set LogEntry=%1
echo %LogEntry%>> %LogFile%
goto :EOF
Sign up to request clarification or add additional context in comments.

1 Comment

Great help! Very well observed and noted by myself! Thanks :)

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.