Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 批处理文件-更改for循环中的变量值_Batch File_For Loop - Fatal编程技术网

Batch file 批处理文件-更改for循环中的变量值

Batch file 批处理文件-更改for循环中的变量值,batch-file,for-loop,Batch File,For Loop,我有以下代码片段 setlocal enableextensions enabledelayedexpansion set b=123 for %%f in (.\input\*.mgc) do ( set "b=%%~nf" echo %b% ) 我希望它输出没有扩展名的文件名,但我总是得到“123”。我的结论是,它有一些后期扩展的问题,但不太确定问题来自何方。我还尝试了echo!b

我有以下代码片段

    setlocal enableextensions enabledelayedexpansion
    set b=123
    for %%f in (.\input\*.mgc) do (
       set "b=%%~nf"
       echo %b%
    )

我希望它输出没有扩展名的文件名,但我总是得到“123”。我的结论是,它有一些后期扩展的问题,但不太确定问题来自何方。我还尝试了
echo!b我相信确凿的证据。我看不到你在执行代码,或者你在混淆代码,所以我来执行你的代码。就像我说的那样

C:\BatchFiles\SO>type testing.bat
@echo off
setlocal enableextensions enabledelayedexpansion
set b=123
for %%f in (.\input\*.mgc) do (
        echo %%~nf
        set "b=%%~nf"
        echo !b!
)
pause
C:\BatchFiles\SO>dir /b .\input\*.mgc
testfile.mgc

C:\BatchFiles\SO>testing.bat
testfile
testfile
Press any key to continue . . .

C:\BatchFiles\SO>

尝试将
%b%
替换为
!b如上所述:

扩展应延迟的变量应使用感叹号而不是百分号包围


关于cmd.exe如何扩展变量值的常见误解。您需要将变量引用为
echo!b
@Squashman我在我的问题中写道,我已经尝试过了,但是没有结果,
只有两种方法可以回应!b显示文字
!b-要么您没有使用
setlocal enabledelayedexpansion
,要么输入目录中的一个文件名为
!Bmgc
。实际上,这是您输出结果的唯一两种方式。如果
b
没有设置,延迟扩展实际上像你声称的那样打开,你会看到错误
ECHO关闭。
你在使用什么操作系统?显然我没有阅读整个问题,但这对我在Windows 10中的工作很好。你刚刚发布了与我在问题中使用的代码完全相同的代码,但说我的代码在Windows 10中对你不起作用。
 setlocal enableextensions enabledelayedexpansion
 set b=123
 for %%f in (.\input\*.mgc) do (
     set "b=%%~nf"
     echo !b!
 )