1

I am doing some experiments to learn how works the batch script.

I have an issue with displaying some text in a loop

Here is my code :

 for  %%j in (C:\Users\*) do (
 SET _test=123456789abcdef0
  SET _result=%_test:~0,5%
 ECHO %_result%          =12345
 )

And the result is : =12345

If I use the following code :

 SET _test=123456789abcdef0
  SET _result=%_test:~0,5%
 ECHO %_result%          =12345

Then the result is 12345 =12345 as expected.

What is wrong with the loop here ?

1 Answer 1

2

You need delayed expansion.

 setlocal enableDelayedExpansion
 for  %%j in (C:\Users\*) do (
 SET _test=123456789abcdef0
  SET _result=!_test:~0,5!
 ECHO !_result!          =12345
 )
Sign up to request clarification or add additional context in comments.

Comments

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.