Home > Net >  Using Variable as Substring Parameter in Batch
Using Variable as Substring Parameter in Batch

Time:06-08

I'm trying to extract the string before the last slash that I retrieve from my function; :FIND_SLASH. But when I echoed the result, it is displaying the variable name ndxOfSlash, instead of the extracted string. The list.txt contains the sample input below.

Sample Input:

AAA/BBBB/CCC/test.txt

Expected output:

AAA/BBBB/CCC/

Actual output:

ndxOfSlash

Code Snippet

@echo off

setlocal EnableDelayedExpansion

FOR /F "tokens=*" %%x in (list.txt) DO (

    set tempname=%%x
    call :FIND_SLASH indexSlash
    set ndxOfSlash=!indexSlash!
    call set fileDir=!!tempname:~0,!ndxOfSlash!!!
    echo !fileDir!

)

I also tried replacing the variable parameter; ndxOfSlash, with an actual number, and it was able to display the expected output.

I referred to this site when doing the substring.

CodePudding user response:

call set fileDir=%%tempname:~0,!ndxOfSlash!%%

should fix the problem.

  • Related