0

I have this code that reads the content of a file and set all the contents in a variable.

for /f "tokens=1,*" %%h in ('type C:\user\userfiles\title.txt') do (
set title=%%h
)echo %title%

Example contents of title.txt

AAAA BBBB CCCC

Now, It always display the word before the first space. How to display all contents of a file in variable title?

1 Answer 1

2

You need to specify that there should be no delimiters in the FOR parameters, otherwise a space will be used and only pick up the first value:

REM Pick up a single token with no delimiters.
REM This will read the entire line of the file.
for /f "usebackq tokens=* delims=" %%h in (`type "C:\user\userfiles\title.txt"`) do set title=%%h
echo %title%

Note that if the file has multiple lines, only the last line will be set to the %title% variable.

If you want to then turn around and use the result of %title% as a parameter to a program, make sure you put it in quotes as it may contain spaces:

SomeProgram.exe "%title%"
Sign up to request clarification or add additional context in comments.

5 Comments

@PeterS - I just tested exactly what I have posted in my answer and it worked. If you copy/paste the answer exactly, what do you get as the result?
I just figured out the prob. When Im trying to echo the result, result are as expected. however, Im passing %title% into another variable (%4). But the word before the first space was passed. How to solve this? I want to pass the value, I only echo'ed the value in order to test
I already tried that solution before but im getting this error The system cannot find the file specified.
@PeterS - Please update your question with the call you are attempting to make with the %title% variable.

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.