0

I would like to read a list of paths from a file and determine each time the last folder. I have found something to start with, failed, read various threads dealing with nested for loops and variable expansions however without success. I do not get a "Last folder:" response. Anyone seeing the problem here? Any help is greatly appreciated!

External file (paths.txt):

C:\FirstFolder\NextFolder\LastFolder
C:\Somewhere\Deeper\FinallyThere
C:\Temp

Windows batch script:

@echo OFF
setLocal EnableDelayedExpansion

for /F "tokens=* eol=#" %%f in (%CD%\paths.txt) do (
    echo Get new path from file: %%f
    set mydir=%%f
    set m=%mydir:\=;%
    call :GET_SYMLINK %%m
)

setLocal DisableDelayedExpansion
goto :eof


:GET_SYMLINK
for /F "tokens=* delims=;" %%i IN (%1) DO call :LAST_FOLDER %%i
goto :eof


:LAST_FOLDER
if "%1"=="" (
    echo Last folder: %LAST%
    goto :eof
)
set LAST=%1
SHIFT
goto :LAST_FOLDER
Community
  • 1
  • 1

2 Answers2

0
@echo OFF
setLocal EnableDelayedExpansion

for /F "tokens=* eol=#" %%f in (%CD%\paths.txt) do (
    echo Get new path from file: %%f
    set mydir=%%f
)
set m=%mydir:\=;%
call :GET_SYMLINK %m%

Within a block statement (a parenthesised series of statements), the entire block is parsed and then executed. Any %var% within the block will be replaced by that variable's value at the time the block is parsed - before the block is executed - the same thing applies to a FOR ... DO (block).

Hence, IF (something) else (somethingelse) will be executed using the values of %variables% at the time the IF is encountered.

Two common ways to overcome this are 1) to use setlocal enabledelayedexpansion and use !var! in place of %var% to access the changed value of var or 2) to call a subroutine to perform further processing using the changed values.

Hence, replacing the %s in the setting of m within the loop with ! should fix the problem - in part.

Next %%m refers to the matavariable (loop-control variable) m - %m% refers to the environment variable m but within the loop to the original value, !m! to the value as the loop changes.

Next problem is that since you are replacing \ with ; and ; is a separator, to pass it as a parameter to your subroutine, you would need to "enclose it in double quotes." You could then remove those double-quotes by using %~1 in your subroutine if that is what you require.

Personally, I'd move the substringing to the subroutine, since mydir would then contain the line. Since you say you want the last line from the file, then at the end of the loop, mydir will contain the last line, so there's no point in doing the subroutine for each line - just once at the end is sufficient.

I'm not sure what your subroutines are intended to do, so I'll make no comment about those.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Thanks for the explanations, wasn't aware abou the block statement and effectively lost myself in the variable type jungle. As the the oneliner of @Aacini does the job I'll give him the credits. – Mr. Tonight Mar 17 '15 at 13:06
0

I am afraid you are not clear about what is the result you want, but if it is this:

LastFolder
FinallyThere
Temp

... then this Batch file got it:

@echo off
for /F %%a in (paths.txt) do echo %%~Na

For further details, type for /? and read the description about "FOR variable modifiers".

If the paths may have spaces, include "delims=" option in FOR this way:

for /F "delims=" %%a in (paths.txt) do echo %%~Na
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Excellent, that's what I effectively needed, tried that approach at the beginning but couldn't figure out the %%~Na. Thanks! – Mr. Tonight Mar 17 '15 at 13:00