Solved

.csv To .txt Follow Up

Posted on 2014-10-29
20
157 Views
Last Modified: 2014-11-10
Hi Experts,

I have piece of code which import all .csv data in to one .csv file (import.csv) and create import.txt file from import.csv file. All is working well just need to add one more procedure while creating import.txt. Need to sort data in A to Z except header.
here is the code
Sub csvTotxt()
    Dim strData As String
    Dim oRE As Object
    Dim oMatches As Object
    Dim oM As Object
    Dim oSM As Object
    Dim oWsh As Object
    Const ForReading = 1, ForWriting = 2, ForAppending = 3
    Dim oFS As Object, oTS As Object
    Dim lngSM As Long
    Dim vOutput As Variant
    Const cPath As String = "C:\Test\"
    Const cHeader As String = "<ticker>,<per>,<date>,<open>,<high>,<low>,<close>,<vol>,<o/i>"
    
    Set oWsh = CreateObject("wscript.shell")
    Set oFS = CreateObject("Scripting.FileSystemObject")

    Set oRE = CreateObject("vbscript.regexp")
    oRE.Global = True
    oRE.Pattern = "(\w[^,]*),[^,]*,(\d[^,]*),([^,]*),([^,]*),([^,]*),[^,]*,[^,]*,([^,]*),[^,]*,([^,]*).*?,\n"
    
    'combine csv files
    oWsh.Run "cmd /c copy " & cPath & "*.csv " & cPath & "import.csv", 0, True
    
    Set oTS = oFS.OpenTextFile(cPath & "import.csv", ForReading, False, False)
    strData = oTS.readall
    oTS.Close
    Set oTS = oFS.OpenTextFile(cPath & "import.txt", ForWriting, True, False)
    oTS.writeline cHeader
    
    If oRE.test(strData) Then
        ReDim vdata(1 To 9)
        Set oMatches = oRE.Execute(strData)
        For Each oM In oMatches
            With oM
                vdata(1) = .submatches(0)
                vdata(2) = "D"
                vdata(3) = Format(.submatches(6), "yyyymmdd")
                vdata(4) = .submatches(1)
                vdata(5) = .submatches(2)
                vdata(6) = .submatches(3)
                vdata(7) = .submatches(4)
                vdata(8) = .submatches(5)
                vdata(9) = 0
                oTS.writeline Join(vdata, ",")
            End With
        Next
        oTS.Close
    End If
    
End Sub

Open in new window


Prior related question: http:Q_28526105.html

Please Help Me.

Thank you
0
Comment
Question by:itjockey
  • 10
  • 8
20 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 40411663
What about sorting the CSV file in Excel?
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40411665
What fields are your sort keys?
0
 
LVL 8

Author Comment

by:itjockey
ID: 40411688
Column A I.e. Ticker

Thanks
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40411747
Are you going to open the CSV file in Excel or use it some other way?
0
 
LVL 8

Author Comment

by:itjockey
ID: 40411778
I don't do any thing with CSV file ...I use only .txt file.

so expecting final result like this Final output  I not sure about how it will be done but logically I think this way, I may be completely wrong on this Assumption
i need only .txt file in sorted A to Z.

Thanks
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40411802
If you can open the txt file in Excel, that is the most logical place to sort the data.
0
 
LVL 8

Author Comment

by:itjockey
ID: 40411829
Can't we done that automated ? I.e. sorted data in .import.txt ?

Thank you
0
 
LVL 8

Author Comment

by:itjockey
ID: 40411849
As I don't open any file ...I run my charting software add in which update data from this import.txt file ..if I go via unsorted data it will take 5 min and if I go via sorted data in import.txt file it will take for same amount of data only 30 sec.

