标题: [文本处理] 【已解决】批处理怎样利用set替换掉文件中特定的内容 [打印本页]
作者: sniperhgy 时间: 2009-5-11 12:00 标题: 【已解决】批处理怎样利用set替换掉文件中特定的内容
看了一些高手的帖子,学着写了一个比较简单的,内容如下:
- @echo off&setlocal enabledelayedexpansion
- (for /f "eol= tokens=1,* delims=:" %%a in ('findstr /n .* test.ini') do (
- set "line=%%b"
- if "%line%" NEQ "" (set "line=!line:192.168.118.16=192.168.118.25!")
- echo.!line!
- ))>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文件内容:
- ;no$gba 2.6 generated config file - do not edit
-
- GBA Mode == Nintendo DS (in GBA mode)
- NDS Mode == Nintendo DS (normal 4MB)
- Emulation Speed, LCD Refresh == -Realtime, Auto
- Reset/Startup Entrypoint == GBA BIOS (Nintendo logo)
- Topmost Display Lines == -Display Normal
- Sound Output Mode == None (fastest)
- Sound Desired Sample Rate == Low (10kHz) (fast)
- Video Output == 15bit Color
- Emulate BIOS Functions == By real GBA.ROM (accurate)
- Solar Sensor Level == Darkness
- GBA Cartridge Backup Media == -Auto
- NDS-Cartridge Backup Media == -Auto
- Multiboot Port == -None/Disabled
- Multiboot Completion == -Auto-close Upload Box
- Multiboot Normal/BurstDelays == -Medium/Medium (stable)
- Game Screen Filter == None (fast)
- Decompressed Help File == Delete after Usage
- Create Game Window at == Upper/right of Debug Window
- Game Screen Sizing == Strict
- IIgame_size == normal
- Number of Emulated Gameboys == -Single Machine
- Link Gamepaks == -Gamepaks in all GBAs
- Link Cable Type == -Automatic
- Performance Indicator == -Show Timing only if <>100%
- Autosave Options == -Nope
- Load ROM-Images to == -All machines
-
- Joypad Button A == 1
- Joypad Button B == 2
- Joypad Select == 3
- Joypad Start == 4
- Joypad Button L == 5
- Joypad Button R == 6
- Joypad Button X == 7
- Joypad Button Y == 8
- SNESpad Adapter == -None/Disabled (fast)
- SNESpad Button A == -Button A
- SNESpad Button B == -Button B
- SNESpad Button X == -Button X
- SNESpad Button Y == -Button Y
- Mouse Control Mode == -Free Move (right button=on)
- Joysticks/Gamepads == -Enabled
-
- IP=192.168.118.163
-
- SAV/SNA File Format == Compressed
- IIautoRun == yes
- Firmware Boot == Manual
- 3D Renderer == nocash
- IIsnd == stereo
- IIreg == show
- IIcrk == show
-
- gdb_xsiz == 00000000
- gdb_ysiz == 00000000
- cht_xsiz == 00000000
- cht_ysiz == 00000000
- Game_xloc == 00000235
- Game_yloc == 0000015F
- Game_xsiz == 000109D9
- Game_ysiz == 000109CC
-
- KEYB_1 == 111F1E202524222318261716
- KEYB_2 == A1A9A4A65253517526135959
- KEYB_3 == 595959595959590259595959
- KEYB_4 == 595959595959590359595959
- KEYB_5 == 595959595959590459595959
- KEYB_6 == 595959595959590559595959
- KEYB_7 == 595959595959590659595959
- KEYB_8 == 595959595959590759595959
- KEYB_9 == 595959595959590859595959
- KEYB_A == 595959595959590959595959
- KEYB_B == 595959595959590A59595959
- KEYB_C == 595959595959590B59595959
-
- GIFname == NO$GBA.GIF
复制代码
文件中,有些空行就是一个"",而第二行,是38个空格
我就是想无论是"",还是"若干个空格",都替换成
“次行为空”,现在的问题就是,有什么办法,可以判断
某一行是否为""或者"若干个空格"
作者: Batcher 时间: 2009-5-11 16:57 标题: 回复 5楼 的帖子
- @echo off&setlocal enabledelayedexpansion
- (for /f "tokens=1,* delims=:" %%a in ('findstr /n .* test.ini') do (
- set "line=%%b"
- if "!line!" NEQ "" (
- set lineblank=!line: =!
- if "!lineblank!" NEQ "" (
- set "line=!line:192.168.118.16=192.168.118.25!"
- echo/!line!
- ) else (
- echo/
- )
- ) else (
- echo/
- )
- ))>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://bbs.bathome.net/) |
Powered by Discuz! 7.2 |