Skip to content

Instantly share code, notes, and snippets.

@gioxx
Created February 7, 2025 13:08
Show Gist options
  • Save gioxx/a600e06f586c9498d7ad069cf344f190 to your computer and use it in GitHub Desktop.
Save gioxx/a600e06f586c9498d7ad069cf344f190 to your computer and use it in GitHub Desktop.
Funzione PowerShell che permette di modificare il delimitatore utilizzato in un file CSV (da virgola a punto e virgola, e viceversa). Accetta parametri da riga di comando (file da lavorare, opzione di delimitazione da utilizzare). Il file che gli viene dato in pasto, viene modificato (quindi occhio, fai prima backup del tuo file, se necessario).
function Update-CSVDelimiter {
param (
[string]$FilePath,
[string]$Encoding = "ISO-8859-1", # Default encoding, can be changed
[switch]$ToComma, # Switch to convert ";" to ","
[switch]$ToSemicolon # Switch to convert "," to ";"
)
# Check if the specified file exists
if (Test-Path $FilePath) {
try {
# Read the file content with the specified encoding
$content = Get-Content -Path $FilePath -Encoding $Encoding
# Determine the direction of the conversion
if ($ToComma) {
# Convert ";" to ","
$newContent = $content -replace ';', ','
} elseif ($ToSemicolon) {
# Convert "," to ";"
$newContent = $content -replace ',', ';'
} else {
# Default behavior: Convert "," to ";"
$newContent = $content -replace ',', ';'
}
# Write the modified content back to the file with the same encoding
$newContent | Out-File -FilePath $FilePath -Encoding $Encoding -Force
Write-Output "Conversion successfully completed."
} catch {
# Handle any errors that occur during the process
Write-Output "An error occurred: $_"
}
} else {
# Inform the user if the file does not exist
Write-Output "The specified file does not exist."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment