Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Removing all text after a certain line/character  (Read 21607 times)

0 Members and 1 Guest are viewing this topic.

Dark Blade

    Topic Starter
  • Forum Gaming Master


  • Adviser

    Thanked: 24
    • Yes
  • Experience: Experienced
  • OS: Windows XP
Removing all text after a certain line/character
« on: June 06, 2007, 04:35:09 AM »
Well, I'm trying to remove all the text after a certain character by using a batch file. For example, if I want to remove all text after the line end in the file test.txt, I've got a tiny bit of code, but I don't know what to do next.

Code: [Select]
for /f %i in ('findstr /n end C:\test.txt') do (
REM remove all text after end
)

I think that I could remove the :end from %i (%i echos 1:end, because end is on the first line), and then do for /f again, then use "skip:%i" or something.... anyway, I'm just having trouble finding something that removes text from a file, as well as removing the :end.

contrex

  • Guest
Re: Removing all text after a certain line/character
« Reply #1 on: June 06, 2007, 05:01:57 AM »
could we perhaps see the file test.txt?

PS I like that "REM remove all text after end". That's like "REM relational database goes here"... A good start... Shows you're making an effort... :)

Do you mean you want to do this?

Before:-

apples
pears
bananas
end
mangos
lemons

After:-

apples
pears
bananas
end

What confuses me, is that the obvious, the blindingly obvious way to do this is to load the file into a text editor such as Notepad, highlight the text you don't want, and press "delete". I am wondering if you need to do this for homework, or possibly just because you fancy learning how to do batch programming. I'll be charitable and assume the latter, but in either case it would be unhelpful of anyone to just go ahead and write a batch file for you. You would not learn anything that way.

If you like batch programming, and you find it interesting, and you are a curious sort of person, you can hunt around just on this forum and find the answers that you need. If none of those things are true, why are you bothering?

What I am willing to do is sketch out the steps you need (in my opinion) to follow.

1. examine the first line in the file.
2. If it's not the "end" line, write the line out to another file.
3. If it is the "end" line, write the line out and jump out of the loop. (hint: to a label)
4. If it wasn't the end line, get the next line.
5. Keep going until you either find the "end" line or the end of the file.



Maybe you could start by writing a batch that just echoes each line of a file, and only then think about doing some decision making?



« Last Edit: June 06, 2007, 06:48:44 AM by contrex »

Dark Blade

    Topic Starter
  • Forum Gaming Master


  • Adviser

    Thanked: 24
    • Yes
  • Experience: Experienced
  • OS: Windows XP
Re: Removing all text after a certain line/character
« Reply #2 on: June 07, 2007, 12:49:59 AM »
Yes, I could just go into the file and delete abc manually, but that's not the point here. The point is that I'm trying to learn how to do this, so I can move to harder stuff.

OK, I just made a code that echos every line of a file one at a time, except that I have to specify how many lines. I just don't know how to remove all lines after that.
Code: [Select]
@echo off
set /a SKP=0

:begin
if %SKP% GTR 26 goto end
if %SKP% GTR 0 goto second

:first

for /f "delims=: " %%i in ('findstr /n . "C:\test.txt"') do (

echo %%i
goto second
)

:second
set /a SKP=%SKP%+1
for /f "delims=:  skip=%SKP%" %%i in ('findstr /n . "C:\test.txt"') do (

echo %%i
goto begin
)

:end
echo.
@pause


The contents of test.txt is just the letters of the alphabet on different lines (remove delims:  to see the contents).

Just wondering, how do I check how many lines there are in a file without knowing the contents? (I can check how many there are if I know what the last line/character is)

contrex

  • Guest
Re: Removing all text after a certain line/character
« Reply #3 on: June 07, 2007, 12:59:24 AM »
This is homework, I am sure. Why do I keep seeing all these lines like this

