1

I am trying to use the FIXED_LENGTH variable within a FOR loop.

Using the length of the string directly in the loop results in correct result, however trying to use the variable does not work. Any ideas?

Snippet that provides correct result

SETLOCAL ENABLEDELAYEDEXPANSION

REM Loop through the input file and read each line.
FOR /F %%G IN (SVN_URLs_List.txt) DO (

    SET CUR_LINE=%%G
    REM Make sure the variable CUR_LINE is surrounded with ! instead of % to ensure delayed variable expansion.
    @ECHO Processing the line: !CUR_LINE!

    REM Set fixed length of "https://projects.abcdefg.com/subversion/"
    SET FIXED_LENGTH=40

    REM Get the sub-string of CUR_LINE to get the folder name.
    SET FOLDER_NAME=!CUR_LINE:~40!
    @ECHO !FOLDER_NAME!
)
ENDLOCAL

Snippet that does NOT produce correct result:

SETLOCAL ENABLEDELAYEDEXPANSION

REM Loop through the input file and read each line.
FOR /F %%G IN (SVN_URLs_List.txt) DO (

    SET CUR_LINE=%%G
    REM Make sure the variable CUR_LINE is surrounded with ! instead of % to ensure delayed variable expansion.
    @ECHO Processing the line: !CUR_LINE!

    REM Set fixed length of "https://projects.abcdefg.com/subversion/"
    SET FIXED_LENGTH=40

    REM Get the sub-string of CUR_LINE to get the folder name.
    SET FOLDER_NAME=!CUR_LINE:~!!FIXED_LENGTH!!!
    @ECHO !FOLDER_NAME!
)
ENDLOCAL

I have tried

SET FOLDER_NAME=!CUR_LINE:~!!FIXED_LENGTH!!!
SET FOLDER_NAME=!CUR_LINE:~!FIXED_LENGTH!!
SET FOLDER_NAME=!CUR_LINE:~%FIXED_LENGTH%!

The input in the SVN_URLs_List.txt looks like:

https://projects.abcdefg.com/subversion/svnrepo1
https://projects.abcdefg.com/subversion/svn_repo_2
https://projects.abcdefg.com/subversion/another repo 3

I am expecting result:

svnrepo1
svn_repo_2
another repo 3

The first snippet produces right results. However using the second snippet produces something like:

https://projects.abcdefg.com/subversion/svnrepo1FIXED_LENGTH
https://projects.abcdefg.com/subversion/svn_repo_2FIXED_LENGTH
https://projects.abcdefg.com/subversion/another repo 3FIXED_LENGTH

1 Answer 1

1
SET FOLDER_NAME=!CUR_LINE:~!!FIXED_LENGTH!!!
SET FOLDER_NAME=!CUR_LINE:~!FIXED_LENGTH!!

Wrong syntax. Where does the variable reference begin/end? The parser can not determine it.

SET FOLDER_NAME=!CUR_LINE:~%FIXED_LENGTH%!

Almost, the parse can handle this syntax but bad behaviour. As the variable (FIXED_LENGTH) is assigned its value inside of the for code block, and as the parser replaced the read operation on the variable with its value at parse time (no delayed expansion syntax used to read the variable value) before the variable was assigned its value, the operation will fail.

In your case, instead of defining the FIXED_LENGTH variable inside the for loop, you can define the variable before the for loop, out of it. So, when the block of lines in the do clause of the for command is parsed, the variable will have the adecuated value.

If you prefer/need to declare the variable inside the for loop, you will have to use something like

for %%a in (!FIXED_LENGTH!) do SET FOLDER_NAME=!CUR_LINE:~%%a!

That way the read operation on FIXED_LENGTH is delayed, assigned to the replaceable parameter of the for loop and this parameter is used instead of the variable reference in the substring operation.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.