Skip to content

Instantly share code, notes, and snippets.

@auberginehill
Last active July 27, 2018 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save auberginehill/f278871a33f48fe07c522cba4a9a63e8 to your computer and use it in GitHub Desktop.
Save auberginehill/f278871a33f48fe07c522cba4a9a63e8 to your computer and use it in GitHub Desktop.
Appends CSV-files with Windows PowerShell v2.0, adds two custom extra columns to the merged file & replaces an expression inside the outputted CSV-file.
<#
Append-Csv.ps1
#>
# The directory name contains at least one underscore in its name, and the first part
# is used in the outputted filename as a denominator ($directory = "denominator").
# Note: UTF8 encoding in source files, perhaps? (for special characters et al.)
$path = "$env:USERPROFILE\Documents\denominator_split_files"
$directory = (Split-Path $path -Leaf).Split("_")[0]
$csv_file = "$path\merged_csvfile_$directory.csv"
$temp_file = "$path\merged_csvfile_temp_$directory.csv"
$list = Get-ChildItem -Path $path -Filter *.csv
If (($list.Count) -ge 2) {
"Merging $($list.Count) CSV-files."
} ElseIf (($list.Count) -eq 1) {
Return "Found one CSV-file - didn't merge."
} Else {
Return "Didn't find any CSV-files."
} # Else (If $list.Count)
ForEach ($item in $list) {
# Add columns "Type" (with values "original filename without the file-extension") and
# "Source" to the appended CSV-files and merge the files.
# Note: Append parameter of Export-Csv was introduced in PowerShell 3.0.
# $images_selection | Export-Csv $logfile_path -Delimiter ';' -NoTypeInformation -Encoding UTF8 -Append
Import-Csv $item.FullName -Header "longitude","latitude","Direction","ID" |
Select-Object *,@{ Label='Type'; Expression={"$($item.BaseName)"} },@{ Label='Source'; Expression={"scdb"} } |
ConvertTo-Csv -Delimiter ';' -NoTypeInformation | Select-Object -Skip 1 |
Out-File -FilePath $temp_file -Append -Encoding UTF8
} # ForEach
Import-Csv "$temp_file" -Delimiter ';' -Header "longitude","latitude","Direction","ID","Type","Source" |
Export-Csv "$csv_file" -Delimiter ';' -NoTypeInformation -Encoding UTF8
Remove-Item $temp_file -Force
# Replace an expression in the similar manner as "Find and Replace"
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('-]',']') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
@auberginehill
Copy link
Author

[reserved]

@auberginehill
Copy link
Author

<#
Append-Csv_greece.ps1
#>

# The directory name contains at least one underscore in its name, and the first part 
# is used in the outputted filename as a denominator ($directory = "denominator").
# Note: UTF8 encoding in source files, perhaps? (for special characters et al.)
$path = "$env:USERPROFILE\Desktop\scdb\greece_split_garmin_small"
$directory = (Split-Path $path -Leaf).Split("_")[0]
$csv_file = "$path\merged_csvfile_$directory.csv"
$temp_file = "$path\merged_csvfile_temp_$directory.csv"

$list = Get-ChildItem -Path $path -Filter *.csv
If (($list.Count) -ge 2) {
    "Merging $($list.Count) CSV-files."
} ElseIf (($list.Count) -eq 1) {
    Return "Found one CSV-file - didn't merge."
} Else {
    Return "Didn't find any CSV-files."
} # Else (If $list.Count)

ForEach ($item in $list) {
    # Add columns "Type" (with values "original filename without the file-extension") and  
    # "Source" to the appended CSV-files and merge the files.
    # Note: Append parameter of Export-Csv was introduced in PowerShell 3.0.
    # $images_selection | Export-Csv $logfile_path -Delimiter ';' -NoTypeInformation -Encoding UTF8 -Append
    Import-Csv $item.FullName -Header "longitude","latitude","Direction","ID" |
        Select-Object *,@{ Label='Type'; Expression={"$($item.BaseName)"} },@{ Label='Source'; Expression={"scdb"} } |
        ConvertTo-Csv -Delimiter ';' -NoTypeInformation | Select-Object -Skip 1 |
        Out-File -FilePath $temp_file -Append -Encoding UTF8
} # ForEach

