Sorting a .csv file, any help appreciated

Author Message
Thrinn

  • Total Posts : 5
  • Scores: 0
  • Reward points : 0
  • Joined: 12/4/2011
  • Status: offline
Sorting a .csv file, any help appreciated Sunday, December 04, 2011 8:46 PM (permalink)
0
[Helpful answer received] / [List Solutions Only]
Hello everyone,
first I would like to thank you all for this site, it is very helpfull for people like me who are new to scripting.
 
Now to my problem, I have a source .csv file with content similiar to:
    |A                 |B                         |C                |
1| CZ460000    | Adobe Flex        | John Doe   |
2| CZ470000    | MS Office 2007  | John Doe   |
3| CZ470000    | Notepad++       | John Doe   |
4| CZ480000    | Toad                 | John Doe   |
5| SK255555    | Adobe Flex        | Ann Blue    |      
6| SK2444444  | MS Office 2003  | Ann Blue    |   
   
It's basically a report from our detect software application, in first column are the names of workstations, second column is detected software and third column is name of the User to who is the workstation assigned to.
 
What I need the script to do is to report to a file:  Name - workstation name - All the software that was detect on this station,  if there are more stations assigned to one User then in the next row will be the name of next station - all the software on this station and so on... Then Next user and the same thing again to all the users in the .csv file.
 
I am working on this for a week now, and I got it working if the User has only 2 computers, but it can't detect if he has third.
Here is my code, please bear in mind I am new to vbscript so I'm sure the code could look a lot better, I will be gratefull for any advice:
 
 Option Explicit  Dim objExcel, strExcelPath, objSheet
Dim objUser
Dim fWrt, filesys 
Dim strUsercomputer, strUsercomputer2, strUsercomputer3, strUsercomputer4 Dim strUserName, vNewRow, strSoftware, strSoftware2, strSoftware3, strSoftware4, strPocSoft   Set filesys = CreateObject("Scripting.FileSystemObject")   strExcelPath = "D:\input.csv" ' Open specified spreadsheet and select the first worksheet.
objExcel.Workbooks.Open strExcelPath
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1) objExcel.visible=False   Const ForReading = 1, ForWriting = 2, ForAppending = 8 vNewRow = 2   Do While objSheet.Cells(vNewRow, 1).Value <> ""  strUserName = objSheet.Cells(vNewRow, 3)
 strUsercomputer = objSheet.Cells(vNewRow, 1).Value
 strSoftware = objSheet.Cells(vNewRow, 2).Value
 strSoftware2 = ""
 strSoftware3 = ""
 strSoftware4 = ""
 
     
  If objSheet.Cells(vNewRow, 3).Value =  strUserName Then
   Do While objSheet.Cells(vNewRow, 3).Value =  strUserName
   
   If objSheet.Cells(vNewRow, 1).Value = strUsercomputer  Then
    If strSoftware <> objSheet.Cells(vNewRow, 2).Value Then
    strSoftware = strSoftware & "," & objSheet.Cells(vNewRow, 2).Value
    WScript.Echo "Na riadku cislo: " & vNewRow & " je pocitac1 cislo: " & strUsercomputer
    End If
   vNewRow = vNewRow + 1 
   ElseIf objSheet.Cells(vNewRow, 1).Value <>  strUsercomputer  Then
    If strUsercomputer2 = "" Or strUsercomputer2 = objSheet.Cells(vNewRow + 1, 1).Value Then
    strUsercomputer2 = objSheet.Cells(vNewRow, 1).Value
     WScript.Echo "Na riadku cislo : " & vNewRow & " je pocitac2 cislo: " & strUsercomputer2
     If strSoftware2 <> objSheet.Cells(vNewRow, 2).Value Then
     strSoftware2 = strSoftware2 & "," & objSheet.Cells(vNewRow, 2).Value
     End If
    End If
    vNewRow = vNewRow + 1
    ElseIf objSheet.Cells(vNewRow, 1).Value <> strUsercomputer2 Then 'And objSheet.Cells(vNewRow, 1).Value <> strUsercomputer2 Then
   strUsercomputer3 = objSheet.Cells(vNewRow, 1).Value
    If strSoftware3 <> objSheet.Cells(vNewRow, 2).Value Then
    strSoftware3 = strSoftware3 & "," & objSheet.Cells(vNewRow, 2).Value
    WScript.Echo "Na riadku cislo : " & vNewRow & " je pocitac3 cislo: " & strUsercomputer3
    End If
   vNewRow = vNewRow + 1
    'ElseIf objSheet.Cells(vNewRow, 1).Value <> strUsercomputer Or objSheet.Cells(vNewRow, 1).Value <> strUsercomputer2 Or objSheet.Cells(vNewRow, 1).Value <> strUsercomputer3 Then
   'strUsercomputer4 = objSheet.Cells(vNewRow, 1).Value
    'If strSoftware4 <> objSheet.Cells(vNewRow, 2).Value Then
    'strSoftware4 = strSoftware4 & "," & objSheet.Cells(vNewRow, 2).Value
    'End If
   Else
   vNewRow = vNewRow + 1
   End If
  Loop
 End If
 
 
     
     Set fWrt = filesys.opentextfile  ("D:\Temp\Output.csv", ForAppending, True)
     fWrt.Write "Ahoj" & "," & strUserName & "," & "na tvojom pocitaci cislo: " & "," & strUsercomputer & "," & "sa nasiel tento nelegalny software: " & "," & strSoftware
      If strUsercomputer2 <> "" Then
      fWrt.Write ""
      fWrt.Write vbNewLine 
      fWrt.Write "Dalej na pocitaci cislo: " & "," & strUsercomputer2 & "," & "sa nasiel tento nelegalny software: " & "," & strSoftware2
       If strUsercomputer3 <> "" Then
       fWrt.Write ""
       fWrt.Write vbNewLine 
       fWrt.Write "Dalej na pocitaci cislo: " & "," & strUsercomputer3 & "," & "sa nasiel tento nelegalny software: " & "," & strSoftware3
        If strUsercomputer4 <> "" Then
        fWrt.Write ""
        fWrt.Write vbNewLine 
        fWrt.Write "Dalej na pocitaci cislo: " & "," & strUsercomputer4 & "," & "sa nasiel tento nelegalny software: " & "," & strSoftware4
        End If
       End If
      End If
     fWrt.Write ""
     fWrt.Write vbNewLine
     fWrt.Write vbNewLine
     fWrt.Close
     
    strUsercomputer = ""
 strUsercomputer2 = ""
 strUsercomputer3 = ""
 strUsercomputer4 = ""
 
 strSoftware = ""
 strSoftware2 = ""
 strSoftware3 = ""
 strSoftware4 = ""
 
         
    
Loop ' Close workbook and quit Excel.
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit   wscript. quit 

 
Sorry if the code doesn't show right, I'm not sure how to paste it.
Thank you in advance
 
 
#1
    ebgreen

    • Total Posts : 8227
    • Scores: 98
    • Reward points : 0
    • Joined: 7/12/2005
    • Status: offline
    Re:Sorting a .csv file, any help appreciated Monday, December 05, 2011 3:32 AM (permalink)
    0
    So your title says something about sorting a CSV, but you never mention anything about that in your actual question. Could you please clearly state exactly what the issue that you want to resolve is?
    "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
    Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
    http://www.visualbasicscript.com/m_47117/tm.htm
     
    #2
      Thrinn

      • Total Posts : 5
      • Scores: 0
      • Reward points : 0
      • Joined: 12/4/2011
      • Status: offline
      Re:Sorting a .csv file, any help appreciated Monday, December 05, 2011 4:37 AM (permalink)
      0
      I am sorry, my problem is that in the code I posted It only recognizes if the user has two stations. So could you please help me find out how to change the script so it recognizes if there is a third and/or a fourth station under the same user and assign the corresponding software to it.
       
      Or point me in the right direction to a different solution.
       
      The ultimate goal should be I run the script and it reads all cells from the source .csv file, it sorts all workstations to the corresponding user and to each station lists the software  and write this to the output .csv file.
       
      Thank you !
       
       
      #3
        59cobalt

        • Total Posts : 981
        • Scores: 91
        • Reward points : 0
        • Joined: 7/17/2011
        • Status: offline
        Re:Sorting a .csv file, any help appreciated Monday, December 05, 2011 9:17 AM (permalink)
        4
        [This post was marked as helpful]
        So, taking your initial example, what would the desired output be?
        John Doe | CZ460000  | Adobe Flex
        John Doe | CZ470000  | MSOffice 2007, Notepad++
        John Doe | CZ480000  | Toad
        Ann Blue | SK255555  | Adobe Flex
        Ann Blue | SK2444444 | MS Office 2003
        or
        John Doe | CZ460000  | Adobe Flex, MSOffice 2007, Notepad++, Toad
        John Doe | CZ470000  | Adobe Flex, MSOffice 2007, Notepad++, Toad
        John Doe | CZ480000  | Adobe Flex, MSOffice 2007, Notepad++, Toad
        Ann Blue | SK255555  | Adobe Flex, MS Office 2003
        Ann Blue | SK2444444 | Adobe Flex, MS Office 2003
        or
        John Doe | CZ460000, CZ470000, CZ480000 | Adobe Flex, MSOffice 2007, Notepad++, Toad
        Ann Blue | SK255555, SK2444444          | Adobe Flex, MS Office 2003

        In either case I'd probably use a multi-tiered dictionary for the task. Something like this:
        user = "John Doe"
        computer = "CZ470000"
        
        Set inventory = CreateObject("Scripting.Dictionary")
        inventory.Add user, CreateObject("Scripting.Dictionary")
        inventory(user).Add computer, CreateObject("Scripting.Dictionary")
        inventory(user)(computer).Add "MSOffice 2007", True
        inventory(user)(computer).Add "Notepad++", True

         
        #4
          Thrinn

          • Total Posts : 5
          • Scores: 0
          • Reward points : 0
          • Joined: 12/4/2011
          • Status: offline
          Re:Sorting a .csv file, any help appreciated Monday, December 05, 2011 10:41 AM (permalink)
          0
          Thank you for your input 59cobalt.
           
          The output should be exactly like the first case, just the name should be only in the first row, like this:
           
          John Doe  | CZ460000 |  Adobe Flex
          ________ | CZ470000 | MSOffice 2007, Notepad++
          ________ | CZ480000 | Toad
          Ann Blue   | SK255555 | Adobe Flex
          ________ | SK244444 | MS Office 2003
           
          As I mentioned I'm new to vbscript and haven't used the multi-tiered dictionary method yet, so I will look it up and give it a go.  Just a question will this work even if there are 50+ users and 200+ computers in the input sheet ?
           
          Thanks again for the suggestion
           
           
           
           
           
           
          #5
            59cobalt

            • Total Posts : 981
            • Scores: 91
            • Reward points : 0
            • Joined: 7/17/2011
            • Status: offline
            Re:Sorting a .csv file, any help appreciated Tuesday, December 06, 2011 12:13 AM (permalink)
            4
            Thrinn
            Thank you for your input 59cobalt.
            You're welcome.
            Thrinn
            The output should be exactly like the first case, just the name should be only in the first row
            To achieve that you merely need to omit the username in subsequent output lines. Like this:
            For Each username In inventory.Keys
             isSubsequentLine = False
             For Each computername In inventory(username).Keys
             If isSubsequentLine Then
             fWrt.WriteLine ";" & computername & ";" & Join(inventory(username)(computername).Keys, ", ")
             Else
             fWrt.WriteLine username & ";" & computername & ";" & Join(inventory(username)(computername).Keys, ", ")
             isSubsequentLine = True
             End If
             Next
            Next
            Thrinn
            As I mentioned I'm new to vbscript and haven't used the multi-tiered dictionary method yet, so I will look it up and give it a go.
            I doubt that you'll find significantly more than what I posted above. ;)
            Thrinn
            Just a question will this work even if there are 50+ users and 200+ computers in the input sheet ?
            Yes.
             
            #6
              ebgreen

              • Total Posts : 8227
              • Scores: 98
              • Reward points : 0
              • Joined: 7/12/2005
              • Status: offline
              Re:Sorting a .csv file, any help appreciated Tuesday, December 06, 2011 3:20 AM (permalink)
              4
              [This post was marked as helpful]
              I discuss dictionaries and dictionaries of dictionaries briefly here:
               
              http://www.visualbasicscript.com/In-Memory-Data-Structures-m80542.aspx
              "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
              Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
              http://www.visualbasicscript.com/m_47117/tm.htm
               
              #7
                Thrinn

                • Total Posts : 5
                • Scores: 0
                • Reward points : 0
                • Joined: 12/4/2011
                • Status: offline
                Re:Sorting a .csv file, any help appreciated Tuesday, December 06, 2011 3:36 AM (permalink)
                0
                So I tried and made the script using the dictionary method, it works but on each new line it writes User - Computer - One software.
                But the output I need is the same as Case 1 on your first post so  User1 - Computer1 - All software on computer1
                next line: If user1 has more computers then: Computer2 (next line computer3,4... - all soft on computer2(next line 3,4...)
                if not then User2 - Computer1 - all software on computer1
                next line: If user2 has more computers then: Computer2 (next line computer3,4... - all soft on computer2(next line 3,4...)
                if not then User3 - Computer1 - all software on computer1
                etc.
                 
                If I don't use the loop, it only writes the first line in the output file. Also tried to put the loop on different places in the script but it's not much better.
                 
                Can you please take a look on the code and tell me where did I go wrong ? (If you can tell me how to paste the code so it actually looks like code I will gladly repost it, can't figure it out, I'm pasting it from PrimalScript)
                 
                Thanks
                 
                ------------------------------------------------------------------------------------------------------------------------------------------------------
                Option Explicit
                 
                Dim objExcel, strExcelPath, objSheet
                Dim filesys, fWrt, isSubsequentLine
                Dim strUser, vNewRow, strSoftware, strComputer
                Dim user, computer, inventory, software
                 
                Const ForReading = 1, ForWriting = 2, ForAppending = 8
                 
                Set filesys = CreateObject("Scripting.FileSystemObject")
                Set objExcel = CreateObject("Excel.Application")
                 
                 
                strExcelPath = "D:\input.csv"
                objExcel.Workbooks.Open strExcelPath
                Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
                objExcel.visible=False
                 
                vNewRow = 2
                 
                Do While objSheet.Cells(vNewRow, 1).Value <> ""
                 
                strUser = objSheet.Cells(vNewRow, 3)
                strComputer = objSheet.Cells(vNewRow, 1)
                strSoftware = objSheet.Cells(vNewRow, 2)
                 
                user = strUser
                computer = strComputer
                software = strSoftware
                 
                Set inventory = CreateObject("Scripting.Dictionary")
                inventory.Add user, CreateObject("Scripting.Dictionary")
                inventory(user).Add computer, CreateObject("Scripting.Dictionary")
                inventory(user)(computer).Add software, True
                WScript.Echo Join(inventory(user)(computer).Keys)
                 
                 
                Set fWrt = filesys.opentextfile  ("D:\output.csv", ForAppending, True)
                For Each user In inventory.Keys
                isSubsequentLine = False
                For Each computer In inventory(user).Keys
                If isSubsequentLine Then
                fWrt.WriteLine "," & computer & "," & Join(inventory(user)(computer).Keys, ", ")
                fWrt.Close
                Else
                fWrt.WriteLine user & "," & computer & "," & Join(inventory(user)(computer).Keys, ", ")
                fWrt.Close
                isSubsequentLine = True
                End If
                Next
                Next
                vNewRow = vNewRow + 1
                 
                Loop
                 
                objExcel.ActiveWorkbook.Close
                objExcel.Application.Quit
                 
                wscript.quit
                ------------------------------------------------------------------------------------------------------------------------------------------------------
                <message edited by Thrinn on Tuesday, December 06, 2011 8:06 AM>
                 
                #8
                  Thrinn

                  • Total Posts : 5
                  • Scores: 0
                  • Reward points : 0
                  • Joined: 12/4/2011
                  • Status: offline
                  Re:Sorting a .csv file, any help appreciated Tuesday, December 06, 2011 8:10 AM (permalink)
                  0

                  I discuss dictionaries and dictionaries of dictionaries briefly here:

                  http://www.visualbasicscript.com/In-Memory-Data-Structures-m80542.aspx

                   
                  Thank you ebgreen, this actually helped me a lot with my another assignment
                   
                  #9
                    59cobalt

                    • Total Posts : 981
                    • Scores: 91
                    • Reward points : 0
                    • Joined: 7/17/2011
                    • Status: offline
                    Re:Sorting a .csv file, any help appreciated Tuesday, December 06, 2011 11:14 AM (permalink)
                    0
                    [This post was marked as helpful]
                    ebgreen
                    I discuss dictionaries and dictionaries of dictionaries briefly here:

                    http://www.visualbasicscr...Structures-m80542.aspx
                    A word of warning, though: dynamic data structures that grow in more than one dimension are a real pain to debug when something does not go as planned. So much that I wrote a helper function just for these situations.


                    Thrinn
                    So I tried and made the script using the dictionary method, it works but on each new line it writes User - Computer - One software.
                    [...]
                    Can you please take a look on the code and tell me where did I go wrong ?
                    What you are doing is this:
                    For Each line In sourceCSV
                     create new dataStructure
                     add fields from sourceCSV to dataStructure
                     For Each record In dataStructure
                     write record to the destinationCSV
                     Next
                    Next
                    i.e. you're merely taking the records from your source file and write them to the destination file. In a rather inefficient way.

                    What you should be doing is this:
                    create new dataStructure
                    For Each line in sourceCSV
                     add fields from sourceCSV to dataStructure
                    Next
                    For Each record In dataStructure
                     write record to destinationCSV
                    Next
                    i.e. create the data structure once, then use two independent loops. One to read the data from the source file and after that a second one to write the aggregated data to the destination file. Also don't open/close the output file with each loop cycle. Open it once before you start the loop and close it only after you finished the loop. And don't use the field separator character (",") for joining the software entries, lest you get inconsistent data field numbers.
                    <message edited by 59cobalt on Tuesday, December 06, 2011 11:41 AM>
                     
                    #10

                      Online Bookmarks Sharing: Share/Bookmark

                      Jump to:

                      Current active users

                      There are 0 members and 1 guests.

                      Icon Legend and Permission

                      • New Messages
                      • No New Messages
                      • Hot Topic w/ New Messages
                      • Hot Topic w/o New Messages
                      • Locked w/ New Messages
                      • Locked w/o New Messages
                      • Read Message
                      • Post New Thread
                      • Reply to message
                      • Post New Poll
                      • Submit Vote
                      • Post reward post
                      • Delete my own posts
                      • Delete my own threads
                      • Rate post

                      2000-2012 ASPPlayground.NET Forum Version 3.9