Thanks
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40412377
Do you have Notepad++?  Use it to sort the file
(already stated) Use Excel to sort the file
Can you run Powershell?  If so, use it to sort the file
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 40412555
Here is a quick and dirty attempt at doing the sort.
Sub csvTotxt()
    Dim strData As String
    Dim oRE As Object
    Dim oMatches As Object
    Dim oM As Object
    Dim oSM As Object
    Dim oWsh As Object
    Const ForReading = 1, ForWriting = 2, ForAppending = 3
    Dim oFS As Object, oTS As Object
    Dim lngSM As Long
    Dim vOutput As Variant
    Const cPath As String = "C:\Test\"
    
    '                          <ticker>
    Const cHeader As String = "!!!!!!!!,<per>,<date>,<open>,<high>,<low>,<close>,<vol>,<o/i>"
    
    Set oWsh = CreateObject("wscript.shell")
    Set oFS = CreateObject("Scripting.FileSystemObject")

    Set oRE = CreateObject("vbscript.regexp")
    oRE.Global = True
    oRE.Pattern = "(\w[^,]*),[^,]*,(\d[^,]*),([^,]*),([^,]*),([^,]*),[^,]*,[^,]*,([^,]*),[^,]*,([^,]*).*?,\n"
    
    'combine csv files
    oWsh.Run "cmd /c copy " & cPath & "*.csv " & cPath & "import.csv", 0, True
    
    Set oTS = oFS.OpenTextFile(cPath & "import.csv", ForReading, False, False)
    strData = oTS.readall
    oTS.Close
    
    Set oTS = oFS.OpenTextFile(cPath & "import.txt", ForWriting, True, False)
    oTS.writeline cHeader
    
    If oRE.test(strData) Then
        ReDim vdata(1 To 9)
        Set oMatches = oRE.Execute(strData)
        For Each oM In oMatches
            With oM
                vdata(1) = .submatches(0)
                vdata(2) = "D"
                vdata(3) = Format(.submatches(6), "yyyymmdd")
                vdata(4) = .submatches(1)
                vdata(5) = .submatches(2)
                vdata(6) = .submatches(3)
                vdata(7) = .submatches(4)
                vdata(8) = .submatches(5)
                vdata(9) = 0
                oTS.writeline Join(vdata, ",")
            End With
        Next
        oTS.Close
    End If
    
    oWsh.Run "cmd /c sort " & cPath & "import.txt /o " & cPath & "sortedimport.txt", 0, True
    
    Set oTS = oFS.OpenTextFile(cPath & "sortedimport.txt", ForReading, False, False)
    strData = oTS.readall
    oTS.Close
    
    strData = Replace(strData, "!!!!!!!!", "<ticker>")  'restore header
    
    Set oTS = oFS.OpenTextFile(cPath & "import.txt", ForWriting, True, False)
    oTS.write strData
    oTS.Close
    
    oFS.deletefile cPath & "sortedimport.txt"
End Sub

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
ID: 40412572
@David

The problem isn't as simple as that.  Visit the prior related question and look at the posted code for a better understanding.
0
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
ID: 40412576
powershell script change as required
$infile = "F:\Downloads\cm01OCT2014bhav.csv"
$outfile = "C:\temp\outfile.txt"
$csv = import-csv -Path $infile  
$csv | sort-object  {$_.Symbol}|convertto-csv -NoTypeInformation | Out-File $outfile

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
ID: 40412995
as required
All the csv files in a directory have to be processed as described in the prior related question, not just a single file.  Moreover, only certain columns should be included in the output (one added and one formatted), and a new header supplied.
0
 
LVL 8

Author Comment

by:itjockey
ID: 40413938
M I missing something ....I had run this Code
and it is working fine.....please suggest may I close this question?

Thanks
0
 
LVL 8

Author Comment

by:itjockey
ID: 40413952
Mr.David Johnson, CD, MVP,

Your solution I dint tried as it is above my understanding.

Thank You
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40413957
Sure.  If it solves the problem (answers the question), then accept that comment as the solution.
0
 
LVL 8

Author Closing Comment

by:itjockey
ID: 40414015
Awesome ....Thank you very much.
0
 
LVL 8

Author Comment

by:itjockey
ID: 40434185
Mr.aikimark,

one request need to change path "C - Test" to "D:\AmiBroker Data\NSEeq".
I had change this path in code but getting error. please let me know what changes need to done.

Thanks
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40434224
Did you follow the same pattern of the string constant file path in the accepted solution comment?

What does your code look like, specifically the const line?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Compile Error 7 41
Easy Excel formula needed 4 27
Simple Excel re-arrange data.  (I can work in access too). 8 50
excel help 4 18
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
Outlook Free & Paid Tools
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now