Board logo

标题: [文本处理] 【已解决】批处理怎样利用set替换掉文件中特定的内容 [打印本页]

作者: sniperhgy    时间: 2009-5-11 12:00     标题: 【已解决】批处理怎样利用set替换掉文件中特定的内容

看了一些高手的帖子,学着写了一个比较简单的,内容如下:
  1. @echo off&setlocal enabledelayedexpansion
  2. (for /f "eol= tokens=1,* delims=:" %%a in ('findstr /n .* test.ini') do (
  3.     set "line=%%b"
  4.     if "%line%" NEQ "" (set "line=!line:192.168.118.16=192.168.118.25!")
  5.     echo.!line!
  6. ))>haha.ini
复制代码

我的本意是空行的话(这里的空行指line.trim() == "",也就是允许有空格的),就跳过
然后发现行内有192.168.118.16,就替换成192.168.118.25,运行后不成功,估计是
if "%line%" NEQ ""这句有问题,或者是set的用法不对,麻烦知道的朋友指点一下,谢谢了

[ 本帖最后由 sniperhgy 于 2009-5-11 13:16 编辑 ]
作者: Batcher    时间: 2009-5-11 13:00

把%line%改成!line!
作者: sniperhgy    时间: 2009-5-11 13:15

^_^可以了,谢谢Batcher,不过这是为什么呢?
还有一点,我的文件中有一行是13个空格,如果我是想将空行(允许有空格)都写上
“次行为空”,要怎样做呢?批处理里面有类似string.trim()的方法吗?
作者: Batcher    时间: 2009-5-11 13:42     标题: 回复 3楼 的帖子

1、可能你对变量延迟扩展的理解还有点问题,请参考:批处理中的变量延迟扩展、变量嵌套
http://bbs.bathome.net/viewthread.php?tid=2899

2、看不懂你的问题,请把原始的ini文件内容和想要修改成啥样贴出来看看。
作者: sniperhgy    时间: 2009-5-11 14:20

ini文件内容:
  1. ;no$gba 2.6 generated config file - do not edit
  2.                                       
  3. GBA Mode == Nintendo DS (in GBA mode)
  4. NDS Mode == Nintendo DS (normal 4MB)
  5. Emulation Speed, LCD Refresh == -Realtime, Auto
  6. Reset/Startup Entrypoint == GBA BIOS (Nintendo logo)
  7. Topmost Display Lines == -Display Normal
  8. Sound Output Mode == None (fastest)
  9. Sound Desired Sample Rate == Low (10kHz) (fast)
  10. Video Output == 15bit Color
  11. Emulate BIOS Functions == By real GBA.ROM (accurate)
  12. Solar Sensor Level == Darkness
  13. GBA Cartridge Backup Media == -Auto
  14. NDS-Cartridge Backup Media == -Auto
  15. Multiboot Port == -None/Disabled
  16. Multiboot Completion == -Auto-close Upload Box
  17. Multiboot Normal/BurstDelays == -Medium/Medium (stable)
  18. Game Screen Filter == None (fast)
  19. Decompressed Help File == Delete after Usage
  20. Create Game Window at == Upper/right of Debug Window
  21. Game Screen Sizing == Strict
  22. IIgame_size == normal
  23. Number of Emulated Gameboys == -Single Machine
  24. Link Gamepaks == -Gamepaks in all GBAs
  25. Link Cable Type == -Automatic
  26. Performance Indicator == -Show Timing only if <>100%
  27. Autosave Options == -Nope
  28. Load ROM-Images to == -All machines
  29. Joypad Button A == 1
  30. Joypad Button B == 2
  31. Joypad Select == 3
  32. Joypad Start == 4
  33. Joypad Button L == 5
  34. Joypad Button R == 6
  35. Joypad Button X == 7
  36. Joypad Button Y == 8
  37. SNESpad Adapter == -None/Disabled (fast)
  38. SNESpad Button A == -Button A
  39. SNESpad Button B == -Button B
  40. SNESpad Button X == -Button X
  41. SNESpad Button Y == -Button Y
  42. Mouse Control Mode == -Free Move (right button=on)
  43. Joysticks/Gamepads == -Enabled
  44. IP=192.168.118.163
  45. SAV/SNA File Format == Compressed
  46. IIautoRun == yes
  47. Firmware Boot == Manual
  48. 3D Renderer == nocash
  49. IIsnd == stereo
  50. IIreg == show
  51. IIcrk == show
  52. gdb_xsiz == 00000000
  53. gdb_ysiz == 00000000
  54. cht_xsiz == 00000000
  55. cht_ysiz == 00000000
  56. Game_xloc == 00000235
  57. Game_yloc == 0000015F
  58. Game_xsiz == 000109D9
  59. Game_ysiz == 000109CC
  60. KEYB_1 == 111F1E202524222318261716
  61. KEYB_2 == A1A9A4A65253517526135959
  62. KEYB_3 == 595959595959590259595959
  63. KEYB_4 == 595959595959590359595959
  64. KEYB_5 == 595959595959590459595959
  65. KEYB_6 == 595959595959590559595959
  66. KEYB_7 == 595959595959590659595959
  67. KEYB_8 == 595959595959590759595959
  68. KEYB_9 == 595959595959590859595959
  69. KEYB_A == 595959595959590959595959
  70. KEYB_B == 595959595959590A59595959
  71. KEYB_C == 595959595959590B59595959
  72. GIFname == NO$GBA.GIF
复制代码

文件中,有些空行就是一个"",而第二行,是38个空格
我就是想无论是"",还是"若干个空格",都替换成
“次行为空”,现在的问题就是,有什么办法,可以判断
某一行是否为""或者"若干个空格"
作者: Batcher    时间: 2009-5-11 16:57     标题: 回复 5楼 的帖子

  1. @echo off&setlocal enabledelayedexpansion
  2. (for /f "tokens=1,* delims=:" %%a in ('findstr /n .* test.ini') do (
  3.   set "line=%%b"
  4.   if "!line!" NEQ "" (
  5.     set lineblank=!line: =!
  6.     if "!lineblank!" NEQ "" (
  7.       set "line=!line:192.168.118.16=192.168.118.25!"
  8.       echo/!line!
  9.     ) else (
  10.       echo/
  11.     )
  12.   ) else (
  13.     echo/
  14.   )
  15. ))>haha.ini
复制代码
注意:安装你的思路,192.168.118.163会被替换成192.168.118.253,请确认一下这是不是你想要的结果。

[ 本帖最后由 Batcher 于 2009-5-11 17:00 编辑 ]
作者: netbenton    时间: 2009-5-11 18:01

还有个方法:
在红色部分的冒号后面加一个空格就可以在遇到只有空格行时让%%b为空了


for /f "tokens=1,* delims=: " %%a in ('findstr /n .* test.ini') do (
作者: sniperhgy    时间: 2009-5-12 11:05

哦,謝謝樓上兩位指點,方法已經習得




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