| 1. |
Solve : Removing all text after a certain line/character? |
|
Answer» 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. 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 ( 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. 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.Quote from: Dark Blade on June 07, 2007, 02:44:31 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. Quote from: Dark Blade on June 07, 2007, 02:44:31 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 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.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 Quote QuoteYou 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, 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.Quote from: Dark Blade on June 07, 2007, 03:25:00 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? 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!)Quote from: 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?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 You can just completely rewrite that file with the batch file....Quote from: Carbon Dudeoxide on June 07, 2007, 05:15:28 AM You can just completely rewrite that file with the batch file....huh? don't understandif the file you wanted to edit was a batch file like this: Code: [Select]@echo off echo hello echo how are you echo. pauseand 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 pausewow fantastic! but what has this got to do with what OP wants?Quote from: ghostdog74 on June 07, 2007, 08:06:08 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 Quote from: Carbon Dudeoxide on June 07, 2007, 08:08:43 AM Quote from: ghostdog74 on June 07, 2007, 08:06:08 AMwow and if the input file has 1000000000 lines, are you going to key in a batch file with 1000000000 echo lines ?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.wow fantastic! but what has this got to do with what OP wants?well....he wanted to replace a line of text.... Quote from: Dark Blade on June 07, 2007, 04:01:43 AM 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: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.txtQuote from: GuruGary on June 07, 2007, 11:43:41 PM Code: [Select]echo bananas >>food.txtLOL funny. That code puts the text at the end of the file you want to edit.Finally... Adding - Can do Replacing whole file - Can do (so many echos... ) 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. |
|