Jump to content

FileRead - FileWrite


Recommended Posts

Hello everyone, I'm new in the forum...Sorry if I don't speak English very well, but I am Italian. :)

I wanted to ask you if there is a way to read the contents of a file .txt and rewrite it in another .txt file, adding a letter at the beginning of each line?

I wrote a working code, but there is a problem: in fact, to run the entire operation, it took 2 full days!

here's the code:

#include <FIle.au3>


Func _Algoritmo($lettera)

For $i = _FileCountLines("Algoritmo4.txt") to 1 step -1
    $count = $count + 1
$x = FileReadLine("Algoritmo4.txt", $i)
$x2 = $lettera & $x & @CRLF
FileWriteLine("Dictionary.txt", $x2)
TrayTip("Caricamento ciclo...", $count, 100000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000099900)
Next

FileWriteLine("Temp.log", "tempo impiegato: " & $count & "Cicli")

If $Silenzioso = 6 Then
MsgBox(0,"Loading...", "Finish!!!!!!!!")
Exit
Else
Beep(1000)
Beep(500)
Beep(1000)
Beep(500)
MsgBox(0,"Loading...", "Finish!!!!!!!!")
EndIf


EndFunc


Global $count = 1
$lettera=InputBox("Lettera?", "Inserisci una lettera")

Global $Silenzioso = MsgBox(3, "Silenzioso?", "Attivare la modalità silenziosa?")

_Algoritmo($lettera)

This code reads, edits and rewrites every single line, but one at a time. Since the file "Algoritmo4.txt"consists of 400,000 lines of code, the process is too long.

Today I wrote a new code, incredibly fast, but ineffective: It adds the letter only to the first line ... Do you know how to fix it?

This is the code:

Func _Algoritmo($lettera, $Algoritmo)
If Not FileExists($Algoritmo & ".txt") Then 
MsgBox(16, "Errore", "Non esiste il file specificato")
Exit
EndIf

$Read=FileRead($Algoritmo & ".txt")
$ReadAdd= $lettera & $Read
FileWriteLine("Algoritmo - Copia.txt", $ReadAdd)

EndFunc

$lettera=InputBox("Lettera?", "Inserisci una lettera")
$Algoritmo = InputBox("Algoritmo?", "Inserisci il tipo di algoritmo..")

_Algoritmo($lettera, $Algoritmo)

Many thanks in advance :)

Link to comment
Share on other sites

Hello,

Just as an example.

I've created a test file with 400.000 lines (it took 11 seconds to create it).

$filetest="C:\test.txt"
$hfile=FileOpen($filetest,2)
For $i=1 to 400000 Step 1
    FileWrite($hfile, "Hello, I'm line " & $i & @CRLF)
Next
FileClose($hfile)

Now, using test.txt file I've added the letter A on every line and write it to file write.txt (it took only 12 seconds).

#include <file.au3>
$readfile="c:\test.txt"
$writefile="c:\write.txt"
$letter="A"
Dim $array

_FileReadToArray($readfile,$array)

$hfile=FileOpen($writefile,2)
For $i=1 to $array[0]
    FileWrite($hfile,$letter & $array[$i] & @CRLF)
Next
FileClose($hfile)

I hope this helps you ;-)

Link to comment
Share on other sites

If the prefix (letter) is the same you can use this method:

$sFile = FileRead("Algoritmo4.txt")
$sLettera = "#123#"
$sNew = StringRegExpReplace($sFile, "(?<!.).*", $sLettera & "$0")
$hFile = FileOpen("Algoritmo4_new.txt", 2)
FileWrite($hFile, $sNew)
FileClose($hFile)

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...