标题: [已解决]如何在带引号的字符串内添加字符
[打印本页]
作者: xb21cn
时间: 2008-1-30 16:34
标题: [已解决]如何在带引号的字符串内添加字符
原文件boot.ini如下:
timeout=1
default=multi(0)disk(0)rdisk(0)partition(1)\windows
multi(0)disk(0)rdisk(0)partition(1)\windows=”××××××ד ××××××
××××××
要将文件改成如下:
timeout=1
default=multi(0)disk(0)rdisk(0)partition(1)\windows
multi(0)disk(0)rdisk(0)partition(1)\windows=”××××××ד ××××××
multi(0)disk(0)rdisk(0)partition(1)\windows=”×××××××[KF]“ ×××××× UNRVS
××××××
(也就是在“”内字符串前或后加入特定的字符(KF),××××××也为不确定的内容)
望指教!
Last edited by xb21cn on 2008-2-2 at 03:01 PM ]
作者: fastslz
时间: 2008-1-30 17:46
作者: xb21cn
时间: 2008-1-31 17:28
通过楼上指点,搜索后已经解决上个问题:
@echo off
for /f "delims=" %%m in (boot.ini) do (
echo.%%m | find /i "/fastdetect">nul && (echo %%m & echo %%m UNRVS)||echo %%m
)>>boot.new
但还有一个问题就是达到如下效果不知如何实现:
(原文件格式)
××××××="××××××" ××××××
需改成:
××××××=“(KF)××××××” ××××××
或
××××××=“××××××(KF)” ××××××
(也就是在“”内字符串前或后加入特定的字符(KF))
望指教!
作者: fastslz
时间: 2008-1-31 17:39
作者: xb21cn
时间: 2008-2-1 12:17
刚才又搜索了一番,还是搞不定啊,不想用第三方工具,只想批处理
作者: slore
时间: 2008-2-1 14:10
按=号和空格分割你可以得到"XXXXXXX"这段吧?
输入set /?
可以看到一个取字符串的子字符串的方法……如果在这里 1,-1
可以得到去掉引号的部分……
你自己 " +文字+上面得到的字符串+文字+"即可完成。
作者: xb21cn
时间: 2008-2-1 14:17
楼上的不是很了解,因为引号中的内容不确定,如何set不确定的内容呢?
还有=和"之间没有空格噢
Last edited by xb21cn on 2008-2-1 at 02:18 PM ]
作者: slore
时间: 2008-2-1 14:38
@echo off
for /f "tokens=2* delims==/" %%i in (c:\boot.ini) do (
echo %%i
)
pause>nul
自己运行看吧~按=和参数的/划分
作者: xb21cn
时间: 2008-2-1 16:31
谢谢楼上热心回复,试了代码结果如下:
1
multi(0)disk(0)rdisk(0)partition(1)\windows
"××××××"
"××××××"
"××××××"
只是不知怎么会多出前面两行呢,如果没有就ok了
作者: fastslz
时间: 2008-2-1 16:40
确实没有很通用的代码,我临时撮合了一段代码,只处理Microsoft OS
@echo off
attrib -s -h -r C:\boot.ini
del *boot.x 2>nul
findstr /i /n ".*" C:\boot.ini >boot.x
setlocal enabledelayedexpansion
for /f "tokens=1,2,3* delims==/" %%a in ('type boot.x^|find /i "/fastdetect"^|find /v "2000"') do (
set osname=%%b
>>newboot.x echo %%a="XXXXXXXX !osname:~1!/%%c=%%d
)
for /f "tokens=1,2* delims==/" %%a in ('type boot.x^|find /i "/fastdetect"^|find /i "2000"') do (
set osname=%%b
>>newboot.x echo %%a="XXXXXXXX !osname:~1!/%%c
)
for /f "tokens=1 delims=:" %%i in (newboot.x) do call set str=%%str%% %%i
findstr /v "%str%" boot.x>tmpboot.x
copy tmpboot.x + newboot.x boot.x
del newboot.x
for /l %%a in (1,1,14) do findstr /i "%%a:" boot.x>>newboot.x
del boot.x
for /f "delims=" %%i in ('type newboot.x^|findstr "1: 2: 3: 4: 5: 6: 7: 8: 9:"') do (
set var=%%i
>>boot.x echo.!var:~2!
)
for /f "delims=" %%i in ('type newboot.x^|findstr "10: 11: 12: 13: 14:"') do (
set var=%%i
>>boot.x echo.!var:~3!
)
move /y boot.x C:\boot.ini
attrib +s +h +r C:\boot.ini
del *boot.x 2>nul
pause
作者: xb21cn
时间: 2008-2-1 16:53
10楼的代码运行结果:
:
timeout=1
default=multi(0)disk(0)rdisk(0)partition(1)\windows
multi(0)disk(0)rdisk(0)partition(1)\windows="XXXXXXXX windows 2003 专业版" /fastdetect =noexecute=alwaysoff
在双引号中加字符已经实现,只是还有一点小问题:
第二行多了:符号
第七行中/noexecute变成了=noexecute
不知什么原因
Last edited by xb21cn on 2008-2-1 at 04:54 PM ]
作者: fastslz
时间: 2008-2-1 17:10
我说了没通用办法,OS参数有变化更不通用,我这里的结果
multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="XXXXXXXX Microsoft Windows XP Professional" /noexecute=optin /fastdetect
multi(0)disk(0)rdisk(1)partition(1)\WINNT="XXXXXXXX Microsoft Windows 2000 Professional" /fastdetect
multi(0)disk(0)rdisk(0)partition(2)\WINDOWS="XXXXXXXX Windows Server 2003, Datacenter MCC Edition" /noexecute=optout /fastdetect
作者: fastslz
时间: 2008-2-1 17:17
for /f "tokens=1,2,3* delims==/" %%a in ('type boot.x^|find /i "/fastdetect"') do echo %%b
再替换该字符吧
作者: xb21cn
时间: 2008-2-1 17:42
试了下楼上修改的代码,可以准确提取双引号内字符串,但双引号本身也被提出来了。
Last edited by xb21cn on 2008-2-2 at 02:56 PM ]
作者: slore
时间: 2008-2-1 17:53
你自己修改吧~基本上的思路我6楼说过了……
个人觉得那个思路挺通用的呀!
8楼只是个演示。。。。
这个也是演示~不过已经很具体了。
@echo off
for /f "skip=4 tokens=1,2* delims==/" %%i in (C:\boot.ini) do (
Set StrA=%%i
Set StrB=%%j
Set StrC=%%k
Call :EchoBoot
)
pause>nul
exit
:EchoBoot
if "%StrC%"=="" goto :NoC
echo %StrA%="Slore + %StrB:~1,-1% + %date%" /%StrC%
goto :EBend
:NoC
echo %StrA%="Slore + %StrB:~1,-1% + %date%"
:EBend
作者: xb21cn
时间: 2008-2-2 14:53
经过分析10楼的代码,修改一处后终于OK了,只是添加的字符不知如何才能加到后面,试着改改了,加到后面的就只能在“外,只有另再想办法了。
15楼的代码正是我想要的,只是不知如何按需求写回文件,只有自己慢慢再考虑了
谢谢两位了