Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

convert .ini to .txt


  • Please log in to reply
10 replies to this topic

#1 mila

mila

    Seeker

  • Active Members
  • 19 posts

Posted 26 May 2010 - 07:39 AM

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.







#2 niubbone

niubbone

    Adventurer

  • Active Members
  • PipPip
  • 101 posts

Posted 26 May 2010 - 08:02 AM

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.

#3 mila

mila

    Seeker

  • Active Members
  • 19 posts

Posted 26 May 2010 - 08:45 AM

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.

#4 99ojo

99ojo

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 872 posts

Posted 26 May 2010 - 08:51 AM

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

#5 niubbone

niubbone

    Adventurer

  • Active Members
  • PipPip
  • 101 posts

Posted 26 May 2010 - 09:13 AM

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.

#6 mila

mila

    Seeker

  • Active Members
  • 19 posts

Posted 26 May 2010 - 09:23 AM

Hi Stefan,

It's my code :
Plain Text         
#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?

#7 99ojo

99ojo

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 872 posts

Posted 26 May 2010 - 09:54 AM

Hi,

AutoIt         
#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, 26 May 2010 - 10:08 AM.


#8 mila

mila

    Seeker

  • Active Members
  • 19 posts

Posted 26 May 2010 - 10:09 AM

Great. It's Worked.

Thanks Stefan :mellow:

#9 mila

mila

    Seeker

  • Active Members
  • 19 posts

Posted 27 May 2010 - 02:12 AM

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.

#10 99ojo

99ojo

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 872 posts

Posted 27 May 2010 - 05:26 AM

Hi,

this should work. The trick is reading last line of textfile and extracting the number to build new $trno.
AutoIt         
#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, 27 May 2010 - 06:09 AM.


#11 mila

mila

    Seeker

  • Active Members
  • 19 posts

Posted 31 May 2010 - 06:04 AM

Stefan, Thank U

:mellow:




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users