Import-Csv "$temp_file" -Delimiter ';' -Header "longitude","latitude","Direction","ID","Type","Source" |
    Export-Csv "$csv_file" -Delimiter ';' -NoTypeInformation -Encoding UTF8

Remove-Item $temp_file -Force

# Replace an expression in the similar manner as "Find and Replace"
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('-]',']') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('SCDB_','') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('beidseitig','both directions') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Athen Richtung Tripoli, Tunnelmitte','SW') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('From Analipsi','E') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Flughafen in Rtg. Vlicho','both directions') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Richtung','Towards') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Rtg.','Towards') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('abgebaut','removed ') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('>Rethymnon','E') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('18 km before the center of Thessaloniki, To Thessaloniki','NW') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('80,1,285','NW') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Lefkada','both directions') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('perhaps inactive','removed (?)') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('towards lavrio','S') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Towards Grevena','SW') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Towards Kavala','E') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Towards Kozani','W') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Towards Thessaloniki','NE') } | Out-File -FilePath $csv_file -Force -Encoding UTF8

@auberginehill
Copy link
Author

<#
Append-Csv_finland.ps1
#>

# The directory name contains at least one underscore in its name, and the first part 
# is used in the outputted filename as a denominator ($directory = "denominator").
# Note: UTF8 encoding in source files, perhaps? (for special characters et al.)
$path = "$env:USERPROFILE\Desktop\scdb\finland_split_garmin_small"
$directory = (Split-Path $path -Leaf).Split("_")[0]
$csv_file = "$path\merged_csvfile_$directory.csv"
$temp_file = "$path\merged_csvfile_temp_$directory.csv"

$list = Get-ChildItem -Path $path -Filter *.csv
If (($list.Count) -ge 2) {
    "Merging $($list.Count) CSV-files."
} ElseIf (($list.Count) -eq 1) {
    Return "Found one CSV-file - didn't merge."
} Else {
    Return "Didn't find any CSV-files."
} # Else (If $list.Count)

ForEach ($item in $list) {
    # Add columns "Type" (with values "original filename without the file-extension") and  
    # "Source" to the appended CSV-files and merge the files.
    # Note: Append parameter of Export-Csv was introduced in PowerShell 3.0.
    # $images_selection | Export-Csv $logfile_path -Delimiter ';' -NoTypeInformation -Encoding UTF8 -Append
    Import-Csv $item.FullName -Header "longitude","latitude","Direction","ID" |
        Select-Object *,@{ Label='Type'; Expression={"$($item.BaseName)"} },@{ Label='Source'; Expression={"scdb"} } |
        ConvertTo-Csv -Delimiter ';' -NoTypeInformation | Select-Object -Skip 1 |
        Out-File -FilePath $temp_file -Append -Encoding UTF8
} # ForEach

Import-Csv "$temp_file" -Delimiter ';' -Header "longitude","latitude","Direction","ID","Type","Source" |
    Export-Csv "$csv_file" -Delimiter ';' -NoTypeInformation -Encoding UTF8

Remove-Item $temp_file -Force

# Replace an expression in the similar manner as "Find and Replace"
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('-]',']') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('SCDB_','') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('beidseitig','both directions') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('bs','both directions') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('> Heinola','W') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('> North-West','NW') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('> South-East','SE') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('>North-West','NW') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('>South-East','SE') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('60 pori Noormarkku','SW') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('east to west','W') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('east-bound','E') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('eastbound','E') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Eastbound','E') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('N, 80 im Winter','N') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('NO','NE') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('SO','SE') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Rtg. Juva','NE') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('video, O','E') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('west to east','E') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('west-bound','W') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('westbound','W') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Westbound','W') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('W, O','W, E') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('SEurce','Source') } | Out-File -FilePath $csv_file -Force -Encoding UTF8

