Board logo

标题: [文本处理] 批处理set变量失败,这是因为什么 [打印本页]

作者: hentai87    时间: 2020-6-3 13:28     标题: 批处理set变量失败,这是因为什么

  1. @echo off
  2. pushd "%~dp0"
  3. for /f "delims=:" %%a in ('findstr /n msmg设置 ..\set.txt') do set n=%%a
  4. for /f "skip=%n% delims=" %%b in (..\set.txt) do (
  5. set dism=%%b\Bin\x64\DISM10\dism.exe
  6. echo %dism%
  7. )
  8. pause
复制代码
结果
ECHO is off.
Press any key to continue . . .

但是这样就成功了
set var=我是值
echo %var%

是无法set %%b吗
作者: smss    时间: 2020-6-3 13:38

  1. 加延迟变量试试
复制代码

作者: 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

  1. @echo off
  2. pushd "%~dp0"
  3. for /f "delims=:" %%a in ('findstr /n msmg设置 ..\set.txt') do set n=%%a
  4. for /f "skip=%n% delims=" %%b in (..\set.txt) do (
  5. set dism=%%b\Bin\x64\DISM10\dism.exe
  6. )
  7. echo %dism%
  8. pause
复制代码
cmd命令行的括号()中的所有语句等同于一行代码,而cmd执行每一行语句前要给该行代码的所有变量进行预赋值,此时%dism%还是空值,所以echo %dism%等同于echo 空白.
作者: Batcher    时间: 2020-6-3 23:25

回复 1# hentai87
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. pushd "%~dp0"
  4. for /f "delims=:" %%a in ('findstr /n msmg设置 ..\set.txt') do (
  5.     call :GetDISM %%a
  6. )
  7. pause
  8. goto :eof
  9. :GetDISM
  10. for /f "skip=%1 delims=" %%b in (..\set.txt) do (
  11.     set "dism=%%b\Bin\x64\DISM10\dism.exe"
  12.     echo,!dism!
  13. )
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2