1

When inside a for loop, the set command doesn't set the string variable, whereas outside it works fine. I would like it to export to a .txt file directly after storing, and that works fine. Here is the code:

For /l %%a in (1,1,5) do (
set /p string="StringIn %%a:"
echo %string %>> string_list.txt
)

Start string_list.txt

1 Answer 1

1

When expanding variables set within parentheses scope you need to use delayed expansion.

setlocal EnableDelayedExpansion
For /l %%a in (1,1,5) do (
    set /p string="StringIn %%a:"
    >> string_list.txt echo !string!
)
endlocal
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! That worked well (and will be good knowledge for the future), but I'm using this code for folder comparison, and I found a more efficient means by using DIR and exporting the subfolders to a text document. Do you know if there is a way to format the command or output so I have only the folder name and not all the other data? This kind of deviates from the original question, so I don't know if I should ask a new question?
Use the '/b' option for only the file path and name with the '/ad' option to only list directories with the dir command. See 'dir /?' for all the options.

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.