Sign in to follow this  
Followers 0

convert .ini to .txt

11 posts in this topic

#1 ·  Posted

Dear All,

I need help to convert .ini to .txt formatted. I have 2 .ini files in my directory (A.ini & B.ini). Here is .ini format:

(File A.ini)

[Test]

Date = 15052010

Name = A

Acc1 = 580

Acc2 = 581

Tot1 = 1000

Tot2 = 5000

(File B.ini)

[Test]

Date = 15052010

Name = B

Acc1 = 580

Acc2 = 581

Tot1 = 2000

Tot2 = 8000

I want to put them on 1 .txt file with this format :

001;15052010;A;580;1000

001;15052010;A;581;5000

002;15052010;B;580;2000

002;15052010;B;581;8000

can we do that? Please help and Thanks.

Share this post


Link to post
Share on other sites



#2 ·  Posted

Don't have time to put down some code of lines. Though this is what you are looking for:

FileRead

FileReadLine

StringLeft

StringRight

These Will do most of the work. Check Help file for these, and also look for String Management in the Index of help file.

niubbone.

Share this post


Link to post
Share on other sites

#3 ·  Posted

I know the way to read .ini and write them to .txt. I just confuse how to make 2 .ini file to be 1 .txt file and give number (eg. 001, 002) based on each file.

Share this post


Link to post
Share on other sites

#4 ·  Posted

Hi,

where's the problem, if you know how to:

Reading 1st ini and write values to textfile with 001.

Reading 2nd ini and write values to textfile with 002.

If you have problems with your code, you should post it....

;-))

Stefan

Share this post


Link to post
Share on other sites

#5 ·  Posted

Hi,

where's the problem, if you know how to:

Reading 1st ini and write values to textfile with 001.

Reading 2nd ini and write values to textfile with 002.

If you have problems with your code, you should post it....

;-))

Stefan

I second that.

Share this post


Link to post
Share on other sites

#6 ·  Posted

Hi Stefan,

It's my code :

#Include <File.au3>

dim $var1a, $var1b, $var2a, $var2b, $var3a, $var3b
dim $trno, $split, $split1, $message

$dir = FileOpenDialog($message, @ScriptDir & "\","File (*.ini)", 1 + 4)
$message = "Hold down Ctrl or Shift to choose multiple files."

            If @error Then
                MsgBox(4096,"","No File(s) chosen")
            Else
                $dir = StringReplace($dir, "|", @CRLF)
                MsgBox(4096,"Confirm","You choose " & $dir)
            EndIf

;$dir = @ScriptDir & $open
$split = StringRight($dir, 5)
$split1 = StringLeft($split, 1)
$dir2 = @ScriptDir & "\" & $split1 & ".txt"

$var1a = IniRead($dir, "Test", "Date", "")
$var1b = IniRead($dir, "Test", "Name", "")
$var2a = IniRead($dir, "Test", "Acc1", "")
$var2b = IniRead($dir, "Test", "Acc2", "")
$var3a = IniRead($dir, "Test", "Tot1", "")
$var3b = IniRead($dir, "Test", "Tot2", "")

$trno = "00001"

FileWriteLine($dir2, $trno &";"&  $var1a &";"& $var1b & ";" & $var2a &";"& $var3a)
FileWriteLine($dir2, $trno &";"&  $var1a &";"& $var1b & ";" & $var2b &";"& $var3b)

A.ini

================

[Test]

Date=15052010

Name=A

Acc1=580

Acc2=581

Tot1=1000

Tot2=2000

My code just convert 1 ini file & trno i put manual.

How to put 2 .ini into 1 .txt & trno generate automatically?

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Hi,

#Include <File.au3>
Global $var1a, $var1b, $var2a, $var2b, $var3a, $var3b
Global $trno, $message, $files
$message = "Hold down Ctrl or Shift to choose multiple files."
$dir = FileOpenDialog($message, @ScriptDir & "\","File (*.ini)", 1 + 4)
If @error Then
    MsgBox(4096,"","No File(s) chosen. Exit Program")
    Exit
Else
    MsgBox(4096,"Confirm","You choose " & StringReplace($dir, "|", @CRLF))
EndIf
;Splitting string by delimiter "|" into array $files, $files [0] -> array elements, $files [1] -> Path to chosen files
;$files [2.........] -> Filenames
$files = StringSplit ($dir, "|")
;define outputfile
$textfile = @ScriptDir & "\newtextfile.txt"
;loop over $files array starting with 1.st chosen file
For $i = 2 To $files [0]
    $var1a = IniRead($files [1] & "\" & $files [$i], "Test", "Date", "")
    $var1b = IniRead($files [1] & "\" & $files [$i], "Name", "")
    $var2a = IniRead($files [1] & "\" & $files [$i], "Test", "Acc1", "")
    $var2b = IniRead($files [1] & "\" & $files [$i], "Test", "Acc2", "")
    $var3a = IniRead($files [1] & "\" & $files [$i], "Test", "Tot1", "")
    $var3b = IniRead($files [1] & "\" & $files [$i], "Test", "Tot2", "")
    $trno = "0000" & $i - 1
    FileWriteLine($textfile, $trno & ";" &  $var1a & ";" & $var1b & ";" & $var2b & ";" & $var3b)
Next
; show output file
ShellExecute ("notepad.exe", $textfile)

;-))

Stefan

@edit: Missed the numbering part in the post. Changed line $trno = "0000" & $i to $trno = "0000" & $i - 1 in post

Edited by 99ojo

Share this post


Link to post
Share on other sites

#8 ·  Posted

Great. It's Worked.

Thanks Stefan :mellow:

Share this post


Link to post
Share on other sites

#9 ·  Posted

Hi Stefan,

I have question :

If i run the script in 1 time, it will produce trno: 001, 002, 003.

And when i close program then run script again to append text in same txt file, trno

will produce 001, 002, 003 same like before. How to make trno continue (004, 005, ....)?

Thanks.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Hi,

this should work. The trick is reading last line of textfile and extracting the number to build new $trno.

#Include <File.au3>
Global $var1a, $var1b, $var2a, $var2b, $var3a, $var3b
Global $trno, $message, $trnostart
$message = "Hold down Ctrl or Shift to choose multiple files."
$dir = FileOpenDialog($message, @ScriptDir & "\","File (*.ini)", 1 + 4)
If @error Then
    MsgBox(4096,"","No File(s) chosen. Exit Program")
    Exit
Else
    MsgBox(4096,"Confirm","You choose " & StringReplace($dir, "|", @CRLF))
EndIf
$files = StringSplit ($dir, "|")
$textfile = @ScriptDir & "\newtextfile.txt"
If FileExists ($textfile) Then
    ;reading last line of textfile
    $temp = FileReadLine ($textfile, -1)
    ;Splitting string by ;
    $temp = StringSplit ($temp, ";")
    ;$temp [1] is "000xx", so replace 0 with nothing to get start value
    $trnostart = StringReplace ($temp [1], "0", "")
EndIf
    
For $i = 2 To $files [0]
    $var1a = IniRead($files [1] & "\" & $files [$i], "Test", "Date", "")
    $var1b = IniRead($files [1] & "\" & $files [$i], "Name", "")
    $var2a = IniRead($files [1] & "\" & $files [$i], "Test", "Acc1", "")
    $var2b = IniRead($files [1] & "\" & $files [$i], "Test", "Acc2", "")
    $var3a = IniRead($files [1] & "\" & $files [$i], "Test", "Tot1", "")
    $var3b = IniRead($files [1] & "\" & $files [$i], "Test", "Tot2", "")
    If $trnostart + $i - 1 < 10 Then
        $trno = "0000" & $trnostart + $i - 1
    Else
        $trno = "000" & $trnostart + $i - 1
    EndIf
    FileWriteLine($textfile, $trno & ";" &  $var1a & ";" & $var1b & ";" & $var2a & ";" & $var3a)
    FileWriteLine($textfile, $trno & ";" &  $var1a & ";" & $var1b & ";" & $var2b & ";" & $var3b)
Next

;-))

Stefan

@edit: Add $trnostart in variable declaration

@edit: Sorry my code doesn't work for numbers like 10, 20 100 and so on. Rewrite part If FileExists:

If FileExists ($textfile) Then
    ;reading last line of textfile
    $temp = FileReadLine ($textfile, -1)
    ;Splitting string by ;
    $temp = StringSplit ($temp, ";")
    For $i = 1 To StringLen ($temp [1])
        ;Find 1 to 9, If found, $trnostart is $temp [1] at position $i to end of string
        If StringRegExp (StringMid ($temp [1], $i, 1), "[1-9]") Then
            $trnostart = StringMid ($temp [1], $i, StringLen ($temp [1]) - $i + 1)
            ExitLoop
        EndIf
    Next
EndIf

also changed as follows in For .. Loop to write output file:

FileWriteLine($textfile, $trno & ";" &  $var1a & ";" & $var1b & ";" & $var2a & ";" & $var3a)
    FileWriteLine($textfile, $trno & ";" &  $var1a & ";" & $var1b & ";" & $var2b & ";" & $var3b)
Edited by 99ojo

Share this post


Link to post
Share on other sites

#11 ·  Posted

Stefan, Thank U

:mellow:

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
Sign in to follow this  
Followers 0