Created
July 30, 2019 08:12
-
-
Save BroVic/d0a99a490931e6628edb08e33d24c4ec to your computer and use it in GitHub Desktop.
When Excel messes up your CSVs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Converting Date Entries in a CSV file back to POSIX style (after being mucked up by MS Excel) | |
# Note: The first column is for Dates | |
Param( | |
[string]$file, | |
[switch]$Detailed | |
) | |
$tempfile = New-TemporaryFile | |
Get-Content -Path $file | Set-Content $tempfile.FullName | |
$text = Get-Content -Path $file | |
$text | | |
ForEach-Object { | |
$entry = $_.substring(0, $_.indexof(",")) | |
if ($entry -match "^\d{2}/\d{2}/\d{4}$") { | |
$values = $entry.split("/") | |
$newDate = $values[2] + "-" + $values[1] + "-" + $values[0] | |
$_ = $_.replace($entry, $newDate) | |
} | |
$_ | |
} | | |
Set-Content -Path $file | |
if ($Detailed) { | |
Write-Host "A backup of the original file can be found at $tempfile" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment