Skip to content

Instantly share code, notes, and snippets.

@BroVic
Created July 30, 2019 08:12
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 BroVic/d0a99a490931e6628edb08e33d24c4ec to your computer and use it in GitHub Desktop.
Save BroVic/d0a99a490931e6628edb08e33d24c4ec to your computer and use it in GitHub Desktop.
When Excel messes up your CSVs
# 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