找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 25925|回复: 4

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

[复制链接]
发表于 2018-2-23 16:04:26 | 显示全部楼层 |阅读模式
本帖最后由 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下运行,运行结果是我需要的结果。可是为什么组合到一起却不是我想得到的结果?

评分

参与人数 1PB -4 收起 理由
pcl_test -4 勿发笼统无意义的标题

查看全部评分

发表于 2018-2-23 17:09:41 | 显示全部楼层
☛☛☛☛
replace %idx% to %%idx%%
 楼主| 发表于 2018-2-23 17:25:11 | 显示全部楼层
回复 2# ivor


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

只是能否说明一下为什么要这样改么?
发表于 2018-2-23 17:37:20 | 显示全部楼层
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. ?        \?
复制代码
 楼主| 发表于 2018-2-23 17:40:57 | 显示全部楼层
回复 4# ivor


    万分感谢!!!收藏之!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-18 03:00 , Processed in 0.019788 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表