gpt4 book ai didi

windows - 批处理文件 : How to capture output of a function in a variable?

转载 作者:可可西里 更新时间:2023-11-01 11:49:19 27 4
gpt4 key购买 nike

这里是批处理脚本的新手。我正在 try catch Batch 'function' 的输出(不完全是因为 Batch 缺乏对函数的内置支持)变量。这是我的代码:

@echo off
setlocal enabledelayedexpansion
goto main

:: Functions
:runps
powershell -NoProfile -ExecutionPolicy Bypass -Command "%1"
goto :eof

:appendToPath
set OLDPATHPS="[Environment]::GetEnvironmentVariable('PATH', 'User')"
for /f %%i in ('call :runps %OLDPATHPS%') do ^
set OLDPATH=%%i
:: ...
goto :eof

:main
call :appendToPath

当我运行它时,我从控制台得到以下输出:

Invalid attempt to call batch label outside of batch script.

为什么会发生这种情况,我该如何解决?

最佳答案

Call 建立一个新的 cmd 实例,它不能访问旧实例中的批处理标签。

解决方案包括将结果输出到文件并读取文件或可能

for /f %%i in ('%runps% "%OLDPATHPS%"') do (
set OLDPATH=%%i
)

其中 runps 已设置为 powershell -NoProfile -ExecutionPolicy Bypass -Command

关于windows - 批处理文件 : How to capture output of a function in a variable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34255127/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com