Swapping Columns in a CSV file

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
#1
Code:
@setlocal
@echo off
set csvFile=%@Name[%_batchname].csv
text > %csvFile
ItemNumber,ItemName,Price
1234,Widget 1234,9.99
5678,Widget 5678,5.99
endtext

echo Contents of %csvFile:
type %csvFile
echo.

echo Swapping Col1 and Col2:
do theRecord in @%csvFile
  set Col1=%@word[",",0,%theRecord]
  set Col2=%@word[",",1,%theRecord]
  set Col3=%@word[",",2,%theRecord]
  echo %Col2,%Col1,%Col3
enddo
echo.

echo Swapping Col1 and Col2: using TPIPE
set Type=10
set Locate=0
set Param1=1
set Param2=1
set MoveTo=2
set nDelimiter=0
set CustomDelimiter=""
set HasHeader=1
type %csvFile | tpipe /selection=%Type,%Locate,%Param1,%Param2,%MoveTo,%nDelimiter,%CustomDelimiter,%HasHeader
endlocal