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

[文本处理] 批处理如何原样输出百分号(变量不扩展)

本帖最后由 pcl_test 于 2018-3-28 05:36 编辑

请教一个很小白的问题
  1. echo netsh i i show in>>temp.bat
  2. echo ^@set /p idx=请输入“本地连接”的Idx编号:>>temp.bat
  3. echo netsh interface ipv4 set dns name=^%idx^% static 192.168.1.230 >>temp.bat
  4. echo netsh interface ipv4 add dns name=^%idx^% 192.168.1.223 index=2 >>temp.bat
  5. echo ping 127.1 -n 3 >null>>temp.bat
复制代码
运行后的结果是:
netsh i i show in
@set /p idx=请输入“本地连接”的Idx编号:
netsh interface ipv4 set dns name= static 192.168.1.230
netsh interface ipv4 add dns name= 192.168.1.223 index=2
ping 127.1 -n 3

可是我需要的运行结果是:
netsh i i show in
@set /p idx=请输入“本地连接”的Idx编号:
netsh interface ipv4 set dns name=%idx% static 192.168.1.230
netsh interface ipv4 add dns name=%idx% 192.168.1.223 index=2
ping 127.1 -n 3

我单独把第三和第四句在dos下运行,运行结果是我需要的结果。可是为什么组合到一起却不是我想得到的结果?
1

评分人数

    • pcl_test: 勿发笼统无意义的标题PB -4

☛☛☛☛
replace %idx% to %%idx%%
#&cls&@powershell "Invoke-Expression ([Io.File]::ReadAllText('%~0',[Text.Encoding]::UTF8))" &pause&exit

TOP

回复 2# ivor


谢谢!!按照您说的运行结果达到预期效果了!

只是能否说明一下为什么要这样改么?

TOP

Escape Characters
  1. Escape Characters
  2. Character to be escaped Escape Sequence Remark
  3. % %% May not always be required in doublequoted strings, just try
  4. ^ ^^ May not always be required in doublequoted strings, but it won't hurt
  5. & ^&
  6. < ^<
  7. > ^>
  8. | ^|
  9. ' ^' Required only in the FOR /F "subject" (i.e. between the parenthesis), unless backq is used
  10. ` ^` Required only in the FOR /F "subject" (i.e. between the parenthesis), if backq is used
  11. , ^, Required only in the FOR /F "subject" (i.e. between the parenthesis), even in doublequoted strings
  12. ; ^;
  13. = ^=
  14. ( ^(
  15. ) ^)
  16. ! ^^! Required only when delayed variable expansion is active
  17. " "" Required only inside the search pattern of FIND
  18. \ \\ Required only inside the regex pattern of FINDSTR
  19. [ \[
  20. ] \]
  21. " \"
  22. . \.
  23. * \*
  24. ? \?
复制代码
#&cls&@powershell "Invoke-Expression ([Io.File]::ReadAllText('%~0',[Text.Encoding]::UTF8))" &pause&exit

TOP

回复 4# ivor


    万分感谢!!!收藏之!

TOP

返回列表