Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open a file and find a cariage return

Status
Not open for further replies.

GTLoco99

Programmer
Sep 2, 2003
59
US
Hi,

I and trying to write some code to open a *.CSV file and parse out and cariage returns in it.(Where user would hit enter). I have never wrote any code to parse a file so I am not even sure if i am going about this the right way. I am using the code bellow to open the file. Once i get it open though, I dont really know where to go from there. Any help would be appreciated

Code
_______________________________

Public Sub Parse_File_Click()

Dim RetVal As Long
Dim WSH As IWshShell
Dim WaitForTerm As Boolean
Dim Commandline As String
Dim Path As String
Dim PathFld As TextBox

Set PathFld = Me.Path_Name
Set WSH = New IWshShell_Class
Path = PathFld
Commandline = "C:\WINDOWS\NOTEPAD.EXE " & Path & ""
RetVal = WSH.Run(Commandline, vbNormalFocus, WaitForTerm)

End Sub

_____________________________________

Thanks
GTLoco
 
Once you have loaded the contents of the file into a string, you could use the replace command to replace carriage returns with whatever you want (maybe nothing?). i.e.
Code:
strExample = Replace(strExample, vbCrLf, "")


----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
how do you load the contents of the file into a string, and what if the file is too big?

GTLoco
 
To read the file into a string can be done like:
Code:
dim strFileData as String
Open "MyFile.txt" For Input As #1
    strFileData = Input(LOF(1), #1)
Close #1
[code]
This could then be used with the replace command to remove the carriage returns. Another way, if as you say the file is too large, is to loop through 1 line at a time usiong the replace command and maybe even read it into an array i.e.
[code]
ArrFileData = Split(strFileData, vbcrlf)


----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
once i replace the carriage returns, how do i instert the new string back into the file

Thanks
GTLoco
 
Try reading up on how to access/append files as you probably need a better insight on how these commands are used. A quick search on google produced:


----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Got it now! Thanks a ton for the help, got me pointed in the right direction. Here is the final code

Code:
________________________________

Public Sub Parse_File_Click()


Dim Path As String
Dim PathFld As TextBox
Dim textfile As File

Set PathFld = Me.Path_Name

Path = PathFld

Dim strFileData As String
Dim strFileReplace As String

Open Path For Input As #1

strFileData = Input(LOF(1), #1)
strFileReplace = Replace(strFileData, vbCrLf, "")

Close #1

Dim fso As New FileSystemObject, fil As File, ts As TextStream
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile ("c:\ParsedData.csv")
Set fil = fso.GetFile("c:\ParsedData.csv")
Set ts = fil.OpenAsTextStream(ForWriting)

ts.Write strFileReplace
ts.Close


MsgBox "Your file has been parsed. A new file has been created and saved at C:\ParsedData.csv", vbInformation, "Creation Successful"


End Sub
____________________________________
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top