B
B
bazkin2016-07-07 12:33:42
cmd/bat
bazkin, 2016-07-07 12:33:42

Why is the variable behaving so strangely in the for loop?

Hello!
The variable in a script behaves strangely. There is a file in which the paths to the files are entered line by line:

"d:\test\file1.txt"
"d:\test\file2.txt"
"d:\test\file3.txt"

There is a bat -script that reads the path line by line from the file, changes the drive letter in it and passes it on:
SetLocal EnableDelayedExpansion
FOR /F %%i IN (d:\test\tmp.txt) DO (
   SET str=%%i
   SET str=!str:~2,-1!
   echo %str%
)

The problem is the strange behavior of the %str% variable. It can always be empty , it can take the value as it should, i.e. " :\test\file1.txt :\test\file2.txt :\test\file3.txt ", or it can always be :\test\file3.txt
The same script on another server always runs correctly. Both servers are 2012R2Std 9600build.
Tell me what could be the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2016-07-07
@bazkin

Replace inside the
echo %str% loop
with
echo !str!

B
bazkin, 2016-07-07
@bazkin

Thank you very much! Using an exclamation mark (!) instead of a percentage (%) worked!
Can you clarify why this particular syntax is used in this case?
It turns out it is connected with the mode of binding of execution time?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question