[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] 批处理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吗

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

TOP

回复 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.


还是这样

TOP

  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 空白.
微信:flashercs
QQ:49908356

TOP

回复 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. )
复制代码
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

返回列表