@@ -769,6 +769,8 @@ public static void GeneratePowerShell(CsvDefinition csvdef)
769
769
770
770
var col_names = "" ;
771
771
var col_fixed = "" ;
772
+ var col_fixed_write1 = "" ;
773
+ var col_fixed_write2 = "" ;
772
774
var col_order = "" ;
773
775
var col_types = "" ;
774
776
var col_enums = "" ;
@@ -800,7 +802,8 @@ public static void GeneratePowerShell(CsvDefinition csvdef)
800
802
801
803
// list all column names
802
804
col_names += string . Format ( "\" {0}\" {1}" , coldef . Name , comma ) ;
803
- col_order += string . Format ( "\t \t {0} = $_.{1}\r \n " , colnamepad , colname ) ;
805
+ var datemask = ( coldef . DataType == ColumnType . DateTime ? string . Format ( ".ToString(\" {0}\" )" , coldef . Mask ) : "" ) ;
806
+ col_order += string . Format ( "\t \t {0} = $_.{1}{2}\r \n " , colnamepad , colname , datemask ) ;
804
807
805
808
// enumeration
806
809
if ( coldef . isCodedValue )
@@ -855,6 +858,8 @@ public static void GeneratePowerShell(CsvDefinition csvdef)
855
858
col_fixed += string . Format ( "\t \t {0} = $line.Substring({1}, {2}).Trim(' \" ')\r \n " , colnamepad , strpos , strwid ) ;
856
859
startpos += coldef . MaxWidth ;
857
860
}
861
+ col_fixed_write1 += string . Format ( "{{{0},{1}{2}}} " , c , ( coldef . DataType == ColumnType . Integer || coldef . DataType == ColumnType . Decimal ? "" : "-" ) , coldef . MaxWidth ) ;
862
+ col_fixed_write2 += string . Format ( "$row.{0}{1}" , colname , comma ) ;
858
863
}
859
864
860
865
// no decimals, then not technically needed but nice to have as example code
@@ -884,13 +889,26 @@ public static void GeneratePowerShell(CsvDefinition csvdef)
884
889
if ( csvdef . Separator == '\0 ' )
885
890
{
886
891
// fixed width
887
- ps1 . Append ( string . Format ( "# read fixed width data file, positions {0}\r \n " , GetColumnWidths ( csvdef , true ) ) ) ;
888
-
892
+ ps1 . Append ( string . Format ( "# read fixed width data file, positions {0}\r \n " , csvdef . GetColumnWidths ( true ) ) ) ;
889
893
ps1 . Append ( "$stream_in = [System.IO.StreamReader]::new($filename)\r \n \r \n " ) ;
894
+
895
+ if ( csvdef . SkipLines > 0 )
896
+ {
897
+ ps1 . Append ( string . Format ( "# skip first {0} lines\r \n " , csvdef . SkipLines ) ) ;
898
+ ps1 . Append ( string . Format ( "for ($i=0; $i -lt {0}; $i=$i+1) { $skipline = $stream_in.ReadLine() }\r \n " , csvdef . SkipLines ) ) ;
899
+ }
900
+
901
+ if ( csvdef . ColNameHeader )
902
+ {
903
+ ps1 . Append ( "# skip header\r \n " ) ;
904
+ ps1 . Append ( "$skipline = $stream_in.ReadLine()\r \n " ) ;
905
+ }
906
+
907
+ ps1 . Append ( "# read fixed width data\r \n " ) ;
890
908
ps1 . Append ( "$csvdata = while ($line = $stream_in.ReadLine()) {\r \n " ) ;
891
909
ps1 . Append ( "\t [PSCustomObject]@{\r \n " ) ;
892
910
ps1 . Append ( col_fixed ) ;
893
- ps1 . Append ( "\t }\r \n }\r \n \r \n " ) ;
911
+ ps1 . Append ( "\t }\r \n }\r \n $stream_in.Dispose() \r \n \r \n " ) ;
894
912
}
895
913
else
896
914
{
@@ -957,13 +975,22 @@ public static void GeneratePowerShell(CsvDefinition csvdef)
957
975
ps1 . Append ( "\t }\r \n " ) ;
958
976
ps1 . Append ( "}\r \n \r \n " ) ;
959
977
960
- ps1 . Append ( "# Merge datasets example, to join on multiple columns use a list, for example: on=['patient_id', 'center_id'] \r \n " ) ;
961
- ps1 . Append ( "# $merged_df = Join-Object -Left $PSCustomObject -Right $DataTable -LeftJoinProperty 'ID' -RightJoinProperty 'IDD' -ExcludeRightProperties 'Junk' -Prefix 'R_' | Format-Table # same key column name \r \n " ) ;
962
- ps1 . Append ( "# $merged_df = Join-Object -Left $PSCustomObject -Right $DataTable -LeftJoinProperty 'ID ' -RightJoinProperty 'IDD ' -ExcludeRightProperties 'Junk' -Prefix 'R_' | Format-Table # different key column names \r \n \r \n " ) ;
978
+ ps1 . Append ( "## Merge datasets in PowerShell requires custom external modules which goes beyond the scope of this generated script \r \n " ) ;
979
+ ps1 . Append ( "##Install-Module -Name Join-Object \r \n " ) ;
980
+ ps1 . Append ( "## $merged_df = Join-Object -Left $patients -Right $visits -LeftJoinProperty 'PATIENT_ID ' -RightJoinProperty 'PATIENT_ID ' -ExcludeRightProperties 'Junk' -Prefix 'R_' | Format-Table\r \n \r \n " ) ;
963
981
964
982
ps1 . Append ( "# csv write new output\r \n " ) ;
965
983
ps1 . Append ( "$filenew = $pathname + \" output.txt\" \r \n " ) ;
966
- ps1 . Append ( string . Format ( "$csvnew | Export-Csv -Path $filenew -Delimiter \" `t\" -NoTypeInformation\r \n " , separator ) ) ;
984
+ ps1 . Append ( string . Format ( "$csvnew | Export-Csv -Path $filenew -Encoding utf8 -Delimiter \" `t\" -NoTypeInformation\r \n \r \n " , separator ) ) ;
985
+
986
+ ps1 . Append ( "# alternatively, write as fixed width\r \n " ) ;
987
+ ps1 . Append ( "#$stream_out = New-Object System.IO.StreamWriter $filenew\r \n " ) ;
988
+ ps1 . Append ( "#foreach ($row in $csvnew)\r \n " ) ;
989
+ ps1 . Append ( "#{\r \n " ) ;
990
+ ps1 . Append ( "#\t # {colnr,width} space etc, negative width means left aligned\r \n " ) ;
991
+ ps1 . Append ( string . Format ( "#\t $stream_out.WriteLine((\" {0}\" -f {1}))\r \n " , col_fixed_write1 . Trim ( ) , col_fixed_write2 ) ) ;
992
+ ps1 . Append ( "#}\r \n " ) ;
993
+ ps1 . Append ( "#$stream_out.Dispose()\r \n " ) ;
967
994
968
995
// create new file
969
996
notepad . FileNew ( ) ;
@@ -974,4 +1001,4 @@ public static void GeneratePowerShell(CsvDefinition csvdef)
974
1001
}
975
1002
}
976
1003
}
977
- }
1004
+ }
0 commit comments