标题: [已解决]如何获取一符字符串中指定字符开始的n个字符
[打印本页]
作者: nghr
时间: 2007-10-20 14:32
标题: [已解决]如何获取一符字符串中指定字符开始的n个字符
20041119.WindowsXP-KB886677-x86-chs.exe
上面是一个文件字,这样子的文件名有多个,但也不是完全有规则的
现在要获取的字符串中以kb开始的8个字符,即kb******......
以下是我要达到的目的要求
一,一个文本文件,包含内容如下
20041119.WindowsXP-KB886677-x86-chs.exe
windowsmedia-KB885836-x86-CHS.exe
Windows-KB890830-V1.34.exe
KB939653.exe
二,要做的批处理命令为
20041119.WindowsXP-KB886677-x86-chs.exe
/x:.\kb886677
20041213.WindowsXP-KB873339-x86-CHS.exe /x:.\kb873339
windowsmedia-KB885836-x86-CHS.exe /x:.\KB885836
Windows-KB890830-V1.34.exe /x:.\KB890830
KB939653.exe /x:.\KB939653
三,请各位兄弟帮忙写个批处理
for /f "tokens=1" %%i in (hotfix.txt) do (
@echo 正在提取 Windows XP 更新 %%i && @(%%i /x:.\***这里KB目录名不知道怎么提取了***)
)
那位大哥指点一下!!!!
[
Last edited by nghr on 2007-10-21 at 12:43 PM ]
作者: junchen2
时间: 2007-10-20 15:04
echo off
@for /f "tokens=1-4 delims=-" %%a i n (hotfix.txt) do echo %%a-%%b-%%c-%%d /x:.\%%b
pause
作者: nghr
时间: 2007-10-20 15:28
感谢二楼的兄弟!!!!你的这个有点小问题echo %%前加上@应该可以了
不能执行,执行的时候自动的跳过了,实际上等于没有执行,我的意思是这样子的,只要提取文件名字包含-kb****-的字符串,因为有的文件名是没有日期的,有的也没有chs的!!!
如果是不规则的文件名呢?但有一个共享点都是包含KB******八个字符的,如何编写?
[
Last edited by nghr on 2007-10-20 at 03:37 PM ]
作者: nghr
时间: 2007-10-20 15:34
dir /b |find "KB" >hotfix.txt
for /f "tokens=1-4 delims=-" %%a in (hotfix.txt) do @echo 正在提取 Windows XP 更新 %%a && @%%a-%%b-%%c-%%d /x:.\%%b /q)
pause
我自已改了一下,这个应该可以执行了,但是还是不完美啊!
因为hotfix.txt里的文件名有不规则的
20060606.WindowsXP-KB916595-x86-CHS.exe
20060608.WindowsXP-KB918439-x86-CHS.exe
20060612.WindowsXP-KB914389-x86-CHS.exe
20060612.WindowsXP-KB917953-x86-CHS.exe
20060613.WindowsXP-KB917344-x86-CHS.exe
20060710.WindowsXP-KB914388-x86-CHS.exe
20060807.WindowsXP-KB920670-x86-CHS.exe
20060807.WindowsXP-KB920683-x86-CHS.exe
20060911.WindowsXP-KB919007-x86-CHS.exe
20060911.WindowsXP-KB920685-x86-CHS.exe
20060911.WindowsXP-KB922582-x86-CHS.exe
20061009.WindowsXP-KB922819-x86-CHS.exe
20061009.WindowsXP-KB923191-x86-CHS.exe
20061009.WindowsXP-KB923414-x86-CHS.exe
20061009.WindowsXP-KB924496-x86-CHS.exe
20061113.WindowsXP-KB920213-x86-CHS.exe
20061113.WindowsXP-KB923980-x86-CHS.exe
20061113.WindowsXP-KB924270-x86-CHS.exe
20061211.WindowsXP-KB926255-x86-CHS.exe
20070212.WindowsXP-KB918118-x86-CHS.exe
20070212.WindowsXP-KB924667-x86-CHS.exe
20070212.WindowsXP-KB926436-x86-CHS.exe
20070212.WindowsXP-KB927779-x86-CHS.exe
20070212.WindowsXP-KB927802-x86-CHS.exe
20070212.WindowsXP-KB928255-x86-CHS.exe
20070212.WindowsXP-KB928843-x86-CHS.exe
20070403.WindowsXP-KB925902-x86-CHS.exe
20070409.WindowsXP-KB930178-x86-CHS.exe
20070409.WindowsXP-KB931261-x86-CHS.exe
20070409.WindowsXP-KB931784-x86-CHS.exe
20070409.WindowsXP-KB932168-x86-CHS.exe
20070424.WindowsXP-KB930916-x86-CHS.exe
20070611.WindowsXP-KB929123-x86-CHS.exe
20070611.WindowsXP-KB935839-x86-CHS.exe
20070611.WindowsXP-KB935840-x86-CHS.exe
20070611.WindowsXP-KB936357-x86-CHS.exe
20070724.WindowsXP-KB938828-x86-CHS.exe
20070801.WindowsXP-KB933360-x86-CHS.exe
20070808.WindowsXP-KB896423-x86-CHS.exe
20070813.WindowsXP-KB938127-x86-CHS.exe
20070814.WindowsXP-KB921503-x86-CHS.exe
20070814.WindowsXP-KB936021-x86-CHS.exe
20070814.WindowsXP-KB938829-x86-CHS.exe
20071008.WindowsXP-KB933729-x86-CHS.exe
20071008.WindowsXP-KB941202-x86-CHS.exe
20071009.WindowsXP-KB939653-x86-CHS.exe
WindowsMedia9-KB936782-x86-CHS.exe
WindowsMedia10-KB936782-x86-CHS.exe
WindowsMedia11-KB936782-x86-CHS.exe
WindowsMedia11-KB939683-x86-CHS.exe
WindowsMedia11-KB929399-v2-x86-INTL.exe
20070813.IE7-WindowsXP-KB938127-x86-CHS.exe
20071009.IE7-WindowsXP-KB939653-x86-CHS.exe
20051213.WindowsXP-KB904706-v2-x86-CHS.exe
20060213.WindowsMedia-KB911564-x86-CHS.exe
20060425.WindowsXP-KB908531-v2-x86-CHS.exe
20060627.WindowsXP-KB911280-v2-x86-CHS.exe
20061113.WindowsXP-KB923789-x86-CHS.exe
20070507.WindowsXP-KB927891-v3-x86-CHS.exe
20070710.WindowsMedia6-KB925398-v2-x86-CHS.exe
20070710.WindowsXP-KB923689-v2-x86-CHS.exe
Windows-KB890830-V1.34.exe
KB939653.exe
但有一个共同点,就是包含kb******
作者: lxmxn
时间: 2007-10-20 15:46
标题: 用sed试试
sed "s/.*KB\([0-9]\+\).*/& \/x:.\\KB\1/" hotfix.txt
作者: 26933062
时间: 2007-10-20 15:49
@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%a in (hotfix.txt) do (
set var=%%a
set var=!var:kb=#!
set var=!var:*#=!
for /f "tokens=1 delims=-." %%b in ("!var!") do (
echo %%a x:.\%%b
))
pause
作者: slore
时间: 2007-10-20 16:08
@echo off
for /f "tokens=2 delims=B" %%a in (hotfix.txt) do (
for /f "delims=.-" %%i in ("%%a") do @echo 正在提取 Windows XP 更新 %%i
)
pause
作者: zerocq
时间: 2007-10-20 19:30
set test=20041119.WindowsXP-KB886677-x86-chs.exe
for /f "tokens=1-5 delims=KB-." %a in ('echo %test%') do @echo KB%b
作者: nghr
时间: 2007-10-21 11:30
Quote: |
Originally posted by 26933062 at 2007-10-20 03:49 PM:
[code]
@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%a in (hotfix.txt) do (
set var=%%a
set var=!var:kb=#!
set var=!var:*#=!
for /f "tokens=1 delims=-." ... |
|
不能执行,且目录没有KB显示
作者: nghr
时间: 2007-10-21 11:33
Quote: |
Originally posted by slore at 2007-10-20 04:08 PM:
@echo off
for /f "tokens=2 delims=B" %%a in (hotfix.txt) do (
for /f "delims=.-" %%i in ("%%a") do @echo 正在提取 Windows XP 更新 %%i
)
pause |
|
只是显示屏幕语句,没有实际操作,也不能显示KB******,只是将后面的数字提取出来了
作者: nghr
时间: 2007-10-21 11:50
结合朋友们的帮助,我修改一下,下面的可以执行了
dir /b |find "KB" >hotfix.txt
@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%a in (hotfix.txt) do (
set var=%%a
set var=!var:kb=#!
set var=!var:*#=!
for /f "tokens=1 delims=-." %%b in ("!var!") do (
@echo 正在提取 Windows XP 更新 %%a && @%%a /x:.\%%b /q
))
pause
但是还有个小问题,提取的目录名称是KB后面的数字,KB两个字符没有
运行的效果如下:
20041119.WindowsXP-KB886677-x86-chs.exe /x:.\886677
20041213.WindowsXP-KB873339-x86-CHS.exe /x:.\873339
windowsmedia-KB885836-x86-CHS.exe /x:.\885836
Windows-KB890830-V1.34.exe /x:.\890830
KB939653.exe /x:.\939653
[
Last edited by nghr on 2007-10-21 at 11:57 AM ]
作者: fastslz
时间: 2007-10-21 12:05
@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%a in (hotfix.txt) do (
set var=%%a
set var=!var:kb=#!
set var=!var:*#=!
for /f "tokens=1 delims=-." %%b in ("!var!") do (
echo %%a x:.\KB%%b
))
pause
6楼的代码加%%b前面加KB,要实际操作echo %%a x:.\KB%%b里的echo去掉
作者: nghr
时间: 2007-10-21 12:09
Quote: |
Originally posted by fastslz at 2007-10-21 12:05 PM:
@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%a in (hotfix.txt) do (
set var=%%a
set var=!var:kb=#!
set var=!var:*#=!
for /f "tokens=1 delims=-." %%b in (&q ... |
|
感谢老兄,终于搞掂了!!!
作者: nghr
时间: 2007-10-21 12:17
再问一下各位老大
执行了上面的批处理之后,提取的KB*****目录下的文件结构如下:
update-----------------<目录
sp1qfe-----------------<目录
spuninst.exe---------<文件
spmsg.dll--------------<文件
sp2qfe-----------------<目录
而我要做的操作如下:
update-----------------<目录 ---------<操作:保留目录
sp1qfe-----------------<目录 ---------<操作:删除目录
spuninst.exe---------<文件 ---------<操作:删除文件
spmsg.dll--------------<文件 ---------<操作:删除文件
sp2qfe-----------------<目录 ---------<操作:保留目录
上面是大致要做的内容,当然不是DEL可以简单解决的,因为提取的目录是不规则的,有的还有其他目录,但有一个共同特点,就是每个KB******目录下都是有update与sp2qfe目录,我现在要做的具体操作就是删除kb******目录下所有文件与目录,只保留update与sp2qfe目录,请帮助指点一下
作者: slore
时间: 2007-10-21 12:17
Quote: |
Originally posted by nghr at 2007-10-21 11:33:
只是显示屏幕语句,没有实际操作,也不能显示KB******,只是将后面的数字提取出来了 |
|
最难的就是提取数字吧?你自己不会改?汗~
作者: nghr
时间: 2007-10-21 12:21
Quote: |
Originally posted by slore at 2007-10-21 12:17 PM:
最难的就是提取数字吧?你自己不会改?汗~ |
|
改好了,感谢兄弟的指点,偶对批处理还刚入门,不好意思!
作者: fastslz
时间: 2007-10-21 13:21
笨办法find 过滤不删除的
@echo off
dir /s /a-d /b /x >tmp.txt
type tmp.txt|find /i /v "sp2qfe" >tmp1.txt
type tmp1.txt|find /i /v "update" >tmp2.txt
type tmp2.txt|find /i /v "%~n0" >tmp3.txt
for /f "delims=*" %%i in (tmp3.txt) do
echo del /f /s /q %%i
del tmp*.txt 2>nul
dir /s /ad /b >tmp.txt
type tmp.txt|find /i /v "sp2qfe" >tmp1.txt
type tmp1.txt|find /i /v "update" >tmp2.txt
for /f "delims=*" %%i in (tmp2.txt) do
echo rd /q %%i
del tmp*.txt 2>nul
pause
这个批处理把红色echo部分去掉杀伤力很强,特别在根目录,请谨慎操作,本人概不负责
[
Last edited by fastslz on 2007-10-21 at 01:22 PM ]
作者: nghr
时间: 2007-10-21 14:40
Quote: |
Originally posted by fastslz at 2007-10-21 01:21 PM:
笨办法find 过滤不删除的
@echo off
dir /s /a-d /b /x >tmp.txt
type tmp.txt|find /i /v "sp2qfe" >tmp1.txt
type tmp1.txt|find /i /v "update" >tmp2.txt
type t ... |
|
强,I服了YOU
作者: wang6610
时间: 2007-10-21 23:01
回帖很精彩