I have 11 plain text files which I want to open up in Excel. Each file has a different record type and different record length with the following attributes:
fixed column length
no blank lines in the files
no delimiters
trailing spaces in some or all of the files
When I open up the files in Excel, I have to go through the tedious task of using the wizard to define the columns. Would be nice to be able to convert the files into comma quoted ahead of time, that way Excel opens up with each column already defined. Column headers are not necessary.
I am writing a batch to put double quotes, comma, and more double quotes in-between each of the columns, with one double quotes at the beginning and one double quotes at the end. I have tested successfully with 7 columns so far. I wanted to get your thoughts before continuing with the syntax for the rest of the columns.
Is there a limit to the number of bytes that can be included in one command/statement? The file I have with the most number of columns to define is 50. This is going to make the syntax extremely long.
Example input records from one file:
AB012345AC 201399Long name 55555198900006589673101N008500000008200979999999999900015294260999999999999999
AB012345ABC 201399Another long name 11111194500000012023203N000313200001840909999999999900000260524999999999999999
AB012345ABCD 201399three times 33333199600003609819104N004265000008000979999999999900005823119999999999999999
I want the output to be like this, for example:
"AB","012345","AC ","2013","99","Long name ","55555","1989","0000658967","31","01","N","00850000","00082","0097","999999","99999","0001529426","0","999999999999999"," "
"AB","012345","ABC ","2013","99","Another long name ","11111","1945","0000001202","32","03","N","00031320","00018","4090","999999","99999","0000026052","4","999999999999999"," "
"AB","012345","ABCD ","2013","99","three times ","33333","1996","0000360981","91","04","N","00426500","00080","0097","999999","99999","0000582311","9","999999999999999"," "
Here is the batch code:
@echo off
setlocal EnableDelayedExpansion
cd/d c:\download
if ErrorLevel 1 (
cls
echo ---------------------------------------------
rem
echo ERROR ERROR ERROR
echo Folder "c:\download" not found
echo Press a key to close this window and stop the program.
rem
echo ---------------------------------------------
pause
goto FINISH
)
if exist test1.csv del test1.csv
set single_quote="
set quotes=","
for /f "delims=" %%A in (myfile1.TXT) do (
set partial=%%A
set newstring=%single_quote%!partial:~0,2!%quotes%!partial:~2,6!%quotes%!partial:~8,6!%quotes%!partial:~14,4!%quotes%!partial:~18,2!%quotes%!partial:~20,30!%quotes%!partial:~50,5!%single_quote%
echo !newstring!>> test1.csv
)
start "" test1.csv
:FINISH
exit
Note: in the code I have defined only 7 columns so far, as stated above, whereas in the example input records I have listed the entire line of one of the input files.
Thanks in advance.