给定行号,使用批处理(.bat)脚本,如何删除文本文件的行? [英] How to delete a line of a text file, given the line number using batch (.bat) script?
本文介绍了给定行号,使用批处理(.bat)脚本,如何删除文本文件的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想编写一个批处理脚本,用户可以在其中输入行号,该脚本将删除文本文件的该行.
I want to write a batch script where the user can enter the line number and the script will delete that line of a text file.
例如:tmp.txt
Eg: tmp.txt
1. aaa
2. bbb
3. ccc
4. ddd
我想在执行脚本并输入3时将tmp.txt设为
I want when i execute my script and user inputs 3, the tmp.txt to be
1. aaa
2. bbb
4. ddd
我正在尝试这样的事情:
I am trying something like this:
@ECHO OFF
SET /P LINENUMBER=Enter the Line No:
FOR /F "tokens=1* delims=[]" %%a IN ('FIND /n /v "" ^< tmp.txt') DO (
IF %%a equ %LINENUMBER% (
????????????????????
)
)
推荐答案
尝试一下:
@echo off & setlocal
set "InFile=Z:\Text.txt"
set "OutFile=Z:\Erg.txt"
set /p LineToDelete=
if exist "%OutFile%" del "%OutFile%"
for /f "tokens=1* delims=:" %%i in ('findstr /n $ "%InFile%"^|findstr /b "%LineToDelete%:"') do findstr /v /b /c:"%%j" "%InFile%">>"%OutFile%"
这篇关于给定行号,使用批处理(.bat)脚本,如何删除文本文件的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文