way1000

insert lines from text file

8 posts in this topic

i have many text files with "[@Description]" as the first line

i have to insert lines from a text file full of phrases to the text files that start with "[@Description]"

eg:

sourcefile.txt

line1

line2

...

targetfile1.txt

[@Description]

line1

targetfile2.txt

[@Description]

line2

 

thanks a lot in advance

Share this post


Link to post
Share on other sites



@way1000 this forum is not a place where you put in an order and someone spoon feeds a script to you. What have you tried on your own? I would suggest looking at FileRead, FileReadLine, and FileReadToArray in the help file. Actually try the example scripts contained in each topic, and then try to modify the example scripts to fit your needs. Then, if you get stuck, post your code here and we will do our best to assist.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

thanks a lot for your help

 

now i have a script that inserts a specific line of text on a line number in a specified target file.

but i cant make the target file an array of target files and the text line an array of text lines (the source is a text file), i don't know how

#include <File.au3>


; Example: Write to line 3 of c:\temp\test.txt NOT REPLACING line 3
_FileWriteToLine("C:\Users\user\Desktop\New Text Document.txt", 3, "my insertion", False)

 

thanks a lot in advance

 

 

 

 

Share this post


Link to post
Share on other sites

how can i make it work so it inserts lines from a source text file to many text files (1 line each)

eg:

"my insertion" an array of lines from a source text file

and

"C:\Users\user\Desktop\New Text Document.txt" many files in a folder

Share this post


Link to post
Share on other sites

Once you have your source text as a variable using JLogan3o13's suggestions  you can use:

_FileListToArrayRec   along with the FILES parameter (so you don't get any folders in your array)

Then loop through them with a a FOR NEXT Loop and insert the source text one by one using:

_FileWriteToLine

Share this post


Link to post
Share on other sites

i have a script, it runs but doesn't throw an error or writes anything

#include <Array.au3> ; Only required to display the arrays
#include <File.au3>
#include <MsgBoxConstants.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

Example()

Func Example()
    Local $sAutoItDir = StringLeft(@AutoItExe, StringInStr(@AutoItExe, "\", Default, -1))
    If StringRight($sAutoItDir, 5) = "beta\" Then
        $sAutoItDir = StringTrimRight($sAutoItDir, 5)
    EndIf
    ConsoleWrite($sAutoItDir & @CRLF)

    $aArray = _FileListToArrayRec($sAutoItDir, "*.txt", $FLTAR_FILES)
    _FileWriteToLine("$aArray", 3, "$sFileRead", False)


   Local $hFileOpen = FileOpen("C:\Users\user\Desktop\sourcefile.txt", $FO_READ)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
        Return False
    EndIf

    ; Read the fist line of the file using the handle returned by FileOpen.
    Local $sFileRead = FileReadLine($hFileOpen, 1)

    ; Close the handle returned by FileOpen.
    FileClose($hFileOpen)


 EndFunc   ;==>Example

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Trying to understand any logic in there but can't find it.  You have variables enclosed in quotes, making them an literal string and you are "trying" to write something that is read at a later point in the script?

Jos 

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

It looks like the OP used the _FileListToArrayRec Example and tried to write his code around it.

Not a bad idea (that's how most of us learned) but in this case it didn't work...

This should be closer to what you are trying to do. I have left in all consolewrites so you can see how to test and tweak your code.

I have made comments where I thought necessary, but feel free to ask if something is unclear.

#include <Array.au3> ; Only required to display the arrays
#include <File.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

;;;;Get your source text as a variable ... =  $sFileRead==============================

Local $sFilePath = @ScriptDir & "\sourcefile.txt"

   Local $hFileOpen = FileOpen($sFilePath, $FO_READ)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
    EndIf

    ; Read the fist line of the file using the handle returned by FileOpen.
    Local $sFileRead = FileReadLine($hFileOpen, 1)

    ; Close the handle returned by FileOpen.
    FileClose($hFileOpen)
;;;;Get your source text as a variable ... =  $sFileRead==============================

ConsoleWrite( "$sFileRead  = " & $sFileRead & @CRLF)
; see in the console what you will be writing to the files the array finds !!!if $sFileRead  = nothing, then nothing will be written

;;;Your array loop goes down here AFTER getting the source text=======================

Local $sFileWritePath = @ScriptDir & "\testfolder\"            ;;;change to your path(s)
ConsoleWrite( "$sFileWritePath  = " & $sFileWritePath & @CRLF)
    $aArray = _FileListToArrayRec($sFileWritePath, "*.txt", 1)
    _ArrayDisplay($aArray) ; have a look at your array these are the files that will be written to

    For $j = 1 to UBound($aArray) - 1   ;loop through files and write line
        Local $s2FilePath = @ScriptDir & "\testfolder\" & $aArray[$j]    ;;;change to your path(s)
        ConsoleWrite( "$s2FilePath  = " & $s2FilePath & @CRLF)
    _FileWriteToLine($s2FilePath, 1, $sFileRead, True)  ; do not put quotes around variables it emasculates them
    If @error Then
        MsgBox(0, "error", "_FileWriteToLine error = " & @error)
        EndIf
    ConsoleWrite( "$sFileRead  = " & $sFileRead & @CRLF)
    ConsoleWrite( "$aArray[$j]  = " & $aArray[$j] & @CRLF)
    Next

 

Edited by l3ill

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now