中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-04 13:14
48,038 topics / 350,123 posts / today 0 new / 48,251 members
DOS批处理 & 脚本技术(批处理室) » [Re-seeking help] Novice asks about batch processing to modify text file content issues
Printable Version  1,668 / 13
Floor1 caiyiqun Posted 2007-05-03 22:31
初级用户 Posts 11 Credits 25
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
Floor2 qasa Posted 2007-05-03 22:47
高级用户 Posts 311 Credits 959 From 广东-LianJiang

Suppose your file is 1.txt



Floor3 caiyiqun Posted 2007-05-04 14:20
初级用户 Posts 11 Credits 25
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?
Floor4 caiyiqun Posted 2007-05-04 14:21
初级用户 Posts 11 Credits 25
The FOR command is really deep. I don't understand it and am studying hard.
Floor5 lxmxn Posted 2007-05-04 17:27
版主 Posts 4,938 Credits 11,386
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
```
Floor6 caiyiqun Posted 2007-05-04 20:29
初级用户 Posts 11 Credits 25
Originally posted by lxmxn at 2007-5-4 04:27 AM:
Low efficiency.
Floor7 caiyiqun Posted 2007-05-04 21:59
初级用户 Posts 11 Credits 25
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?
Floor8 chuier Posted 2007-05-05 21:45
新手上路 Posts 3 Credits 8
Originally posted by qasa at 2007-5-3 10:47 PM:

Suppose your file is 1.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."
Floor9 lxmxn Posted 2007-05-06 16:04
版主 Posts 4,938 Credits 11,386
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.
Floor10 chuier Posted 2007-05-06 19:49
新手上路 Posts 3 Credits 8
### 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".
Floor11 lxmxn Posted 2007-05-06 20:15
版主 Posts 4,938 Credits 11,386
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.
Floor12 jinrich Posted 2007-05-06 20:58
初级用户 Posts 11 Credits 26
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
Floor13 lxmxn Posted 2007-05-06 22:01
版主 Posts 4,938 Credits 11,386
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.
Floor14 a201 Posted 2007-05-08 04:26
新手上路 Posts 1 Credits 2
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 ]
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023