Computer Hope

Microsoft => Microsoft DOS => Topic started by: Dark Blade on June 06, 2007, 04:35:09 AM

Title: Removing all text after a certain line/character
Post by: Dark Blade 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.
Title: Re: Removing all text after a certain line/character
Post by: contrex 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?



Title: Re: Removing all text after a certain line/character
Post by: Dark Blade 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)
Title: Re: Removing all text after a certain line/character
Post by: contrex 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.


Title: Re: Removing all text after a certain line/character
Post by: Dark Blade 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.
Title: Re: Removing all text after a certain line/character
Post by: contrex 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.
Title: Re: Removing all text after a certain line/character
Post by: ghostdog74 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
Title: Re: Removing all text after a certain line/character
Post by: Dark Blade 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.
Title: Re: Removing all text after a certain line/character
Post by: contrex 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
Title: Re: Removing all text after a certain line/character
Post by: Dark Blade 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.
Title: Re: Removing all text after a certain line/character
Post by: contrex 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?
Title: Re: Removing all text after a certain line/character
Post by: Dark Blade 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!)
Title: Re: Removing all text after a certain line/character
Post by: ghostdog74 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
Title: Re: Removing all text after a certain line/character
Post by: Carbon Dudeoxide on June 07, 2007, 05:15:28 AM
You can just completely rewrite that file with the batch file....
Title: Re: Removing all text after a certain line/character
Post by: ghostdog74 on June 07, 2007, 07:59:14 AM
You can just completely rewrite that file with the batch file....
huh? don't understand
Title: Re: Removing all text after a certain line/character
Post by: Carbon Dudeoxide on June 07, 2007, 08:03:34 AM
if the file you wanted to edit was a batch file like this:
Code: [Select]
@echo off
echo hello
echo how are you
echo.
pause
and say you wanted to add a "echo i'm good", do this:
Code: [Select]
@echo off
del "C:\path\file.bat"
echo @echo off >>"C:\path\file.bat"
echo echo hello >>"C:\path\file.bat"
echo echo how are you >>"C:\path\file.bat"
echo echo. >>"C:\path\file.bat"
echo echo I'm good >>"C:\path\file.bat"
echo pause >>"C:\path\file.bat"

This would delete the old one and create a new file with the added sentence.
The finished product would be:
Code: [Select]
@echo off
echo hello
echo how are you
echo.
echo I'm good
pause
Title: Re: Removing all text after a certain line/character
Post by: ghostdog74 on June 07, 2007, 08:06:08 AM
wow fantastic! but what has this got to do with what OP wants?
Title: Re: Removing all text after a certain line/character
Post by: Carbon Dudeoxide on June 07, 2007, 08:08:43 AM
wow fantastic! but what has this got to do with what OP wants?
well....he wanted to replace a line of text....
This is another way to 'replace' something  :P
Title: Re: Removing all text after a certain line/character
Post by: ghostdog74 on June 07, 2007, 08:37:52 AM
wow fantastic! but what has this got to do with what OP wants?
well....he wanted to replace a line of text....
This is another way to 'replace' something  :P
wow and if the input file has 1000000000 lines, are you going to key in a batch file with 1000000000 echo lines ?
Title: Re: Removing all text after a certain line/character
Post by: contrex on June 07, 2007, 09:08:09 AM
The whole idea of text processing in batch files has now been explained exhaustively in about 6 different ways, and I would have thought that if a person had not "got" it by now, it would be time to find a less taxing hobby.

Title: Re: Removing all text after a certain line/character
Post by: GuruGary on June 07, 2007, 11:43:41 PM
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
Adding a line of text to the end of a file is easy.  If your file is named food.txt, then your code would be:
Code: [Select]
echo bananas >>food.txt
Title: Re: Removing all text after a certain line/character
Post by: Carbon Dudeoxide on June 08, 2007, 12:00:43 AM
Code: [Select]
echo bananas >>food.txt
LOL funny.
That code puts the text at the end of the file you want to edit.
Title: Re: Removing all text after a certain line/character
Post by: Dark Blade on June 08, 2007, 12:30:32 AM
Finally...

Adding - Can do
Replacing whole file - Can do (so many echos... :P)
Deleting

I just made something that deletes a file if it exists, then add lines to the new file (like what Carbon Dudeoxide did, but it adds the output of my batch file).


I'm going to start making something that deletes all text after a specified line.