|
| Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free! |
Batch File To Delete Line
|
Original Message
|
Name: gameking66
Date: July 8, 2007 at 21:45:19 Pacific
Subject: Batch File To Delete LineOS: Windows VistaCPU/Ram: 1.67GHz 2GB RAMModel/Manufacturer: HP Pavillion dv9230us |
Comment: Is it possible for a batch script to delete a single line in a txt file? example: my.txt contains the following: ab cd is it possible to delete cd using batch?
Report Offensive Message For Removal
|
|
Response Number 2
|
Name: ghostdog
Date: July 8, 2007 at 23:24:42 Pacific
|
Reply: (edit)here's a vbscript. assuming the file is not in gigabyte sizes.. [code] Dim objFSO,objFile,fileCount,allData,NumberOfLines Const ForReading=1 Set objFSO=CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile("c:\test.txt", ForReading) allData=Split(objFile.ReadAll,vbCrLf) NumberOfLines = objFile.Line objFile.Close For fileCount = 0 to NumberOfLines - 2 WScript.Echo allData(fileCount) Next Set objFSO=Nothing [/code] on the command prompt, type cscript /nologo myscript.vbs > output
Report Offensive Follow Up For Removal
|
|
Response Number 4
|
Name: gameking66
Date: July 10, 2007 at 00:10:57 Pacific
|
Reply: (edit)My File Contains The Following:( X = RANDOM ) QXXXXXX XXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXX @XXXXXX The file must delete "@XXXXXX", but leaving the rest untouched. I need user input however which is why i asked for batch. I need the user to be able to input the name of the file to be changed!
Report Offensive Follow Up For Removal
|
|
Response Number 6
|
Name: Mechanix2Go
Date: July 10, 2007 at 11:55:41 Pacific
|
Reply: (edit)This should work UNLESS the file has chars with 'special meaning' to the OS. Those are usually: <>|%^&! and maybe some others which don't come to mund. ::== delLAST.bat :: del last line of txt :: NG if txt has <>|!% etc @echo off > outfile setlocal EnableDelayedExpansion set /p FN=file name? type !FN! | find /c /v "" > %temp%\# set /p L=<%temp%\# type !FN! | find /n /v "" > %temp%\## for /f "tokens=1,* delims=[]" %%c in (%temp%\##) do ( if %%c equ !L! goto :eof if '%%d'=='' ( echo. >> outfile ) else ( echo %%d >> outfile ) ) :: DONE ===================================== If at first you don't succeed, you're about average.M2
Report Offensive Follow Up For Removal
|
|
Response Number 7
|
Name: Mechanix2Go
Date: July 10, 2007 at 14:31:44 Pacific
|
Reply: (edit)Or more simply. [Simpler is better.] Same precaution applies. ::== del LAST line with FIND @echo off > outfile setlocal EnableDelayedExpansion set /p FN=file name? for /f "tokens=* delims= " %%c in (!FN!) do ( set str=%%c ) find /v "!str!" < !FN! > outfile :: DONE
===================================== If at first you don't succeed, you're about average.M2
Report Offensive Follow Up For Removal
|
|
Response Number 8
|
Name: FishMonger
Date: July 10, 2007 at 18:10:46 Pacific
|
Reply: (edit)Here's a Perl 1 liner option. c:\>perl -MTie::File -e "tie @f,'Tie::File', 'my.txt'; pop @f; untie @f;" ================================================= Or, if you want it in script form. #!perl use Tie::File; tie @f,'Tie::File', $ARGV[0]; pop @f; untie @f; ------------ executed as: c:\>delete_last_line.pl my.txt However, none of the proposed solutions meet all of the requested (or implied) requirements.
Report Offensive Follow Up For Removal
|
|
Response Number 9
|
Name: gameking66
Date: July 10, 2007 at 19:31:01 Pacific
|
Reply: (edit)mechanix2go, you have outdone yourself! THANK YOU! 1 more thing tho. is it possible to check if there is an @ symbol on the last line before changing the file, to make sure it was not already changed? thanks again mechanix!
Report Offensive Follow Up For Removal
|
|
Response Number 10
|
Name: Mechanix2Go
Date: July 10, 2007 at 22:07:40 Pacific
|
Reply: (edit)Not sure how you're defining already changed, but this will check to see if the last line contains an @. ::== ::== ::== del LAST line with FIND :: del last line of txt :: NG if txt has <>|!% etc @echo off > outfile setlocal EnableDelayedExpansion set /p FN=file name? for /f "tokens=* delims= " %%c in (!FN!) do ( set str=%%c ) echo !str! | find "@" > nul if errorlevel 1 (echo there's no @ ) else ( echo last line has an @ ) find /v "!str!" < !FN! > outfile ::==
===================================== If at first you don't succeed, you're about average.M2
Report Offensive Follow Up For Removal
|

Post Locked
This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
Go to Programming Forum Home
|
|
|