@echo off
set THIS_FILENAME=%0
if "%1" == "" set VARNAME=VARNAME
if Not "%1" == "" set VARNAME=%1
set VARDATA=%TEMP%\%0_%VARNAME%.cmd
set CMDTOSET=%~2

if "%1" == "" (
    call :USAGE %VARNAME%
) else if "%CMDTOSET%" == "" (
     REM The findstr used below is simply there to capture everything from STDIN
     findstr /V "|||||||" > "%VARDATA%"
     FOR /F "usebackq tokens=1*" %%i IN (`type %VARDATA%`) DO call :SETIT %VARNAME% 1%%i "2%%j"  %VARDATA%
     REM set /p %VARNAME%= < "%VARDATA%"
     goto :EOF
) else (
     if Exist "%VARDATA%" del "%VARDATA%"
     call %CMDTOSET%  >  "%VARDATA%"
     FOR /F "usebackq tokens=1*" %%i IN (`type %VARDATA%`) DO call :SETIT %VARNAME% 1%%i "2%%j"
     REM set /p %VARNAME%= < "%VARDATA%"
     del %VARDATA%
)
goto :DONE

:SETIT
REM echo set %1=%2 %3
set VARNAME=%1
REM The juggling below using the :~1 expression is to trim the leading character
REM character from 1 and 2 parameter designators.  We have this character as a
REM placeholder, because there may not be a value for parameter #2.
set D1=%2
set D1=%D1:~1%
set D2=%~3
set D2=%D2:~1%
REM If there is no remaining data in D2, then we are fine,
REM but is there is more, then we want to append this to our values to be set.
if Not "%D2%" == "" set D1=%D1% %D2%

if "%4" == "" (
    REM echo set %VARNAME%=%D1%
    set %VARNAME%=%D1%
) else (
    set VARDATA=%4
    echo set %VARNAME%=%D1%> %VARDATA%
    echo exportVarSet.cmd must be call'ed to enforce the set of %VARNAME%.
    echo call exportVarSet.cmd %VARNAME%
)
goto :eof

:USAGE
     echo Usage for %THIS_FILENAME%
     echo usage1:
     echo            call %THIS_FILENAME% %1 "[quoted command(s) to evaluate]"
     echo .
     echo or (if your command(s) contain "" marks)
     echo usage2:
     echo "          [command(s) to evaluate] | %THIS_FILENAME% %1                     "
     echo            call %VARDATA%
     echo            del %VARDATA%
     echo .
     echo where %1 is the name of the variable you'd like to set.
     goto :DONE
goto :eof

:DONE
set VARNAME=
set VARDATA=
set THIS_FILENAME=