0

I've seen Batch macro not working - but it does not address my question.

Basically, I saw:

... and I thought, let me try it.

So first, I save this in a test.bat file:

powershell -command "$proc = Start-Process notepad.exe -passthru ; Write-Output $proc.id"
pause

I double-click this test.bat, and I get:

C:\tmp>powershell -command "$proc = Start-Process notepad.exe -passthru ; Write-Output $proc.id"
10968

C:\tmp>pause
Press any key to continue . . .

So, ok, it works.


Then I decide to wrap the above code in a "batch macro" - so test.bat becomes instead:

%$set% TESTPID="powershell -command "$proc = Start-Process notepad.exe -passthru ; Write-Output $proc.id""
echo TESTPID %TESTPID%
pause

Now when I double-click this test.bat, it does not work, and I get:

C:\tmp>TESTPID="powershell -command "$proc = Start-Process notepad.exe -passthru ; Write-Output $proc.id""
'TESTPID' is not recognized as an internal or external command,
operable program or batch file.

C:\tmp>echo TESTPID
TESTPID

C:\tmp>pause
Press any key to continue . . .

It's as if the %$set% was interpreted as a variable, and as such was taken as empty, and then the rest of the line was executed ...

So, where am I going wrong - and can I get this "batch macro" to work in this case, and if so, how?

sdbbs
  • 4,270
  • 5
  • 32
  • 87

1 Answers1

1

Ok, turns out, that was a whole function that I should have copied - I was otherwise persuaded %$set% was a special built-in batch command ...

So the complete working code is:

@echo off
call :initMacro

%$set% TESTPID="powershell -command "$proc = Start-Process notepad.exe -passthru ; Write-Output $proc.id""
echo TESTPID %TESTPID[0]%
pause

:initMacro
if "!!"=="" (
    echo ERROR: Delayed Expansion must be disabled while defining macros
    (goto) 2>nul
    (goto) 2>nul
)
(set LF=^
%=empty=%
)
(set \n=^^^
%=empty=%
)

set $set=FOR /L %%N in (1 1 2) dO IF %%N==2 ( %\n%
    setlocal EnableDelayedExpansion                                 %\n%
    for /f "tokens=1,* delims== " %%1 in ("!argv!") do (            %\n%
        endlocal                                                    %\n%
        endlocal                                                    %\n%
        set "%%~1.Len=0"                                            %\n%
        set "%%~1="                                                 %\n%
        if "!!"=="" (                                               %\n%
            %= Used if delayed expansion is enabled =%              %\n%
                setlocal DisableDelayedExpansion                    %\n%
                for /F "delims=" %%O in ('"%%~2 | findstr /N ^^"') do ( %\n%
                if "!!" NEQ "" (                                    %\n%
                    endlocal                                        %\n%
                    )                                               %\n%
                setlocal DisableDelayedExpansion                    %\n%
                set "line=%%O"                                      %\n%
                setlocal EnableDelayedExpansion                     %\n%
                set pathExt=:                                       %\n%
                set path=;                                          %\n%
                set "line=!line:^=^^!"                              %\n%
                set "line=!line:"=q"^""!"                           %\n%
                call set "line=%%line:^!=q""^!%%"                   %\n%
                set "line=!line:q""=^!"                             %\n%
                set "line="!line:*:=!""                             %\n%
                for /F %%C in ("!%%~1.Len!") do (                   %\n%
                    FOR /F "delims=" %%L in ("!line!") Do (         %\n%
                        endlocal                                    %\n%
                        endlocal                                    %\n%
                        set "%%~1[%%C]=%%~L" !                      %\n%
                        if %%C == 0 (                               %\n%
                            set "%%~1=%%~L" !                       %\n%
                        ) ELSE (                                    %\n%
                            set "%%~1=!%%~1!!LF!%%~L" !             %\n%
                        )                                           %\n%
                    )                                               %\n%
                    set /a %%~1.Len+=1                              %\n%
                )                                                   %\n%
            )                                                       %\n%
        ) ELSE (                                                    %\n%
            %= Used if delayed expansion is disabled =%             %\n%
            for /F "delims=" %%O in ('"%%~2 | findstr /N ^^"') do ( %\n%
                setlocal DisableDelayedExpansion                    %\n%
                set "line=%%O"                                      %\n%
                setlocal EnableDelayedExpansion                     %\n%
                set "line="!line:*:=!""                             %\n%
                for /F %%C in ("!%%~1.Len!") DO (                   %\n%
                    FOR /F "delims=" %%L in ("!line!") DO (         %\n%
                        endlocal                                    %\n%
                        endlocal                                    %\n%
                        set "%%~1[%%C]=%%~L"                        %\n%
                    )                                               %\n%
                    set /a %%~1.Len+=1                              %\n%
                )                                                   %\n%
            )                                                       %\n%
        )                                                           %\n%
        set /a %%~1.Max=%%~1.Len-1                                  %\n%
)                                                                   %\n%
    ) else setlocal DisableDelayedExpansion^&set argv=

When I double-click the .bat file now, I get:

TESTPID 17704
Press any key to continue . . .

... which is what I expected ...

sdbbs
  • 4,270
  • 5
  • 32
  • 87