Jump to content

modify text file


Recommended Posts

Hi,

I'm absolutly new to autoit. I need a quick start to decide if it's the right tool for me. Could someone assist me with some code with which one I'm able to modify a text file.

I have a file which looks like this:

48.32332,9.34565,"Test1","Test2"

38.32332,8.34565,"Test11","Test2"

.....

I want to genarte a new file which looks like this:

48.32332,9.34565,"great"

38.32332,8.34565,"great"

......

I don't need any gui for that - just a cmd-line solution.

Thanks in advance.

Cheers

Wolle

Cheers Wolle

Link to comment
Share on other sites

Look in the help for the _FileReadToArray, StringInstr and StringLeft and _FileWriteFromArray.

They are all the functions you will need.

PsuedoCode:

Read File into array

Loop through array

Determine position of first " character

Use StringLeft to change array variable to be everything before that character

Append "great"

End Loop

Write file from array

That should get you going pretty well

Edit: Spelling

Edited by sandyd
----[ SandyD ]---
Link to comment
Share on other sites

Thanks a lot. This helped me a lot. Actually I got:

#include <file.au3>

Dim $aRecords

If Not _FileReadToArray("c:\temp\test.txt",$aRecords) Then

MsgBox(4096,"Error", " Error reading log to Array error:" & @error)

Exit

EndIf

For $x = 1 to $aRecords[0]

;Msgbox(0,'Record:' & $x, $aRecords[$x])

$col = StringInStr($aRecords[$x], ',"')

;MsgBox(0, "count:", $col)

$leftpart = StringLeft($aRecords[$x],$col-2)

MsgBox(0, "leftpart:", $leftpart)

$mynewline = ($leftpart + "great")

;MsgBox(0, "Search result:", $mynewline)

Next

How do append a string ? And what do you mean about the code so far ?

Cheers Wolle

Edited by wolle7

Cheers Wolle

Link to comment
Share on other sites

Basically its '&' to join a string not '+'

Good start for a first timer :-)

#include <file.au3>
Dim $aRecords
If Not _FileReadToArray("test.txt",$aRecords) Then
MsgBox(4096,"Error", " Error reading log to Array error:" & @error)
Exit
EndIf
For $x = 1 to $aRecords[0]
    ;Msgbox(0,'Record:' & $x, $aRecords[$x])

$col = StringInStr($aRecords[$x], ',"')
;MsgBox(0, "count:", $col)

$leftpart = StringLeft($aRecords[$x],$col-2)
;MsgBox(0, "leftpart:", $leftpart)
$mynewline = $leftpart & "," & Chr(34) & "great" & Chr(34)  ; chr(34) is Ascii code for "quote marks"
;MsgBox(0, "Search result:", $mynewline)
Next
Edited by Hasher

Firefox's secret is the same as Jessica Simpson's: its effortless, glamorous style is the result of — shhh — extensions!

Link to comment
Share on other sites

Actually, since you want great to be in double quotes, you should use single quotes to encase it. e.g.

$mynewline = ($leftpart & ',"great"')

if you now assign the value of $mynewline to your array variable, you can write the whole array as a file at the end of your script.

----[ SandyD ]---
Link to comment
Share on other sites

Thanks to you all ! I start loving autoit :whistle:

My script now looks like this:

CODE
#include <file.au3>

#include <Array.au3>

Dim $aRecords

Dim $outRecords

If Not _FileReadToArray("c:\temp\test.txt",$aRecords) Then

MsgBox(4096,"Error", " Error reading log to Array error:" & @error)

Exit

EndIf

For $x = 1 to $aRecords[0]-1

;Msgbox(0,'Record:' & $x, $aRecords[$x])

$col = StringInStr($aRecords[$x], ',"')

;MsgBox(0, "count:", $col)

$leftpart = StringLeft($aRecords[$x],$col-2)

;MsgBox(0, "leftpart:", $leftpart)

$mynewline = ($leftpart & ',"great"')

;MsgBox(0, "Search result:", $mynewline)

_ArrayInsert ($outRecords, $x , $mynewline)

;$outRecords[$x] = $mynewline

Next

If Not _FileWriteFromArray("c:\Temp\test_new.txt",$outRecords) Then

MsgBox(4096,"Error", " Error writing to Array error:" & @error)

Exit

Endif

Actually I'm not able to write the $mynewline in the $outRecords Array hmmmm what is the right syntax ?

Edited by wolle7

Cheers Wolle

Link to comment
Share on other sites

Try this:

#include <File.au3>

Dim $aRecords

If Not _FileReadToArray('c:\test.txt',$aRecords) Then
    MsgBox(4096,"Error", " Error reading log to Array error:" & @error)
    Exit
EndIf

For $x = 1 to $aRecords[0]
    $col = StringInStr($aRecords[$x], ',"')
    $leftpart = StringLeft($aRecords[$x],$col-2)
    $mynewline = ($leftpart & ',"great"')
    $aRecords[$x] = $mynewline
Next

If Not _FileWriteFromArray('c:\text1.txt',$aRecords,1) Then ; Need to start the array at 1
    MsgBox(4096,"Error", " Error writing to file error:" & @error)
    Exit
EndIf

MsgBox(0,'','Complete')
----[ SandyD ]---
Link to comment
Share on other sites

Wow that's great ! Thanks a lot. When I understood it right, then you replace every entry in the array with the new value. That's smart so I need not the double of memory if I'd used a second array.

But there is one point I don't understand.

If had this line: For $x = 1 to $aRecords[0]-1

You changed it to: For $x = 1 to $aRecords[0]

My input file looks like this (no empty 3rd line):

48.32332,9.34565,"Test1","Test2"

38.32332,8.34565,"Test11","Test2"

The output files looks like this with your solution:

48.32332,9.3456,"great"

38.32332,8.3456,"great"

,"great"

Am I wrong to set the "-1" of the counter ?

Cheers Wolle

Link to comment
Share on other sites

I would check your input file, i suspect it has a blank line at the end of it.

You could use this:

For $x = 1 to $aRecords[0]
    $col = StringInStr($aRecords[$x], ',"')
    if $col > 0 then
      $leftpart = StringLeft($aRecords[$x],$col-2)
      $mynewline = ($leftpart & ',"great"')
      $aRecords[$x] = $mynewline
  endif
Next

and it will ignore any lines that do not contain the double quotes

----[ SandyD ]---
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...