Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
removing a line from a text document
#1
how can i remove 1 line from a text document without opening it
#2
Kevin Wrote:how can i remove 1 line from a text document without opening it
could you please explain what your trying to do? I'm totally lost with this question, maybe if I understood, I could help, not being rude or anything, I just want to help you, I just don't understand.

------
edit: you mean to remove a sentence in a notepad .txt file without even opening it up ?
#3
i am checking a inside a file for a text then trying 2 remove that text from the file.But the catch is the file isn't open at all
#4
i know how 2 find the specific text inside the file just not how 2 remove it
#5
Kevin Wrote:i know how 2 find the specific text inside the file just not how 2 remove it
Confusedhock: can you post your code? I may be able to make a str for the text found & delete it. just need 2 get started off with the code :lol:


---
edit: because idk how 2 get the text from a non-open file, & it would make it much easier to expierment with deleteing the found text if I knew how to get the text. :lol:
---
edit again: you don't gotta post the whole code, just show me how to get the text of a file not opened, & I can come up with something most likely to delete the specified text.
#6
Code:
Copy      Help
str a b
b="remove this line"
a.getfile("$Desktop$\test.txt")
str s
foreach s a
,if b=s
,
,b.setfile("$Desktop$\test.txt" -1)
#7
Code:
Copy      Help
str a b
b="remove this line"
a.getfile("$Desktop$\test.txt")
str s
foreach s a
,if b=s
;;;;;;;;;;;need code 2 remove str b from file    
,b.setfile("$Desktop$\test.txt" -1)
#8
I have never tried something like this before but I would say try using something like this:

For end of macro:

,if b=s
,,a.findreplace(b)
,a.setfile("$Desktop$\test.txt" -1)


I don't know if this would work or not but it might be worth a shot.
Taking on Quick Macros one day at a time
#9
not quite. it did delete the entry after it added everything else again but the orignal line was still there
#10
ok i managed 2 delete the line but it is replace but a blank line now
#11
i got it had 2 delete the file first
ty for your help
#12
Anytime.
Taking on Quick Macros one day at a time
#13
ok it works but only problem is it leaves a blank line in the file
#14
this is the whole code i am using for testing
Code:
Copy      Help
str a b
b="remove this line"
a.getfile("$Desktop$\test.txt")
str s
foreach s a
,if b=s
,,a.findreplace(b)
,,del "$Desktop$\test.txt"
,,a.setfile("$Desktop$\test.txt" -1)
#15
Check this post out it will show you how to delete a line without leaving a blank line.
http://www.quickmacros.com/forum/showthread.php?tid=497
#16
Code:
Copy      Help
str a
a.getfile("$Desktop$\test.txt")
;a.RemoveLineN(4);; deletes line 5 from file
a.RemoveLineN(numlines(a)-1);; deletes last line of file
a.setfile("$Desktop$\test.txt")

You must have Member function str.RemoveLineN
(To add new Member function) while QM window is active press and release (Ctrl+Alt+N) this will add a new Member function make sure you name the new Member function (str.RemoveLineN), and add the below code to it.

str.RemoveLineN
Code:
Copy      Help
function# lineindex [nlines]

;Removes specified line(s).
;Returns index of first character of lineindex-th line, or -1 if lineindex is too big.


;lineindex - zero-based line index.
;nlines - number of lines to remove. Default or 0: 1 line.


;EXAMPLE
;str s="zero[]one[]two"
;s.RemoveLineN(1)
;out s



if(nlines<1) nlines=1
int i=findl(this lineindex)
if(i>=0) this.remove(i findl(this+i nlines))
ret i
#17
ty for your rsponse i looked at that and tried it and again for some reason i have 2 dlete the file and resave it also had 2 add a code 2 count the lines but that was no biggie.if i don't delete the file it adds every other name again and doesn't delete the entry i wanted except on the when it adds all the lines in the file again 2 the file
#18
If you use flag (-1) with setfile it will append the text to the file not replace it like you're wanting to do, don't use any flag at all. As far as the counting I dont know why you would have to do that, numlines() will do that for you.
#19
ok ty yeah once i removed the flag it works without deleting but i have 2 get the line count cause the entry could be on any line. maybe line 5 our could be line 25 u never know.so i just made it count the line so i had the line number 2 delete.Cause it won't let me use anything but a number or an integer in this line
a.RemoveLineN(x)<-----x being an integer
#20
Sounds like you have it then ?
#21
yes i do tyvm.I didn't use the numblines line cause i wasn't sure how 2 implement it.So that's why i had 2 adapt it 2 count the lines
#22
Glad I could help.
#23
i'm having a problem saving a file if the text begins with things like this
**==
^Smile^
any thoughts why?
#24
what is happening is if the text of the window starts with characters like that i can't save the file name as that text of the window
#25
Member function str.ReplaceInvalidFilenameCharacters. Create it using menu File -> New -> New Member Function.
Code:
Copy      Help
function# [$replaceto]

;Some characters cannot be used in file names. This function replaces
;these characters to valid characters or just removes them.


;EXAMPLE
;str s="file*\[9]name.txt"
;s.ReplaceInvalidFilenameCharacters("_")
;out s
;s-"$desktop$\"
;s.setfile(s)



replacerx("[\\/|<>?*:''[1]-[31]]" replaceto)


Forum Jump:


Users browsing this thread: 1 Guest(s)