2

I'm trying to concatenate a repeating string depending on the first parameter value passed into the script when it is called. This value represents the number of directory levels the current directory is from the repository root in which the user is working. This is my script so far:

set REPO_ROOT="%~1"
set "UPPATH=.\"
set "UPDIR=..\"
for /l %%i in (1,1,%REPO_ROOT%) do set "UPPATH=%UPPATH%%UPDIR%"
echo %UPPATH%

My problem is that if I hard-code set "UPPATH=%UPPATH%%UPDIR%" any number of times, it works fine, but it doesn't seem to want to work in the loop. Any thoughts or suggestions on how to get this loop to work with the passed parameter would be appreciated. Thanks.

fixer1234
  • 28,064
Jim Fell
  • 6,099

1 Answers1

1

It works fine, but it doesn't seem to want to work in the loop.

You need to set EnableDelayedExpansion.


EnableDelayedExpansion

Delayed Expansion will cause variables to be expanded at execution time rather than at parse time, this option is turned on with the SETLOCAL command. When delayed expansion is in effect variables may be referenced using !variable_name! (in addition to the normal %variable_name% )

Example

Setlocal EnableDelayedExpansion

Source EnableDelayedExpansion

See the answer by Joey for a more detailed explanation of why this is needed.


Further Reading

DavidPostill
  • 162,382