Quote
for /f "delims=: " %%i in ('findstr /n . "C:\test.txt"') do (

I would not use that to loop through a file line by line.

OK I will slightly relax my rule and help you with your homework.

If you are going to blindly copy code from wherever, copy good code! What is wrong with this?...

Quote
for /F "delims=" %%L in (C:\test.txt) do (
   echo %%L
)



Quote
Just wondering, how do I check how many lines there are in a file without knowing the contents? (I can check how many there are if I know what the last line/character is)

use set /a to put a variable to zero. read through the file one line at a time, each time add 1 to the variable using set /a, when the loop finishes you know how many lines it has.



Dark Blade

    Topic Starter
  • Forum Gaming Master


  • Adviser

    Thanked: 24
    • Yes
  • Experience: Experienced
  • OS: Windows XP
Re: Removing all text after a certain line/character
« Reply #4 on: June 07, 2007, 02:44:31 AM »
Look, as much as you don't want to believe it, this is NOT homework! Also, I made this code myself, not copy it.

With that out of the way, I am still looking for a way to remove text through batch. And the reason that I use findstr is to find out what line end (or whatever a am looking for) is on.

Current Revision:
Code: [Select]
@echo off
set /a LNE=0
set /a DOT=1
:find
set /a LNE=%LNE%+1
for /f "skip=%LNE%" %%m in ('findstr /n . "C:\test.txt"') do (
cls
if %DOT% EQU 1 echo Searching line amount.
if %DOT% EQU 2 echo Searching line amount..
if %DOT% EQU 3 echo Searching line amount...
if %DOT% LSS 3 set /a DOT=%DOT%+1
if %DOT% EQU 3 set /a DOT=1


goto find
)

cls
echo Running Program...
echo.
echo.

set /a SKP=0

:begin
if %SKP% GTR %LNE% goto end
if %SKP% GTR 0 goto second

:first

for /f %%i in ('findstr /n . "C:\test.txt"') do (

echo %%i
goto second
)

:second
set /a SKP=%SKP%+1
for /f "skip=%SKP%" %%i in ('findstr /n . "C:\test.txt"') do (

echo %%i
goto begin
)

:end
echo.
echo Done!
echo.
@pause
)

:end
echo.
echo Done!
echo.
@pause

Heh. I even added a little dot dot dot thing for fun.

Anyway, I am still left with the original problem of how to delete text in a batch file. That is my only question.

contrex

  • Guest
Re: Removing all text after a certain line/character
« Reply #5 on: June 07, 2007, 02:47:58 AM »
Anyway, I am still left with the original problem of how to delete text in a batch file. That is my only question.

You cannot directly modify a text file stored on disk easily. You have to make a copy which has the changes you want, then delete or rename the original, then rename or copy the new file back to the first filename. This is what in fact happens when you open a text file in an editor such as Notepad, make changes, and save the changed file, only it's all hidden from you, so that you only need the concept of "altering" just the one file. When you dig deeper, as you are doing, you need to appreciate such things.
« Last Edit: June 07, 2007, 03:05:35 AM by contrex »