@auberginehill
Copy link
Author

<#
Append-Csv_sweden.ps1
#>

# The directory name contains at least one underscore in its name, and the first part 
# is used in the outputted filename as a denominator ($directory = "denominator").
# Note: UTF8 encoding in source files, perhaps? (for special characters et al.)
$path = "$env:USERPROFILE\Desktop\scdb\sweden_split_garmin_small"
$directory = (Split-Path $path -Leaf).Split("_")[0]
$csv_file = "$path\merged_csvfile_$directory.csv"
$temp_file = "$path\merged_csvfile_temp_$directory.csv"

$list = Get-ChildItem -Path $path -Filter *.csv
If (($list.Count) -ge 2) {
    "Merging $($list.Count) CSV-files."
} ElseIf (($list.Count) -eq 1) {
    Return "Found one CSV-file - didn't merge."
} Else {
    Return "Didn't find any CSV-files."
} # Else (If $list.Count)

ForEach ($item in $list) {
    # Add columns "Type" (with values "original filename without the file-extension") and  
    # "Source" to the appended CSV-files and merge the files.
    # Note: Append parameter of Export-Csv was introduced in PowerShell 3.0.
    # $images_selection | Export-Csv $logfile_path -Delimiter ';' -NoTypeInformation -Encoding UTF8 -Append
    Import-Csv $item.FullName -Header "longitude","latitude","Direction","ID" |
        Select-Object *,@{ Label='Type'; Expression={"$($item.BaseName)"} },@{ Label='Source'; Expression={"scdb"} } |
        ConvertTo-Csv -Delimiter ';' -NoTypeInformation | Select-Object -Skip 1 |
        Out-File -FilePath $temp_file -Append -Encoding UTF8
} # ForEach

Import-Csv "$temp_file" -Delimiter ';' -Header "longitude","latitude","Direction","ID","Type","Source" |
    Export-Csv "$csv_file" -Delimiter ';' -NoTypeInformation -Encoding UTF8

Remove-Item $temp_file -Force

# Replace an expression in the similar manner as "Find and Replace"
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('-]',']') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('SCDB_','') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace (', im Tunnel','') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace (', both lanes','') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('between Karlstad and Molkom, ','') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('kersberga and Roslags Kulla, ','') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('between Enk','') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('between ','') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('bs','both directions') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('BS','both directions') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace (', removed',', removed ') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('north-bound','N') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('northbound','N') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('direction North','N') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('direction NO','NE') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('south-bound','S') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('southbound','S') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('direction South','S') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('direction SW','SW') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('east-bound','E') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Eastbound','E') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('direction East','E') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('west-bound','W') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('westbound','W') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('direction W','W') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Rtg. NW','NW') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Rtg. NO','NE') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Rtg. N','N') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Rtg. Ost','E') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Rtg. O','E') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Rtg. SW','SW') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Rtg. SO','SE') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Rtg. S','S') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Rtg. West','W') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Rtg. W','W') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('0km','0 km') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('Rtg.','Towards') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('ping and Sala, N','N') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('ping and Sala, NW','NW') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('ping and Sala, S','S') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('ping and Sala','both directions') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('>Boxholm','NE') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('dir Arninge','N') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('direction Munksund','both directions') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('direction Pite havsbad','S') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('direction Pitea','N') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('From flemmingsberg to estuna','both directions') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('K50S60@90','both directions') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('O,','E,') } | Out-File -FilePath $csv_file -Force -Encoding UTF8
(Get-Content $csv_file) | ForEach-Object { $_ -replace ('vers Grisslehamn','both directions') } | Out-File -FilePath $csv_file -Force -Encoding UTF8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment