vba - Excel CSV files delimiter change -
i'm creating in excel sub-folder in directory , save there multiple csv-files excel workbook
my problem need on system list separator ','. csv files getting read system default list separator ';'. cannot change this
so need change ',' in csv files ';'. idea achieve using powershell.
my first attempt change delimiter of csv after creating in excel passing script file-name. manage change delimiter file struggle pass pathname script (no error no change in file):
script code:
param([string]$path) $content = [io.file]::readalltext($path) #readparameter import-csv -path $content -delimiter ','|export-csv -path c:\users\desktop\temp.csv -delimiter ';' -notypeinformation #export csv-file ; (get-content c:\users\desktop\temp.csv) | % {$_ -replace '"', ""} | out-file -filepath c:\users\desktop\temp.csv -force -encoding ascii #remove " file remove-item -path $content #remove old csv-file rename-item -path c:\users\desktop\temp.csv -newname $content #change file name
excel call:
call shell("powershell.exe -executionpolicy unrestricted -file c:\users\desktop\delimiterchange.ps1 -path """ & location & """", 1)
thank you
param([string]$path) $content = [io.file]::readalltext($path) #readparameter import-csv -path $content -delimiter ','|export-csv -path c:\users\desktop\temp.csv -delimiter ';' -notypeinformation
your code reads content of csv (assuming path csv passed via parameter -path
) , tries pass path import-csv
. change above this:
param([string]$path) import-csv -path $path | export-csv -path c:\users\desktop\temp.csv -delimiter ';' -notype
you can replace content of file if run import-csv
in expression:
(import-csv -path $path) | export-csv -path $path -delimiter ';' -notype
i'd recommend keeping double quotes, if must remove them can in pipeline this:
(import-csv -path $path) | convertto-csv -delimiter ';' -notype | % { $_ -replace '"', '' } | set-content -path $path
Comments
Post a Comment