1

I have a log file containing a stack trace split over a number of lines. I need to read this file into a batch file and remove all of the lines breaks.

As a first step, I tried this:

if exist "%log_dir%\Log.log" (
    for /F "tokens=*" %%a in ("%log_dir%\Log.log") do @echo %%a
)

My expectation was that this would echo out each line of the log file. I was then planning to concatenate these lines together and set that value in a variable.

However, this code doesn't do what I would expect. I have tried changing the value of the options for delims and tokens, but the only output I can get is the absolute path to the log file and nothing from the contents of this file.

How can I set a variable to be equal to the lines of text in a file with the line breaks removed?

2 Answers 2

1

If you want to use quotes for your filename in the FOR/F loop you need to add the usebackq option too, else you get a string not the content of your file.

for /F "usebackq delims=" %%a in ("%log_dir%\Log.log") do @echo %%a
Sign up to request clarification or add additional context in comments.

Comments

0

Or remove the quotes

if exist "%log_dir%\Log.log" (
    for /F "tokens=*" %%a in (%log_dir%\Log.log) do @echo %%a
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.