AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Moving a line of text from one .txt file to another

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Gakusei



Joined: 09 Jan 2008
Posts: 15

PostPosted: Thu Jan 17, 2008 4:31 pm    Post subject: Moving a line of text from one .txt file to another Reply with quote

Is there a way for AHK to read a line from a text file and move it (copy to destination file & delete from source file) to another text file? Specifically, I'd like to know if this can be done one line at a time in case the process terminates for whatever reason so that I can continue where I left off.

Thanks
Back to top
View user's profile Send private message
Sakurako



Joined: 10 May 2007
Posts: 149
Location: China/ Canada

PostPosted: Thu Jan 17, 2008 7:58 pm    Post subject: Reply with quote

Try using FileReadLine/ Loop ~
Code:
FileReadLine, OutputVar, Filename, LineNum
Code:
Loop, Read, InputFile [, OutputFile]
Code:
FileName = testing.ahk
ErrorReq = 1500
Gui, Add, Button, x10 gReadFile, Read
Gui, Add, Button, xp+125 gGenerateFile, Generate
Gui, Add, Edit, x10 vFileRec w645 R30
Gui, Show
return

ReadFile:
Loop, read, %FileName%
{
 Line%A_Index% := A_LoopReadLine
}
return

GenerateFile:
Loop
{
 If Line%A_Index%
 {
  FileRec := FileRec "`n" Line%A_Index%
  If A_Index = 1
   FileRec := Line1
  Error := 0
 }
 Else
  Error += 1
 If Error > %ErrorReq%
  break
}
GuiControl,, FileRec, %FileRec%
return
Back to top
View user's profile Send private message Visit poster's website
Predated



Joined: 06 Nov 2007
Posts: 42

PostPosted: Fri Jan 18, 2008 12:22 am    Post subject: Reply with quote

Code:
Loop read, name_of_source_file.ext, name_of_dest_file.ext
{
    FileAppend %A_LoopReadLine%`n   
}


the delete bit I have a couple ideas on how to pull off, but nothing elegant, perhaps someone else might
Back to top
View user's profile Send private message
Gakusei



Joined: 09 Jan 2008
Posts: 15

PostPosted: Fri Jan 18, 2008 4:59 pm    Post subject: Reply with quote

The copying part works great.

Does anyone have any suggestions on how to delete the lines from the source file as the script copies each line, though?
Back to top
View user's profile Send private message
Gakusei



Joined: 09 Jan 2008
Posts: 15

PostPosted: Fri Jan 18, 2008 6:38 pm    Post subject: Reply with quote

Looks like Titan's FileDeleteLine() function does the job.
Found @ http://www.autohotkey.com/forum/topic25438.html. Thanks to HugoV for finding it.

This is the code I needed (in case anyone needs it too):

Code:
; sourcefile needed to work with FileDeleteLine()
; if you are going to use a folder other than
; the one the script is in.
; destinationfile is there for consistency.
sourcefile=Inputlist.txt
destinationfile=Outputlist.txt

; count the number of lines in the sourcefile
i=0
Loop, read, %sourcefile%
   i++

; go through the sourcefile line-by-line,
; copy the line over to the destination file,
; and delete that line from the sourcefile.
Loop, %i%
{
   FileReadLine, line, %sourcefile%, 1
   line=%line%`n
   FileAppend, %line%, %destinationfile%
   FileDeleteLine(sourcefile, 1, "`n")
}

; the miracle function written by Titan
FileDeleteLine(file, line , eol) {
   ; eol = end of line character
   FileRead, src, %file%
   If line = 1 ; offset should be 0 for first line:
      start = 0
   Else {
      line-- ; since there is no linefeed at the start (i.e. 0 based index)
      StringGetPos, start, src, %eol%, L%line% ; find position of specified line
      If ErrorLevel != 0 ; if it was not found, return here (do nothing)
         Return
   }
   ; get ending linefeed (if any) where the offset is the length of the eol more than the starting position:
   StringGetPos, end, src, %eol%, , start + StrLen(eol)
   If ErrorLevel = 0 ; if this is not the last line (ending linefeed was found) then:
      StringTrimLeft, newEnd, src, end + 1 ; get trailing part of file
   StringLeft, newStart, src, start ;
   ; replace file contents
   FileDelete, %file%
   FileAppend, %newStart%%newEnd%, *%file% ; use * to preserve \n in Unix files
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group