windowsbatch-filecmddelayedvariableexpansion

Getting delayed expansion value to function output


Here is a windows cmd fragment:

:moveFiles
    @for /f %%R in ('dir /b current') do (
        @echo ___
        @echo Moving %%R to FTP server.
        %java_cmd% -jar f17ftp.jar -dput -file:%%R  %select%
        if 1 neq 0 (
            @echo Transfer of %%R failed.
            call :readLog
            echo %log%
            %java_cmd% -cp SQLQueryXML.jar com.saliencrgt.chums.AlertMessage "Contents of ftp log are:<br/><br>"%log%
        ) else (
            @echo Transfer of %%R succeeded.
            move current\%%R xmlfiles 1> nul
        )
    )
    exit /b

:readLog
    setlocal enableextensions enabledelayedexpansion
    @for /F "delims=" %%x in (f17ftp.debug.log) do (
        if .!build!==. (set build=%%x) else (set build=!build!,%%x)
    )
    endlocal & set log=!build!
    exit /b

I am trying to get the contents of a file into a variable to send as an argument to a java class The readLog function does read the file and if I echo !build! prior to the endlocal the file contents do print to the console.

I can set "log" to a constant instead of the !build! variable and I see it in the calling function, but I can't get the delayed variable value out of the function.


Solution

  • Here's a quick example of how I'd expect your :readLog labelled section to look:

    :readLog
        @set "log="
        @setlocal enableextensions enabledelayedexpansion
        @set "build="
        @for /f "usebackq delims=" %%x in ("f17ftp.debug.log") do @(
            if not defined build (set "build=%%x") else set "build=!build!,%%x"
        )
        @endlocal & set "log=%build%"
        @exit /b
    

    However to view it, as it is still being defined within a parenthesized block, you will probably find that your :moveFiles labelled section would also need to have enabled delayed expansion, and then echo !log! not echo %log%.

    You may find therefore that something a little more like this will work better for you:

    :moveFiles
        @for /f "eol=? delims=" %%R in ('dir /b "current" 2^>nul') do @(
            echo ___
            echo Moving %%R to FTP server.
            "%java_cmd%" -jar "f17ftp.jar" -dput -file:"%%R"  %select%
            if errorlevel 1 (
                echo Transfer of %%R failed.
                setlocal enabledelayedexpansion
                set "log="
                set "build="
                call :readLog
                echo !log!
                "%java_cmd%" -cp "SQLQueryXML.jar" com.saliencrgt.chums.AlertMessage "Contents of ftp log are:<br/><br>"!log!
                endlocal
            ) else (
                echo Transfer of %%R succeeded.
                move "current\%%R" "xmlfiles" 1>nul
            )
        )
        @exit /b
    
    :readLog
        @for /f "usebackq delims=" %%x in ("f17ftp.debug.log") do @(
            if not defined build (set "build=%%x") else set "build=!build!,%%x"
        )
        @set "log=%build%"
        @exit /b