Search code examples
batch-filesplitdos

Batch Extract pathdir from a variable


if I have this situation:

C:\Users\l72\Desktop\fs.cfg
C:\Users\s80\Desktop\tt.cfg
C:\Users\r88\Desktop\kj.cfg

and each entire path is setted to a variable

set dir1=C:\Users\l72\Desktop\fs.cfg
.
.
.

how can I extract only the second field(l72,s80,r88)?


Solution

  • for /f "tokens=3delims=\" %%i in ("%dir1%") do echo %%i
    

    is a way to extract the third token from the string contained in the environment variable dir1 when the string is tokenised using \ as a delimiter.