How to delete a line of a text file, given the line number using batch (.bat) script?

点点圈 提交于 2019-11-29 23:53:04

问题


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.

Eg: tmp.txt

1. aaa
2. bbb
3. ccc
4. ddd

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% (
                  ????????????????????
    )
)

回答1:


You cannot modify the file directly. You must create a new file with the desired content and then replace the original file with the new file.

You create the new file by redirecting output and ECHOing the desired content. I use ECHO(%%b in case %%b is empty because the line was blank. ECHO %%b would produce ECHO is off. if b is empty. Most people use ECHO., but that can fail under esoteric conditions. ECHO( will never fail.

You use MOVE to replace the original file with the new.

You can eliminate the need to check for the number within your FOR /F loop by using FINDSTR after FIND to filter out the unwanted line.

Note that your code, as written so far, will strip any leading [ and ] characters from each line. Because the line filter is applied before the FOR iteration, you can improve the situation slightly by setting DELIMS to ] only.

@ECHO OFF
SET /P LINENUMBER=Enter the Line No:
>tmp.txt.new (
  FOR /F "tokens=1* delims=]" %%a IN (
    'FIND /n /v "" ^<tmp.txt^|FINDSTR /VLB "[%LINENUMBER%]"'
  ) DO ECHO(%%b
)
>nul MOVE /y tmp.txt.new tmp.txt

If any of your original lines already begin with ] then you can use FINDSTR instead of FIND to introduce the line number. That uses nn: format instead of [nn]. This solution will strip leading : from your lines.

@ECHO OFF
SET /P LINENUMBER=Enter the Line No:
>tmp.txt.new (
  FOR /F "tokens=1* delims=:" %%a IN (
    'FINDSTR /n "^" tmp.txt^|FINDSTR /VLB "%LINENUMBER%:"'
  ) DO ECHO(%%b
)
>nul MOVE /y tmp.txt.new tmp.txt

If your text file contains lines that begin both with : and ] then you can't use FOR /F to parse out the line number. Instead you use search and replace. Delayed expansion is toggled on and off within the loop to protect any ! that may exist in the file.

@ECHO OFF
setlocal disableDelayedExpansion
SET /P LINENUMBER=Enter the Line No:
>tmp.txt.new (
  FOR /F "delims=" %%a IN (
    'FINDSTR /n "^" tmp.txt ^| FINDSTR /VLB "%LINENUMBER%:"'
  ) DO (
    set "ln=%%a"
    setlocal enableDelayedExpansion
    echo(!ln:*:=!
    endlocal
  )
)
>nul MOVE /y tmp.txt.new tmp.txt



回答2:


try out this:

@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%"


来源:https://stackoverflow.com/questions/12736509/how-to-delete-a-line-of-a-text-file-given-the-line-number-using-batch-bat-sc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!