Dan
Dan

Reputation: 713

for command is executed only for the first value when a label is inside

I have the script

for /f "delims=" %%i in ('dir "%folder%*.txt" /b /s') do (
    set s=%%i
    set s=!s:%folder%=!
    set new_s=!s:\=!
    if "x!new_s!" NEQ "x!s!" (
        :ProcessListSource
        For /f "tokens=1* delims=\" %%A in ("!s!") do (
            if "%%A" NEQ "" ( 
                if "!Folder1!" NEQ "" (
                    Set Folder1=!Folder1!\!Name!
                )else (
                    Set Folder1=!Name!
                )
                Set Name=%%A
            )
            if "%%B" NEQ "" (
                set s=%%B
                goto :ProcessListSource
            )
        )
        echo Folder is: !Folder1!
        echo Name is: !Name!
        echo ---------------------
    ) else (
        echo Not a folder !s!
    )
)

but it does not work as I would have expected: The first for is executed only once and also the last echo is printed on the screen.

Given a folder I need the files from subfolders without the given folder and than split them into the folder and file

Ex: folder=C:\test

The for would give me the file C:\test\test1\test2\t.txt

And I need test1\test2 and t.txt

Upvotes: 2

Views: 46

Answers (2)

MC ND
MC ND

Reputation: 70971

When the parser reads your code, all the code inside your for loop is "considered" as only one command that is readed, parsed and executed. As stated in the npocmaka answer, any goto call takes you out of this "line" of code, ending the process of the for loop.

This is a alternative. Use pushd + xcopy /l /s commands to generate a list of the relative paths of the files.

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "folder=%cd%"

    pushd "%folder%"
    for /f "delims=" %%a in ('xcopy /l /s /y * "%temp%"^|findstr /vbr /c:"[0-9]"'
    ) do for /f "delims=: tokens=1,*" %%b in ("%%~a") do (
        echo [%%c] [%%~nxa]
    )
    popd

Upvotes: 1

npocmaka
npocmaka

Reputation: 57332

GOTO breaks your FOR /F \ IF context and they can be executed only once.

More simple example:

@echo off

for /l %%S in (1=1=5) do (
    echo %%S
    goto :inner_label
    rem
    :inner_label
    rem

)

This will print only 1 . Do you really need the GOTO here?

Upvotes: 2

Related Questions