【问题标题】:Cannot get batch macro to work (cmd.exe)?无法使批处理宏工作(cmd.exe)?
【发布时间】:2021-10-12 19:56:55
【问题描述】:

我见过Batch macro not working - 但它没有解决我的问题。

基本上,我看到了:

...我想,让我试试吧。

首先,我将其保存在test.bat 文件中:

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

我双击这个test.bat,我得到:

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

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

所以,好的,它有效。


然后我决定将上面的代码包装在一个“批处理宏”中——所以test.bat 变成了:

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

现在当我双击这个test.bat 时,它不起作用,我得到:

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 . . .

好像%$set% 被解释为一个变量,因此被视为空,然后执行该行的其余部分......

那么,我哪里出错了 - 在这种情况下,我可以让这个“批处理宏”工作吗?如果可以,怎么做?

【问题讨论】:

    标签: windows batch-file


    【解决方案1】:

    好吧,事实证明,这是我应该复制的整个函数 - 否则我被说服 %$set% 是一个特殊的内置批处理命令......

    所以完整的工作代码是:

    @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=
    

    当我现在双击 .bat 文件时,我得到:

    TESTPID 17704
    Press any key to continue . . .
    

    ...这是我所期望的...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-26
      • 2015-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多