标题: [文本处理] 批处理set变量失败,这是因为什么 [打印本页]
作者: hentai87 时间: 2020-6-3 13:28 标题: 批处理set变量失败,这是因为什么
- @echo off
- pushd "%~dp0"
-
- for /f "delims=:" %%a in ('findstr /n msmg设置 ..\set.txt') do set n=%%a
- for /f "skip=%n% delims=" %%b in (..\set.txt) do (
- set dism=%%b\Bin\x64\DISM10\dism.exe
- echo %dism%
- )
-
- pause
复制代码
结果
ECHO is off.
Press any key to continue . . .
但是这样就成功了
set var=我是值
echo %var%
是无法set %%b吗
作者: smss 时间: 2020-6-3 13:38
复制代码
作者: hentai87 时间: 2020-6-3 17:27
回复 2# smss
setlocal EnableDelayedExpansion
D:\lopatkin\Package_Remover\Remove-package>for /F "delims=:" %a in ('findstr /n msmg设置 ..\set.txt') do set n=%a
D:\lopatkin\Package_Remover\Remove-package>set n=2
D:\lopatkin\Package_Remover\Remove-package>for /F "skip=2 delims=" %b in (..\set.txt) do (
set dism=%b\Bin\x64\DISM10\dism.exe
echo
)
D:\lopatkin\Package_Remover\Remove-package>(
set dism=D:\ZT\ToolKit_v9.9\Bin\x64\DISM10\dism.exe
echo
)
ECHO is on.
还是这样
作者: flashercs 时间: 2020-6-3 18:01
- @echo off
- pushd "%~dp0"
-
- for /f "delims=:" %%a in ('findstr /n msmg设置 ..\set.txt') do set n=%%a
- for /f "skip=%n% delims=" %%b in (..\set.txt) do (
- set dism=%%b\Bin\x64\DISM10\dism.exe
- )
- echo %dism%
- pause
复制代码
cmd命令行的括号()中的所有语句等同于一行代码,而cmd执行每一行语句前要给该行代码的所有变量进行预赋值,此时%dism%还是空值,所以echo %dism%等同于echo 空白.
作者: Batcher 时间: 2020-6-3 23:25
回复 1# hentai87 - @echo off
- setlocal enabledelayedexpansion
- pushd "%~dp0"
- for /f "delims=:" %%a in ('findstr /n msmg设置 ..\set.txt') do (
- call :GetDISM %%a
- )
- pause
- goto :eof
-
- :GetDISM
- for /f "skip=%1 delims=" %%b in (..\set.txt) do (
- set "dism=%%b\Bin\x64\DISM10\dism.exe"
- echo,!dism!
- )
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |