Reputation: 3
I was writing a windows batch script to convert pipe delimited file to comma delimited. When i run it for a single file it does the conversion but when I am running it for the files in a folder it does not.
I have the code written for single file but it does not loop thru the folder..
@echo off
set "INPUT=%C:\temp\source\*.csv"
set "OUTPUT=%C:\temp\dest\"
setlocal EnableExtensions DisableDelayedExpansion
> "%OUTPUT%" (
for %%F in (%*) do
(
delims^=^ eol^= %%L in ("%INPUT%") do (
set "LINE=%%L"
setlocal EnableDelayedExpansion
echo(!LINE:^|^=,!
endlocal
)
)
)
endlocal
exit /B
i want to run the script on all the files in the folder
Upvotes: 0
Views: 517
Reputation: 38623
Here are my two powershell based comments, expanded for multiple csv files:
$folderPath = 'C:\temp\source\'
$folderPathDest = 'C:\temp\dest\'
Get-ChildItem $folderPath -Name -Filter *.csv |
ForEach-Object {
$filePath = $folderPath + $_
$filePathdest = $folderPathDest + $_
Import-Csv -Path $filePath -Delimiter '|' |
Export-Csv -Path $filePathDest –NoTypeInformation
}
$folderPath = 'C:\temp\source\'
$folderPathDest = 'C:\temp\dest\'
# As the pipe is a special character we need to escape it with a backslash
$stringToReplace = '\|'
$stringToAdd = ','
Get-ChildItem $folderPath -Name -Filter *.csv |
ForEach-Object {
$filePath = $folderPath + $_
$filePathdest = $folderPathDest + $_
(Get-Content -Path $filePath) -Replace $stringToReplace,$stringToAdd |
Set-Content -Path $filePathdest
}
In the latter, if using powershell-v3.0+ you can also include the -Raw
option to Get-Content
, i.e. (Get-Content -Path $filePath -Raw)
Upvotes: 1