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-04 12:09
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Re-seeking help] Novice asks about batch processing to modify text file content issues View 1,665 Replies 13
Original Poster Posted 2007-05-03 22:31 ·  中国 福建 泉州 石狮市 电信
初级用户
Credits 25
Posts 11
Joined 2007-04-03 16:24
19-year member
UID 83843
Gender Male
Status Offline
If there is a file with content (not including quotes) " ABCDEFGHH PPP EGFAFDAF", I want to find PPP and delete PPP and all the content after it, and skip if not found! How to do it? Thanks in advance! Posting again with a thick face! :P
Floor 2 Posted 2007-05-03 22:47 ·  中国 广东 湛江 廉江市 电信
高级用户
★★★
Credits 959
Posts 311
Joined 2006-04-11 14:08
20-year member
UID 53665
Gender Male
From 广东-LianJiang
Status Offline

Suppose your file is 1.txt

for /f "tokens=1 delims=PPP" %%i in (1.txt) do (
echo %%i >2.txt
)


κχυμγνξοθπρωψιαδλεηφβτζσ┬╀┾┳┞┯┰┱┣┲┳╂╁│├┟┭┠这是什么??这就是我的人生
Floor 3 Posted 2007-05-04 14:20 ·  中国 福建 泉州 石狮市 电信
初级用户
Credits 25
Posts 11
Joined 2007-04-03 16:24
19-year member
UID 83843
Gender Male
Status Offline
Thanks! But when the file has a carriage return, only the line before the carriage return is output, and the following cannot be input into 2.txt. For example, the content (not including quotes) " ABCDEFGHH
44
PPP
EGFAFDAF" can only output the first line! How to improve?
Floor 4 Posted 2007-05-04 14:21 ·  中国 福建 泉州 石狮市 电信
初级用户
Credits 25
Posts 11
Joined 2007-04-03 16:24
19-year member
UID 83843
Gender Male
Status Offline
The FOR command is really deep. I don't understand it and am studying hard.
Floor 5 Posted 2007-05-04 17:27 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
The efficiency is relatively low.
```
@echo off
for /f "delims=" %%a in (1.txt) do (
echo %%a|find /i "PPP" >nul 2>&1 && set str=batchering
if not defined str echo\%%a>>11.txt
)
start 11.txt
```
Floor 6 Posted 2007-05-04 20:29 ·  中国 福建 泉州 晋江市 电信
初级用户
Credits 25
Posts 11
Joined 2007-04-03 16:24
19-year member
UID 83843
Gender Male
Status Offline
Originally posted by lxmxn at 2007-5-4 04:27 AM:
Low efficiency.


@echo off
for /f "delims=" %%a in (1.txt) do (
echo %%a|find /i "PPP" >nul 2>&1 && set str=batchering
if not defined str echo\ ...

No words, convinced! Great!
Floor 7 Posted 2007-05-04 21:59 ·  中国 福建 泉州 石狮市 电信
初级用户
Credits 25
Posts 11
Joined 2007-04-03 16:24
19-year member
UID 83843
Gender Male
Status Offline
Let me ask you again, if you want to delete all the text before PPP, including spaces, returns and so on, how to change it?
Floor 8 Posted 2007-05-05 21:45 ·  中国 河北 廊坊 电信
新手上路
Credits 8
Posts 3
Joined 2007-05-04 18:02
19-year member
UID 87469
Gender Male
Status Offline
Originally posted by qasa at 2007-5-3 10:47 PM:

Suppose your file is 1.txt

for /f "tokens=1 delims=PPP" %%i in (1.txt) do (
echo %%i >2.txt
)



Can you help me take a look at what I wrote?
@echo off
setlocal enabledelayedexpansion
set F=1.txt
set "F=%F:"=%"
for %%i in ("%F%") do set F=%%~fi
set a=PPP
set b=
for /f "delims=" %%i in ('type "%F%"') do (
set str=%%i
set "str=!str:%a%=%b%!"
echo !str!>>"%F%"_new.txt
)
Why does it show "ECHO is off."
chuier
Floor 9 Posted 2007-05-06 16:04 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Originally posted by chuier at 2007-5-5 21:45:
Can you help me take a look at this paragraph I wrote?
@echo off
setlocal enabledelayedexpansion
set F=1.txt
set "F=%F:"=%"
for %%i in ("%F%") do set F=%%~fi
set a=PPP
set b=
for /f ...


The %F% in the first for represents a file. If this file does not exist, %%~fi will also expand its full path, which may eventually cause the second for command to exit because the file cannot be found. It is better to use the form for /f %%a in ('dir %F%') do …… to judge whether there is the %F% file.

When I tested, it was normal and could be replaced, but overall, this batch processing is not rigorous.

In addition, a reminder: send a new topic for new questions.
Floor 10 Posted 2007-05-06 19:49 ·  中国 河北 廊坊 电信
新手上路
Credits 8
Posts 3
Joined 2007-05-04 18:02
19-year member
UID 87469
Gender Male
Status Offline
### Question 1:
In the command `for /f "delims=" %%a in (1.txt) do (echo %%a|find /i "PPP" >nul 2>&1)`, `2>&1` means redirecting the standard error output (file descriptor 2) to the standard output (file descriptor 1). So that any error messages from the `find` command are sent to the same place as the normal output, and in this case, both are redirected to `nul` to suppress them.

### Question 2:
`set str=batchering` means setting a variable named `str` with the value "batchering".
chuier
Floor 11 Posted 2007-05-06 20:15 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Re chuier:

1. Redirect the output of handle 2 (StdErr) to the input of handle 1, and the input of handle 1 is then output to the null device (Nul), so "2>&1" can shield the error output (StdErr) of the command, similar to 2>nul;

2. Here, defining a str is for the service of the "if not defined str" statement, which means that if the str variable is not defined, the following command will be executed.

That's a simple explanation of this point. If you want to know more about their usage in detail, you can search for related discussions in the forum.
Floor 12 Posted 2007-05-06 20:58 ·  中国 江苏 南京 中移铁通
初级用户
Credits 26
Posts 11
Joined 2006-12-15 10:48
19-year member
UID 73609
Gender Male
Status Offline
for /f "tokens=1 delims=PPP" %%i in (1.txt) do (
echo\%%i >>2.txt
)

It's relatively simple. However, qasa's echo %%i 〉2.txt should be changed to echo\%%i >>2.txt
Just does it;I lov bat
Floor 13 Posted 2007-05-06 22:01 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Originally posted by jinrich at 2007-5-6 20:58:
for /f "tokens=1 delims=PPP" %%i in (1.txt) do (
echo\%%i >>2.txt
)

It's relatively simple. However, qasa's echo %%i >2.txt should be changed to echo\%%i >>2.txt

This doesn't work. It can only read the characters before the character P, and cannot handle the characters before PPP.
Floor 14 Posted 2007-05-08 04:26 ·  中国 广东 深圳 宝安区 电信
新手上路
Credits 2
Posts 1
Joined 2007-03-02 11:05
19-year member
UID 80555
Gender Male
Status Offline
Originally posted by caiyiqun at 2007-5-3 10:31 PM:
If there is a file with content (excluding quotes) " ABCDEFGHH PPP EGFAFDAF", I want to find PPP and delete PPP and all the content that follows it, and skip if not found!

How to do it?

Thanks in advance! Posting again with cheekiness! :P



Then if we change PPP to AAA, how to change it? Keep all the content that follows it

[ Last edited by a201 on 2007-5-8 at 04:27 AM ]
Forum Jump: