ClioSport.net

Register a free account today to become a member!
Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

  • When you purchase through links on our site, we may earn an affiliate commission. Read more here.

Batch to copy then sort csv file



  E39 530i
evening guys, need some help. I'm trying to write a batch file which will copy a .csv file but also sort the contents of it. Any ideas, or help please.
 
  E39 530i
windows 7, it's just going to be basic text. Two columns, first column with a number, and the second column with a name.
 
windows 7, it's just going to be basic text. Two columns, first column with a number, and the second column with a name.
Single line in PowerShell - I am assuming its in a subfolder of C:\ called MyData and you want to sort on Name column

PS C:\MyData> $mycsv = Import-Csv .\Source.csv | Sort-Object -Property Name -Descending | Export-Csv NameSorted.csv -NoTypeInformation

The | are pipes.
 
  E39 530i
Cheers Spoonie. When i run the ps script it creates the secondary file but the file is all alien text. any ideas....
 
Not sure, could be encoding. You can split the command up:

PS C:\MyData> $mycsv = Import-Csv .\Source.csv
PS C:\MyData> $mycsv | Sort-Object -Property Name -Descending

Does that print out to the screen as expected?
 
  E39 530i
Nop, my fault. Got it producing the file but its not sorting it correct. Below is a simple of the original and the output file it produces

Number Name
21 John
43 Mark
2 Ed
1 Ellen
100 Steven
89 Michael

Number Name
89 Michael
43 Mark
21 John
2 Ed
10 Steven
1 Ellen

What am i doing wrong
 
What's the output from the following:

PS C:\MyData> $mycsv = Import-Csv .\Source.csv
PS C:\MyData> $mycsv | Get-Member

EDIT: Its these you want to check:

Code:
PS C:\> $mycsv | Get-Member

TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
[B]Name NoteProperty System.String Name=John
Number NoteProperty System.String Number=21[/B]

That is the property it will try and sort on, not sure why yours isn't sorting. Mine:


Code:
PS C:\> $mycsv | Sort-Object -Property Name -Descending
Number Name
------ ----
100 Steven
89 Michael
43 Mark
21 John
1 Ellen
2 Ed
 
Last edited:


Top