You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

批处理Goto后返回技术问题:调用子程序后无法回到原执行点

解决批处理goto调用子程序后无法返回的问题

这个问题我之前帮朋友排查过,核心问题是你用错了跳转指令——批处理里的goto是单向跳转,不会帮你记录返回位置,而call才是专门用来调用子程序并返回的指令。

问题原因拆解

你的代码里用goto :installscreen跳转到显示屏幕的子程序,执行完后用goto :eof返回,但goto :eof的作用是直接跳到整个脚本的末尾,而不是回到你跳转前的位置。所以当你从:10跳过去执行完子程序后,直接就到脚本结尾了,:10里的pause和后面的:20:30自然不会被执行。

修复方案

把所有的goto :installscreen替换成call :installscreen就行。call指令会保存当前的执行位置,当子程序执行到goto :eof时,会自动回到call的下一行继续执行。

修改后的完整代码:

@echo off
cls
setlocal enabledelayedexpansion
:10
set watitle=10
title %watitle%
call :installscreen
pause
:20
set watitle=20
title %watitle%
call :installscreen
pause
:30
set watitle=30
title %watitle%
call :installscreen
pause
exit
:installscreen
echo.
echo ===============================================================================
echo %watitle%
echo ===============================================================================
echo.
goto :eof

额外说明

如果你需要在子程序里提前返回,也可以用exit /b替代goto :eof,效果是完全一样的——都会回到call的下一行继续执行脚本。

内容的提问来源于stack exchange,提问作者Romeo Montecchi

火山引擎 最新活动