let me describe my problem. I have a csv file exported from excel with large amount of data. The file has a title in the first row and column headings in the second. I need to extract only two columns (2nd and 3rd) from that file, put them to 1 column and send the output to another file.
Example:
Title
colA , colB , colC , colD ,...
abc , def , ghi , jkl ,...
abc , def , ghi , jkl ,...
abc , def , ghi , jkl ,...
abc , def , ghi , jkl ,...
The thing is, that the csv parser fails when it meets the row containing a string with - ( ) @ characters. (the loop treats them as delimiter I think, so it gives me an out of range error each time).
Here is what I already have.
@Echo off & setlocal EnableExtensions
setLocal EnableDelayedExpansion
REM creating and clearing files
copy /y NUL C:\list1.csv >NUL
copy /y NUL C:\list1_tmp.csv >NUL
copy /y NUL C:\exportedColumns.csv >NUL
copy /y NUL C:\Result.txt >NUL
set Result=C:\Result.txt
set Source=C:\sourcelist.csv
set list1=C:\list1.csv
set list1_tmp=C:\list1_tmp.csv
set expCol=C:\exportedColumns.csv
REM skip 1st two lines from source file and put to output file list1
for /f "skip=2 delims=*" %%a in (%Source%) do (echo %%a >>%list1%)
REM shorten each line to 500 chars and put it to new file
for /f "tokens=* delims=" %%a in ("%list1%") do (
set s=%%a
set s=%s:~0,500%
echo.%s% >> "%list1_tmp%"
)
REM ^^^^^^^^^^^ this is not working. It puts only 1 space to the output file
rem Parsing the csv file
rem Process the file:
call :ProcessFile < %list1_tmp%
exit /B
:ProcessFile
set /P line=
:nextLine
set line=:EOF
set /P line=
if "!line!" == ":EOF" goto :EOF
set i=0
for %%e in (%line%) do (
set /A i+=1
for %%i in (!i!) do (
if %%i==1 echo %%~e >> %expCol%
if %%i==2 echo %%~e >> %expCol%
)
if %%i==3 goto nextLine
REM I don't want it to process all the columns
)
goto nextLine
I'd like to ask you to look at this and help me to get 2 columns into one and put the output to 1 file.
I'd be extremely grateful.
>>"%list1_tmp%" echo.!s!to delay the expansion of%s%? (I just reordered the command redirection to keep from echoing a trailing whitespace into%list1_tmp%, but that's not the important part.)