Jump to content

[Batch] How to delete 3 lines form a text file


Recommended Posts


We need more details. The first 3 lines? Lines at a specific position? Lines that contain specific text? 3 lines that are duplicate? ...

It's pretty hard to come up with a working solution to a still mostly unknown problem.

Either ways, I'd have a look at sed and awk first (unless you don't want of a 3rd party util) or I'd write a simple script that does it (totally trivial to do)

Link to comment
Share on other sites

This here are the exactly lines (19-24) that i try to remove from a Batch file:



ECHO.
ECHO Launching program XY...
START "" /WAIT "%SystemDrive%\XY.exe"
ECHO Deleting file "%SystemDrive%\XY.exe"...
DEL /Q "%SystemDrive%\XY.exe"

Edited by Outbreaker
Link to comment
Share on other sites

This here are the exactly lines (19-24)

Well, if deleting lines 19 to 24 is your final problem description then here's one way: get sed from here (in the zip). Then run this command:

sed 19,24d oldfile.ext > newfile.ext

Lines 19 to 24 will be omitted in the new file. There are other ways to do this if you prefer...

Link to comment
Share on other sites

Like I said, you have to describe the problem properly. So if it's not line numbers like you said it was before, how are we supposed to tell? From the few fake lines (unless you really have a program called XY? And that it's always going to be the one and only program you want removed?) you wrote which I bet won't be exactly that and which could be basically anywhere, not knowing what the rest of the file is like either? Nevermind that the 3 lines now seem to be more like 5. If you can't describe the problem or how it should work, then we just can't do anything. I'll wait for a proper description before offering another solution. "Just delete some lines that look kinda like this somewhere random in a file" isn't it as we can't just make a program that guesses which lines it is you want removed.

Link to comment
Share on other sites

I try to do a batch file that searches for this text in a file and if the text exists in there then it should be removed.

I like also to do this without an extra .exe if it doesn't work in Batch then a VBScript would do the jop also.



ECHO.
ECHO Launching program XY...
START "" /WAIT "%SystemDrive%\XY.exe"
ECHO Deleting file "%SystemDrive%\XY.exe"...
DEL /Q "%SystemDrive%\XY.exe"

Edited by Outbreaker
Link to comment
Share on other sites

And again, you should explain the problem you are having (as opposed to the way you think it should be solved).

There is no problem in removing lines, as long as they are "unique" in the batch, but since you probably have several lines like "ECHO." you cannot use "ECHO." as a simple argument for FIND or FINDSTR.

Also parsing the batch can be simple or difficult depending on it's contents.

An example is this one:

ECHO This is before

::BEGIN_REMOVE

ECHO.

ECHO Launching program XY...

START "" /WAIT "%SystemDrive%\XY.exe"

ECHO Deleting file "%SystemDrive%\XY.exe"...

DEL /Q "%SystemDrive%\XY.exe"

::END_REMOVE

ECHO This is after

Running something like this:

SET FLAG=0
FOR /F "tokens=* delims=" %%? IN ('TYPE my_original_batch.cmd') DO (
IF "%%?"=="::BEGIN_REMOVE" SET FLAG=1
IF "%%?"=="::END_REMOVE" SET FLAG=0
IF !FLAG!==0 ECHO %%?>>my_edited_batch.cmd
)
DEL my_original_batch.cmd
REN my_edited_batch.cmd my_original_batch.cmd

Will remove anything between the two "tags", but it simply won't work if you have redirection or piping symbols in your original batch.

jaclaz

Link to comment
Share on other sites

I have copyed you code 1.1 but for some reason it doesn't work.

Good :), that was essentially the reason why I posted it, to show you how giving partial informations is unuseful.

It was intentionally "broken" to see if instead of trying to understand how it can work and write your own, you would have - as expected - simply tried to run it from a copy and paste. :whistle:

Try this one ;):


@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET FLAG=0
FOR /F "tokens=* delims=" %%? IN ('TYPE my_original_batch.cmd') DO (
IF "%%?"=="::BEGIN_REMOVE" SET FLAG=1
IF "%%?"=="::END_REMOVE" SET FLAG=0
IF !FLAG!==0 ECHO %%?|FIND /V "::">>my_edited_batch.cmd
SET FLAG
)
DEL my_original_batch.cmd
REN my_edited_batch.cmd my_original_batch.cmd

Hint:

Check in the previous one spaces after the SET FLAG ;)

jaclaz

Link to comment
Share on other sites

As already mentioned, either provide the full correct and actual batch file you wish to be searched or don't expect a working solution.

The only suggestion I can make from the extremely poor examples you've provided is to search for and change any lines containing XY. As you can understand removing lines from a file which contain a string of two characters is not what I'd consider safe, especially as we haven't seen the entire file.

This is what I mean!

findstr/vc:" XY." /c:"\XY." yourbat.cmd>yournewbat.cmd

If your next response doesn't provide us with the information we've requested, I will close this topic!

Link to comment
Share on other sites

A "bad" way would be something like this:


@echo off
setlocal ENABLEDELAYEDEXPANSION
if "%1"=="" goto syntax
if "%2"=="" goto syntax
if "%3"=="" goto syntax
if "%4"=="" goto syntax
set /a nbl=0
if exist %2 del /q /f %2
for /f "delims=" %%? in (%1) do (
set /a nbl+=1

if !nbl! LSS %3 echo %%? >>%2
if !nbl! GTR %4 echo %%? >>%2

)
goto end
:syntax
echo %0 sourcefile destinationfile lowerlinenumber upperlinenumber
echo will create destinationfile with lines from lowerlinenumber to upperlinenumber removed
goto end
:end
endlocal

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...