|
caiyiqun
初级用户
 
积分 25
发帖 11
注册 2007-4-3
状态 离线
|
『楼 主』:
[再次求助]新手请教批处理修改文本文件内容问题
使用 LLM 解释/回答一下
如偶一个文件,内容为(不括引号)" ABCDEFGHH PPP EGFAFDAF"
我要查找PPP并删除PPP和其后面的全部内容,没找到就跳过!
如何搞定?
先谢谢了!再次厚着脸皮发帖了!:P
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
|
|
2007-5-3 22:31 |
|
|
qasa
高级用户
   
积分 959
发帖 311
注册 2006-4-11 来自 广东-LianJiang
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
假设你那个文件为1.txt
for /f "tokens=1 delims=PPP" %%i in (1.txt) do (
echo %%i >2.txt
)
Suppose your file is 1.txt
for /f "tokens=1 delims=PPP" %%i in (1.txt) do (
echo %%i >2.txt
)
|

κχυμγνξοθπρωψιαδλεηφβτζσ┬╀┾┳┞┯┰┱┣┲┳╂╁│├┟┭┠这是什么??这就是我的人生 |
|
2007-5-3 22:47 |
|
|
caiyiqun
初级用户
 
积分 25
发帖 11
注册 2007-4-3
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
谢谢!!
但是当文件有回车苻时,只能输出回车前那一行,后面的里都不能输入到2.txt中
如内容为(不括引号)" ABCDEFGHH
44
PPP
EGFAFDAF"
只能输出第一行!!
如何改进?
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?
|
|
2007-5-4 14:20 |
|
|
caiyiqun
初级用户
 
积分 25
发帖 11
注册 2007-4-3
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
FOR 命令好深呀,不懂,正在努力学习中
The FOR command is really deep. I don't understand it and am studying hard.
|
|
2007-5-4 14:21 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
效率比较低。
@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
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
```
|
|
2007-5-4 17:27 |
|
|
caiyiqun
初级用户
 
积分 25
发帖 11
注册 2007-4-3
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
Originally posted by lxmxn at 2007-5-4 04:27 AM:
效率比较低。
@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\ ...
没话说,服了!!棒!
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!
|
|
2007-5-4 20:29 |
|
|
caiyiqun
初级用户
 
积分 25
发帖 11
注册 2007-4-3
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
再问下,如果是删除 PPP 之前的全部文本,包括空格回车之类的,要怎么改?
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?
|
|
2007-5-4 21:59 |
|
|
chuier
新手上路

积分 8
发帖 3
注册 2007-5-4
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
Originally posted by qasa at 2007-5-3 10:47 PM:
假设你那个文件为1.txt
for /f "tokens=1 delims=PPP" %%i in (1.txt) do (
echo %%i >2.txt
)
能帮我看看我写的这段么?
@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
)
为什么显示的是"ECHO is off."
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 |
|
2007-5-5 21:45 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
Originally posted by chuier at 2007-5-5 21:45:
能帮我看看我写的这段么?
@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 ...
第一个for里面的%F%代表的是一个文件,如果没有这个文件,%%~fi 也会扩展它的全路径,最后导致第二个for命令可能因为找不到文件而退出。最好用 for /f %%a in ('dir %F%') do …… 的形式,来判断是不是有%F%这个文件。
我测试的时候是正常的,可以替换,但是总的来说,这个批处理不够严谨。
另外,提醒一下,新问题单独发个主题出来。
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.
|
|
2007-5-6 16:04 |
|
|
chuier
新手上路

积分 8
发帖 3
注册 2007-5-4
状态 离线
|
『第 10 楼』:
能详细解释一下么?
使用 LLM 解释/回答一下
提问:
1 for /f "delims=" %%a in (1.txt) do (echo %%a|find /i "PPP" >nul 2>&1)命令中">&1"怎么理解?
2 set str=batchering什么含义?
等待答复............
### 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 |
|
2007-5-6 19:49 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
Re chuier:
1、将句柄2(StdErr)的输出定向到句柄1的输入中,而句柄1的输入又被输出到空设备(Nul)了,所以 “2>&1” 可以屏蔽命令的错误输出(StdErr),类似于 2>nul ;
2、这里定义一个 str 是为 if not defined str 语句服务的,表示如果没有定义 str 变量的话,就执行后面的命令。
简单的解释这点了,想要详细了解它们的用法,可以搜索一下论坛相关的讨论。
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.
|
|
2007-5-6 20:15 |
|
|
jinrich
初级用户
 
积分 26
发帖 11
注册 2006-12-15
状态 离线
|
『第 12 楼』:
re
使用 LLM 解释/回答一下
for /f "tokens=1 delims=PPP" %%i in (1.txt) do (
echo\%%i >>2.txt
)
比较简单,只不过qasa的 echo %%i 〉2.txt 应改成echo\%%i >>2.txt
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 |
|
2007-5-6 20:58 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
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
)
比较简单,只不过qasa的 echo %%i 〉2.txt 应改成echo\%%i >>2.txt
这个不行,只能读取字符 P 前面的字符,不能处理 PPP 前面的字符。
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.
|
|
2007-5-6 22:01 |
|
|
a201
新手上路

积分 2
发帖 1
注册 2007-3-2
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
Originally posted by caiyiqun at 2007-5-3 10:31 PM:
如偶一个文件,内容为(不括引号)" ABCDEFGHH PPP EGFAFDAF"
我要查找PPP并删除PPP和其后面的全部内容,没找到就跳过!
如何搞定?
先谢谢了!再次厚着脸皮发帖了!:P
那如果把PPP改为AAA如何改?保留后面全部内容
Last edited by a201 on 2007-5-8 at 04:27 AM ]
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 ]
|
|
2007-5-8 04:26 |
|