ghostdog74



    Specialist

    Thanked: 27
    Re: Removing all text after a certain line/character
    « Reply #6 on: June 07, 2007, 02:57:50 AM »
    Anyway, I am still left with the original problem of how to delete text in a batch file. That is my only question.
    a rough vbscript solution:
    Code: [Select]
    Const ForAppending=2
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("C:\temp\test1.txt", 1)
    Set objOutFile = objFSO.OpenTextFile("c:\temp\temp.txt", ForAppending,True)
    Do Until objFile.AtEndOfStream
        strNextLine = objFile.ReadLine
        If InStr(strNextLine,"end") = 1 Then       
            objOutFile.Close
            objFile.Close
            objFSO.DeleteFile("C:\temp\test1.txt")
    objFSO.MoveFile "C:\temp\temp.txt" , "C:\temp\test1.txt"
            WScript.Quit
        Else
            objOutFile.WriteLine(strNextLine)
        End If
    Loop

    Dark Blade

      Topic Starter
    • Forum Gaming Master


    • Adviser

      Thanked: 24
      • Yes
    • Experience: Experienced
    • OS: Windows XP
    Re: Removing all text after a certain line/character
    « Reply #7 on: June 07, 2007, 03:03:12 AM »
    So, contrex, all in all, is it impossible (or close to) to make a batch file that, as you put it, does this:

    Before:-

    apples
    pears
    bananas
    end
    mangos
    lemons

    After:-

    apples
    pears
    bananas
    end

    You say that you have to make the changes manually to a copy of the file, and because of that I can't make a file that automatically deletes text.

    Well, anyway, thanks for the help.

    contrex

    • Guest
    Re: Removing all text after a certain line/character
    « Reply #8 on: June 07, 2007, 03:12:49 AM »
    I repeat,

    Quote
    You cannot directly modify a text file stored on disk easily. You'd need to be able to alter the disk sectors directly, and update the file size data etc. You have to make a copy which has the changes you want, then delete or rename the original, then rename or copy the new file back to the first filename. This is what in fact happens when you open a text file in an editor such as Notepad, make changes, and save the changed file, only it's all hidden from you, so that you only need the concept of "altering" just the one file. When you dig deeper, as you are doing, you need to appreciate such things in order to make any progress.

    Quote
    You say that you have to make the changes manually to a copy of the file, and because of that I can't make a file that automatically deletes text.

    I didn't say that at all. (Is English your first language?) I have said that you can write a batch file that appears to "alter" a text file, ie, before you run the file, there will be a file called fruits.txt that contains the following lines

    apples
    pears
    bananas
    end
    mangos
    lemons

    And after the batch file has finished, there will still be a file called fruits.txt but it will have a later file modified date than the first one, and a different size, and it will contain these lines

    apples
    pears
    bananas
    end

    Dark Blade

      Topic Starter
    • Forum Gaming Master


    • Adviser

      Thanked: 24
      • Yes
    • Experience: Experienced
    • OS: Windows XP
    Re: Removing all text after a certain line/character
    « Reply #9 on: June 07, 2007, 03:25:00 AM »
    Quote
    Quote
    You say that you have to make the changes manually to a copy of the file, and because of that I can't make a file that automatically deletes text.

    I didn't say that at all. (Is English your first language?) I have said that you can write a batch file that appears to "alter" a text file, ie, before you run the file, there will be a file called fruits.txt that contains the following lines

    Well, sorry if I misinterpreted you. I thought that was what you meant when you said:
    Quote
    You have to make a copy which has the changes you want

    I didn't see your edit (I posted before it was written), so I didn't grasp everything you meant. Now I understand it.

    contrex

    • Guest
    Re: Removing all text after a certain line/character
    « Reply #10 on: June 07, 2007, 03:42:22 AM »
    so I didn't grasp everything you meant. Now I understand it.

    So now are you going to get coding and we can help you along?

    Dark Blade

      Topic Starter
    • Forum Gaming Master


    • Adviser

      Thanked: 24
      • Yes
    • Experience: Experienced
    • OS: Windows XP
    Re: Removing all text after a certain line/character
    « Reply #11 on: June 07, 2007, 04:01:43 AM »
    I may sound repeatative, but I don't know how to alter text into files. How do you do that?

    Is there any command that can alter/manipulate text? If I get that, then I'll start with some easy, like adding a 1 to the end of each file. Like this:

    Before:-

    apples
    pears
    ADD

    After:-

    apples
    pears
    ADD
    bananas


    I don't what exact code, just something to push me in the right direction (because making the code is half the fun!)

    ghostdog74



      Specialist

      Thanked: 27
      Re: Removing all text after a certain line/character
      « Reply #12 on: June 07, 2007, 05:12:32 AM »
      I may sound repeatative, but I don't know how to alter text into files. How do you do that?
      you can alter text in a number of ways
      1) looping over the original file using for loop, changing whatever text then echoing the changed line out to another file
      2) using available tools, like edlin, findstr , find, type, more , grep etc etc...plus using the  >> , > redirection operators
      3) using other languages that are more suited for text processing, eg perl/python/awk/sed/vbscript, even Java/powershell etc etc etc . They have more string functions than what DOS batch can offer you and you can have more control over what you are doing
      4) others etc etc

      Carbon Dudeoxide

      • Global Moderator

      • Mastermind
      • Thanked: 169
        • Yes
        • Yes
        • Yes
      • Certifications: List
      • Experience: Guru
      • OS: Mac OS
      Re: Removing all text after a certain line/character
      « Reply #13 on: June 07, 2007, 05:15:28 AM »
      You can just completely rewrite that file with the batch file....

      ghostdog74



        Specialist

        Thanked: 27
        Re: Removing all text after a certain line/character
        « Reply #14 on: June 07, 2007, 07:59:14 AM »
        You can just completely rewrite that file with the batch file....
        huh? don't understand