Solved

# file to array -> change array -> array to file

Posted on 2003-03-03
Medium Priority
136 Views
textfile(location c:\test.txt) contents:
[Test1]
parity=none
option2=differerent

[Test2]
parity=odd
inverse
option2=differerent

[Test3]
parity=even
direct
option2=differerent

code:
----
Dim insideTest2 As Boolean
Open "c:\test.txt" For Binary As #1
Dim strBuff As String: strBuff = Space(LOF(1))
Get #1, , strBuff
Close #1
Dim strArr() As String: strArr = Split(strBuff, vbCrLf)
Dim i As Long
For i = 0 To UBound(strArr)
If RTrim\$(strArr(i)) = "[Test2]" Then
insideTest2 = True
Else
If Left(strArr(i), 1) = "[" And Right(RTrim(strArr(i)), 1) = "]" Then
insideTest2 = False
End If
End If
If insideTest2 Then
If Not InStr(strArr(i), "parity=") = 0 Then
Select Case RTrim\$(LCase\$(Mid\$(strArr(i), InStr(strArr(i), "parity=") + 7)))
Case "none"
If strArr(i + 1) = "inverse" Then
'here I want to delete item from array
End If
If strArr(i + 1) = "direct" Then
'here I want to delete item from array
End If
Case "even"
If strArr(i + 1) = "direct" Then
Else
If strArr(i + 1) = "inverse" Then
ReDim Preserve strArr(UBound(strArr) + 1)
strArr(i + 1) = "direct"
Else
strArr(i) = strArr(i) & vbCrLf & "direct"
End If
End If
Case "odd"
If strArr(i + 1) = "inverse" Then
Else
If strArr(i + 1) = "direct" Then
ReDim Preserve strArr(UBound(strArr) + 1)
strArr(i + 1) = "inverse"
Else
strArr(i) = strArr(i) & vbCrLf & "inverse"
End If
End If
End Select
End If
End If
Next
Open "c:\test.txt" For Binary As #1
Put #1, , Join(strArr, vbCrLf)
Close #1
End Sub
---------------------
What I want to do:
1.where it says "here I want to delete item from array" , I want to do just that.
2. Make code more simple but do same things.
like if you change parity=none [Test2] in test.txt
[Test2] should look like [Test1]. But if you change
[Test2] parity=odd the text inverse should be there. And
[Test2] parity=even the text direct should be there.

(3.)Make it unixformat(I can but I can not post it here)
So this one is already solved.
Do this to make your created test.txt:
---
Open "c:\test.txt" For Binary As #1
Put #1, , Join(strArr, Chr(13))
Close #1
and then run project
stop project.
replace all "vbCrLf" with "Chr(13)"
now it is unixformat.
0
Question by:atlantiscd
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2

LVL 3

Expert Comment

ID: 8061343
looks like a .ini file to me, you can use the WritePrivateProfileString API.  I made a little class to make things easier for myself, here is the code.

Put this in a Module:
Option Explicit

Declare Function WritePrivateProfileString& Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As String, ByVal lpFileName As String)
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Put this in a Class Module(called clsINI):
Option Explicit
Private mIniFileName As String

Public Property Let FileName(ByVal INIFileName As String)
'On Error Resume Next
If Dir(INIFileName, vbNormal) = "" Then
Open INIFileName For Output As #1
Close #1
End If
mIniFileName = INIFileName
End Property

Public Property Get FileName() As String
'On Error Resume Next
FileName = mIniFileName
End Property

Public Function GetValue(ByVal Section As String, ByVal Key As String, Optional ByVal DefaultValue As String) As String
On Error GoTo Hell
Dim Value As String, retval As String, x As Integer
retval = String\$(255, 0)
x = GetPrivateProfileString(Section, Key, DefaultValue, retval, Len(retval), mIniFileName)
GetValue = Trim(Left(retval, x))
Exit Function
Hell:
GetValue = DefaultValue
End Function

Public Function WriteValue(ByVal Section As String, ByVal Key As String, ByVal Value As String) As Boolean
On Error GoTo Hell
Dim x As Integer
x = WritePrivateProfileString(Section, Key, Value, mIniFileName)
If x <> 0 Then WriteValue = True
Exit Function
Hell:
End Function

Public Function GetAllSections() As Collection
Dim Value As String, retval As String, x As Integer
Dim s() As String, i As Integer
retval = String\$(255, 0)
x = GetPrivateProfileString(vbNullString, "", "", retval, Len(retval), mIniFileName)
Value = Trim(Left(retval, x))
s = Split(Value, Chr(0))
Set GetAllSections = New Collection
With GetAllSections
For i = LBound(s) To UBound(s)
If s(i) <> "" Then .Add s(i)
Next
End With
End Function

Public Function GetAllKeys(ByVal Section As String) As Collection
Dim Value As String, retval As String, x As Integer
Dim s() As String, i As Integer
retval = String\$(255, 0)
x = GetPrivateProfileString(Section, vbNullString, "", retval, Len(retval), mIniFileName)
Value = Trim(Left(retval, x))
s = Split(Value, Chr(0))
Set GetAllKeys = New Collection
With GetAllKeys
For i = LBound(s) To UBound(s)
If s(i) <> "" Then .Add s(i)
Next
End With
End Function

Public Function DeleteSection(ByVal Section As String) As Boolean
On Error GoTo Hell
Dim x As Integer
x = WritePrivateProfileString(Section, vbNullString, "", mIniFileName)
If x <> 0 Then DeleteSection = True
Exit Function
Hell:
End Function

Public Function DeleteKey(ByVal Section As String, ByVal Key As String) As Boolean
On Error GoTo Hell
Dim x As Integer
x = WritePrivateProfileString(Section, Key, vbNullString, mIniFileName)
If x <> 0 Then DeleteKey = True
Exit Function
Hell:
End Function

This is how you would use it:
Dim MyConfig as new clsINI
MyConfig.FileName = "yourfile.ini"
MyConfig.WriteValue("Test1","parity","none")
If the section does not exist then it will create it.
this gets the value of a key. the last parameter is the default value, it gets returned if no value was found.
You can play around with it to see what the other Methods do.
0

Author Comment

ID: 8061576
hmm... Looks like you have problem to write the values.
inverse
direct
how do you get them at line after parity=odd/even/none?

thanks for response..
0

Author Comment

ID: 8061655
hmm... Looks like you have problem to write the values.
inverse
direct
how do you get them at line after parity=odd/even/none?

thanks for response..
0

LVL 3

Accepted Solution

Da_Weasel earned 150 total points
ID: 8065594
Do you need to have the words inverse and direct on a line by them selves?
What are these to values used for?  It would probably be better if you did this:

someval=inverse/direct
0

Author Comment

ID: 8067847
No it is NOT possible to do so.
Cause I use my app as a GUI for another app.
If I change it does not work in that app.
0

Expert Comment

ID: 8900815
atlantiscd:
Experts: Post your closing recommendations!  Who deserves points here?
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
###### Suggested Courses
Course of the Month14 days, 18 hours left to enroll