0

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.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Keith Fosberg
  • 89
  • 2
  • 9
  • First, you've posted only part of your code, and in doing so, have included variables which have not been defined within. For this reason it is not a [mcve], and should be updated to make it so. I have additionally noticed the you appear to be using and `if`/`else` structure, where the `else` can never be invoked, `1` will always `n`ot `eq`ual `0`. There is a distinct lack of doublequotes throughout your code, including in the line with `if .!build!==.` which should at least be `if "!build!" == ""`, and better yet as `if not defined`. `endlocal & set log=!build!` will always undefine `log`. – Compo Aug 01 '21 at 19:59
  • The iff/e;se is rigged so that I always go through the error routine because that is what I am trying to get working. The rest of the scrip is full of proprietary values that I can't post online. – Keith Fosberg Aug 01 '21 at 20:50
  • Do not include solutions in the question; if you really want to share the eventually used code, post an answer instead, with attribution (link) to the answer it is based on… – aschipfl Aug 02 '21 at 16:47

1 Answers1

1

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
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Thank you very much. I also had to surround the !log! with double quotes for the java call because it just sat there waiting for the world to end otherwise. – Keith Fosberg Aug 01 '21 at 21:14
  • Further note: The requirement for the double quotes had nothing to do with the batch file. It was required for the java to read the argument correctly. – Keith Fosberg Aug 01 '21 at 21:25