Re: Replace space in text file

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Thanks Rick.

It looks nice and compact (simple), compared to my strung out attempt.

I know that most of my problems are derived by not knowing many of the VB
commands available. There are just so many to sit there and try to memorize
them all. So even simple tasks like this may escape me if it requires using
a command I've not used before.

Again, thanks.

Webbiz



"Rick Rothstein (MVP - VB)" <rickNOSPAMnews@xxxxxxxxxxxxxxxxx> wrote in
message news:%23GSbKOaVIHA.4808@xxxxxxxxxxxxxxxxxxxxxxx
Get your input and save filenames as you now do, splice them into the
following code where shown, then run the code...

Dim TotalFile As String
Dim gFilename As String
Dim gSavename As String
'
' Get filename to load here
'
FileNum = FreeFile
Open gFilename For Binary As #FileNum
TotalFile = Space(LOF(FileNum))
Get #FileNum, , TotalFile
Close #FileNum
TotalFile = Replace(TotalFile, " ", vbCrLf)
'
' Get filename to save as here
'
FileNum = FreeFile
Open gSavename For Output As #FileNum
Print #FileNum, totatfile
Close #FileNum

Although you could consider getting both file names at once since the code
will run very fast.

Rick


"Webbiz" <noreply@xxxxxxx> wrote in message
news:OMhUf5ZVIHA.2368@xxxxxxxxxxxxxxxxxxxxxxx
That's fine Mike, but how do I do it?

I would not have asked and did all the programming I did if that was
already clear to me, right?

Also, why did I get the error I got?

"MikeB" <m.byerleyATVerizonDottieNettie> wrote in message
news:%23fStKtZVIHA.2268@xxxxxxxxxxxxxxxxxxxxxxx
Just read the file in one gulp and Replace the Spaces with a VbCrLf and
write to File2...

"Webbiz" <noreply@xxxxxxx> wrote in message
news:OiSBtNZVIHA.4448@xxxxxxxxxxxxxxxxxxxxxxx
I'm trying to write a small application that reads a text file where
the data items are all separated by a space, like this...


ABC DEF JKL MNO PQR STU VWX ...

And to create a file that reads like this instead...

ABC
DEF
JKL
MNO
PQR
STU
VWX

Where each space is replaced with a line-feed carriage return so that
the one very big string (the whole file) is broken down into individual
strings.

I'm sure this is VB 101 basic stuff for most of you. I'm just not sure
what is the easiest way to do this.

My code gives me a Run-time error '458': Variable uses an Automation
type not supported in Visual Basic.

Huh?

I'd like to learn why I'm getting this error as much as I'd like to
create this little program.

Here is the code I've done so far that creates this error.

My code uses a form that contains a Drive, Dir and File control for
inputing a text file that ends with filetype .s2r
The form also contains a textbox called txtFileName where you type in
the name of the file to write and a textbox txtDefaultPath that holds
the path where you want the file written to.

The program runs by double-clicking in the file control the file you
want to convert.

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

Drop this into the General Declarations




Public gFilename As String
Public gSavename As String

Public Sub RunConversion()

Dim iFileNum1 As Integer
Dim iFileNum2 As Integer
Dim lByteNum As Long
Dim vChars
Dim sMarket As String

gSavename = txtDefaultPath + txtFileName

'Open the file for reading
iFileNum1 = FreeFile

lByteNum = 1

Open gFilename For Binary Access Read As #iFileNum1

iFileNum2 = FreeFile


Open gSavename For Output As #iFileNum2

While Not EOF(iFileNum1)
Get #iFileNum1, lByteNum, vChars
If vChars = " " Then
If sMarket <> "" Then
Write #iFileNum2, sMarket
sMarket = ""
End If
Else
sMarket = sMarket + vChars
End If

lByteNum = lByteNum + 1
Wend

'Make sure to write to file any remaining information
If sMarket <> "" Then
Write #iFileNum2, sMarket
sMarket = ""
End If

Close #iFileNum1
Close #iFileNum2



End Sub

Private Sub Dir1_Change()

File1.Path = Dir1.Path


End Sub

Private Sub Drive1_Change()

Dir1.Path = Drive1.Drive


End Sub


Private Sub File1_DblClick()

gFilename = File1.Path + "\" + File1.FileName

RunConversion


End Sub

Private Sub Form_Load()

End Sub





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

Appreciate any help.

Thanks.

Webbiz








.



Relevant Pages

  • Re: Replace space in text file
    ... Dim TotalFile As String ... Dim iFileNum2 As Integer ... Private Sub Drive1_Change ...
    (microsoft.public.vb.general.discussion)
  • Re: Array Reference
    ... Public TotalFile As String ... Dim FileNum As Integer ... Dim TotalFile As String ...
    (microsoft.public.vb.general.discussion)
  • Re: Shuffle lines of data. . .
    ... Dim FileNum As Integer ... Dim TotalFile As String ... ' Write the rearranged array back to the hard disk ...
    (microsoft.public.vb.syntax)
  • Re: Increase columns, decrease rows
    ... Dim myFileName As String ... Dim FileNum As Long ... Public Function ReadUntil(ByRef sIn As String, ...
    (microsoft.public.excel.misc)
  • Re: Counting number of columns in CSV/Text file
    ... Function MaxColumns(PathFileName As String) As Long ... Dim FileNum As Long ... If UBound> MaxColumns Then MaxColumns = UBound ...
    (microsoft.public.excel.programming)