China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-06 10:32
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Solved] How to get n characters starting from a specified character in a string View 3,274 Replies 18
Original Poster Posted 2007-10-20 14:32 ·  中国 浙江 台州 电信
初级用户
Credits 88
Posts 34
Joined 2007-10-20 14:06
18-year member
UID 100256
Gender Male
Status Offline
20041119.WindowsXP-KB886677-x86-chs.exe
The above is a file name, and there are multiple such file names, but they are not completely regular.

Now, we need to obtain the 8-character string starting with kb, that is, kb******......

The following are the purpose requirements I want to achieve:

1. A text file contains the following content:
20041119.WindowsXP-KB886677-x86-chs.exe
windowsmedia-KB885836-x86-CHS.exe
Windows-KB890830-V1.34.exe
KB939653.exe

2. The batch processing commands to be done are:
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

3. Please help brothers write a batch processing:
for /f "tokens=1" %%i in (hotfix.txt) do (
@echo 正在提取 Windows XP 更新 %%i && @(%%i /x:.\***Here I don't know how to extract the KB directory name***)
)

Please give me some advice, big brother!!!!

[ Last edited by nghr on 2007-10-21 at 12:43 PM ]
Floor 2 Posted 2007-10-20 15:04 ·  中国 浙江 杭州 电信
高级用户
★★
Credits 537
Posts 219
Joined 2007-08-04 09:43
19-year member
UID 94620
Gender Male
From 杭州--半山
Status Offline
@echo off
@for /f "tokens=1-4 delims=-" %%a in (hotfix.txt) do echo %%a-%%b-%%c-%%d /x:.\%%b
pause
Floor 3 Posted 2007-10-20 15:28 ·  中国 浙江 台州 电信
初级用户
Credits 88
Posts 34
Joined 2007-10-20 14:06
18-year member
UID 100256
Gender Male
Status Offline
Thanks to the brother on the second floor!!!! There is a small problem with yours. Adding @ before echo %% should work.

It can't be executed. When executing, it automatically skips, actually it's like not executed. I mean like this, as long as it extracts the string whose file name contains -kb****-. Because some file names have no date, and some have no chs!!!

What if the file name is irregular? But there is a common point that all contain 8 characters of KB******. How to write it?

[ Last edited by nghr on 2007-10-20 at 03:37 PM ]
Floor 4 Posted 2007-10-20 15:34 ·  中国 浙江 台州 电信
初级用户
Credits 88
Posts 34
Joined 2007-10-20 14:06
18-year member
UID 100256
Gender Male
Status Offline
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
I modified it myself, this should be executable, but it's still not perfect!
Because the file names in hotfix.txt are irregular
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

But there is a common point, that is, it contains kb******
Floor 5 Posted 2007-10-20 15:46 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
sed "s/.*KB\(\+\).*/& \/x:.\\KB\1/" hotfix.txt
Floor 6 Posted 2007-10-20 15:49 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
```
@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
```
致精致简!
Floor 7 Posted 2007-10-20 16:08 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
@echo off
for /f "tokens=2 delims=B" %%a in (hotfix.txt) do (
for /f "delims=.-" %%i in ("%%a") do @echo Extracting Windows XP update %%i
)
pause
Floor 8 Posted 2007-10-20 19:30 ·  中国 浙江 温州 电信
中级用户
★★
Credits 458
Posts 196
Joined 2006-10-05 12:04
19-year member
UID 64614
Status Offline
set test=20041119.WindowsXP-KB886677-x86-chs.exe

for /f "tokens=1-5 delims=KB-." %a in ('echo %test%') do @echo KB%b
Floor 9 Posted 2007-10-21 11:30 ·  中国 浙江 台州 电信
初级用户
Credits 88
Posts 34
Joined 2007-10-20 14:06
18-year member
UID 100256
Gender Male
Status Offline
Originally posted by 26933062 at 2007-10-20 03:49 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=-." ...

Cannot be executed, and there is no KB display in the directory
Floor 10 Posted 2007-10-21 11:33 ·  中国 浙江 台州 电信
初级用户
Credits 88
Posts 34
Joined 2007-10-20 14:06
18-year member
UID 100256
Gender Male
Status Offline
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


It's just a display screen statement, no actual operation, and it can't display KB******, just extracts the following numbers
Floor 11 Posted 2007-10-21 11:50 ·  中国 浙江 台州 电信
初级用户
Credits 88
Posts 34
Joined 2007-10-20 14:06
18-year member
UID 100256
Gender Male
Status Offline
Combined with the help of friends, I modified it, and the following can be executed:

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 Extracting Windows XP update %%a && @%%a /x:.\%%b /q
))
pause

But there is still a small problem. The extracted directory name is the number after KB, and the two characters KB are not there.
The running effect is as follows:

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 ]
Floor 12 Posted 2007-10-21 12:05 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
@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 (
x:.\KB%%b
))
pause
第一高手 第二高手

Floor 13 Posted 2007-10-21 12:09 ·  中国 浙江 台州 电信
初级用户
Credits 88
Posts 34
Joined 2007-10-20 14:06
18-year member
UID 100256
Gender Male
Status Offline
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 ...

Thanks, buddy, finally got it done!!!
Floor 14 Posted 2007-10-21 12:17 ·  中国 浙江 台州 电信
初级用户
Credits 88
Posts 34
Joined 2007-10-20 14:06
18-year member
UID 100256
Gender Male
Status Offline
Let me see. The user is asking for help with batch processing to delete all files and directories under the KB***** directory except for the update and sp2qfe directories. The translated text would be:

Then ask you all bosses again
After executing the above batch processing, the file structure under the extracted KB***** directory is as follows:
update-----------------<Directory
sp1qfe-----------------<Directory
spuninst.exe---------<File
spmsg.dll--------------<File
sp2qfe-----------------<Directory
And the operations I need to do are as follows:
update-----------------<Directory ---------<Operation: Keep the directory
sp1qfe-----------------<Directory ---------<Operation: Delete the directory
spuninst.exe---------<File ---------<Operation: Delete the file
spmsg.dll--------------<File ---------<Operation: Delete the file
sp2qfe-----------------<Directory ---------<Operation: Keep the directory
The above is the general content to be done. Of course, it can't be simply solved by DEL, because the extracted directories are irregular, and there are other directories sometimes, but there is a common feature that is, under each KB****** directory there are update and sp2qfe directories. Now the specific operation I need to do is to delete all files and directories under the kb****** directory, only keep the update and sp2qfe directories. Please help to give pointers
Floor 15 Posted 2007-10-21 12:17 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Originally posted by nghr at 2007-10-21 11:33:

It's just a screen statement, no actual operation, and KB****** cannot be displayed, only the number behind is extracted


The most difficult thing is to extract the number? Can't you modify it yourself? Sweat~
Forum Jump: