Remove Commas in CSV without adding extra columns - windows

I have this script intended to remove "extra commas" within a string in a CSV.
#echo off & setlocal EnableDelayedExpansion
echo Removing commas from FILE CSV file...
type nul > "H:\CONVERT_CSV.csv"
for /F "delims=" %%i in ('type "H:\FILE.csv"') do (
set row=%%i
set row=!row:"=!
echo.!row!>> "H:\CONVERT_CSV.csv"
)
But my script, instead of removing the comma, it adds an extra field within that record.
I know the problem sits in the set row!row"=! line, but I can not figure out what.
Please help
Example
From this:
Column A,Column B
Now,All,2
Then,After
To this:
Column A,Column B
Now,All 2
Then,After
EDIT: EXTRA INFO:
the csv has:
123 ,the time, "Gone, For good", 2023
Then it should become:
123 ,the time, "Gone For good", 2023

Related

Insert a string in a fixed position in a text file

I need to insert 4 strings in a text file in a fixed position:
first string: the product, at the beginning of the line, column 1
second one: %%b, column 20
third one: %%c, column 33
fourth one: doesn't exist, column 42.
I always want the strings written at the exact same position no matter how longer are the other strings before. so it should looks like the example below:
the product ergerzgtrg 65ggrth784rjhnjgbkljn doesn't exist
the product reggbrtbhtrergzthrjhlyoiul rtjntjrez doesn't exist
the product zef rt doesn't exist
Here's my code:
FOR /F "delims=; tokens=1-7*" %%a in (mytextfile.txt) do (
if "%%e"=="Unkown" (
echo the product %%b %%c doesn't exist>>Unkown_product.txt
)
)
mytextfile.txt looks like this:
K5134908-Blabla_4;K5134908;Blabla_4;01-69423;Unkown;K5134908-Blabla_4-516245;K5134908-Blabla_4-516245;
K2602207-Blabla_2;K2602207;Blabla_2;01-81111;Unkown;K2602207-Blabla_2-516245;K2602207-Blabla_2-516245;
K2602006-Blabla_3;K2602006;Blabla_3;01-82789;Unkown;K2602006-Blabla_3-516245;K2602006-Blabla_3-516245;
K2601507-Blabla_4;K2601507;Blabla_4;01-75135;Unkown;K2601507-Blabla_4-516245;K2601507-Blabla_4-516245;
Is there any way to do that within a batch file (.bat)?
Add enough spaces to the values (20 in my example), then cut the first [whatever you need] characters (15 in my example):
#echo off
setlocal
FOR /F "delims=; tokens=1-7*" %%a in (mytextfile.txt) do (
if "%%e"=="Unkown" call :format "%%b" "%%c"
)
goto :eof
:format
set "b=%~1 "
set "c=%~2 "
set "b=%b:~0,15%"
set "c=%c:~0,15%"
echo the product %b% %c% doesn't exist>>Unkown_product.txt
Note: the search string is "Unkown" according to your file example (probably a typo, I guess it should be "Unknown")

Extract the first quoted string of each line of a text file

I want to read through a text file with a lot of lines.
in the beginning of each line, i have a string between quotes, then a coma and then the rest of the lines, Ex.:
"CBL003","C3/C5 // <>SdcdUB","",0,1,"PfcdDT_gerergv","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",
"CBL004","C3<.<C7 // <>SqsxUB","",0,1,"PDzesdxT_esfdczec","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,""
What I want is to read through the file, and extract the first line put it in a different text file and name it with the first strings of the line that it contains. Ex.:
In the above example, I should have the text file CBL003.txt that contains:
"CBL003","C3/C5 // <>SdcdUB","",0,1,"PfcdDT_gerergv","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",
and a second file text CBL004.txt that contains :
"CBL004","C3<.<C7 // <>SqsxUB","",0,1,"PDzesdxT_esfdczec","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,""
I already have a code that read through each line :
FOR /F "tokens=*" %%a IN (C:\SourceFile.txt) DO (
ECHO %%a
)
But I don't know how to extract the first part of each line
You need tokens=1 because you want %%a to be set to only the first token, and you need delims=, to specify the comma as the delimiter. You can remove the quotes, if you want, by using %%~a. Type for /? for help.
FOR /F "tokens=1 delims=," %%a IN (C:\SourceFile.txt) DO (
ECHO %%~a
)

Windows Batch Scripting: Parse CSV file and extract data

I am new to windows batch scripting so pardon my ignorance. I have a CSV file that looks like this:
ColumnA,ColumnB,ColumnC
01/02/2015,ABC,111
01/03/2015,DEF,222
01/03/2015,HHH,333
01/05/2015,XYZ,767
The number of rows in this file will vary but the columns will remain the same. I need to extract the date column A, row 1 and date from column A, last row.
In this case I need to extract 01/02/2015 and 01/05/2015. Next I want to store both these dates in separate variable.
How can I achieve this? I have no idea where to begin. The only thing I have is this:
for /F "tokens=1 delims=," %%s in (IntFlow.csv) do #echo %%s
This one line of code cuts the first column and echo's it. I don't know what do next. Please advice.
Use a for /f loop to parse the csv file line-by-line. Use skip=1 to skip the header row. Only set the first variable if it isn't already defined. Then just set the last variable on every loop iteration. It's bound to be correct sooner or later, right? :)
#echo off
setlocal
set "first="
set "last="
for /f "skip=1 usebackq delims=," %%I in ("test.csv") do (
if not defined first set "first=%%~I"
set "last=%%~I"
)
echo %first%
echo %last%
tokens=1 is already the default behavior. usebackq allows you to enclose the filename in quotes, so it should work if your csv filename includes a space or some other unsavory character in the name.
I see in your comment you've decided to use PowerShell instead. Here's a PowerShell script that'll let you populate variables with the dates.
$csv = ipcsv test.csv
$first = ($csv | select -first 1 | %{ $_.ColumnA })
$last = ($csv | select -last 1 | %{ $_.ColumnA })
$first
$last

Batch script issue for getting dynamic column of csv file?

This is script which i am using for export the data from csv and write into a .trn extension file. The code execute for certain column of csv file data right, i mean to say if the column no indexing A to Z, code execute right,but when after the Z column means for AA,AB ect the data gets wrong. Code you can see here:
Batch Script
#echo off
set "line1=^%%a,cf,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,,,,,,,,,,,,,"
set "line2=^%%a,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,qbconid,,,^%%b,,,,,,,,,"
.......................................................................................
.......................................................................................
set "line26=^%%a,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,txtstate,,,^%%z,,,,,,,,,"
set "line27=^%%a,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,pmtper,,,^%%aa,,,,,,,,,"
set "line28=^%%a,#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,minfee,^%%ab,,,,,,,,,"
(for /F "tokens=1-28 skip=1 delims=," %%a in ('type "NewPortfolios.csv"') do (
echo %line1%
echo %line2%
............
............
echo %line26%
echo %line27%
echo %line28%
)) >"file.trn"
And the csv file data is:
A B C AA AB
$portcode $qbconId $name ....... $pmtper #minfee
asingh12 123456789 Ajay Singh....... ajay 123
Output is:
asingh12,cf,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,,,,,,,,,,,,,
asingh12,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,qbconid,,,123456789,,,,,,,,,
...................................................................................
asingh12,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,pmtper,,,asingh12a,,,,,,,,,
asingh12,#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,minfee,asingh12b,,,,,,,,,
In the above 2 lines you can see that the column value is coming wrong.
As you can see CSV data the last 2 column value should be ajay and 123 but it is coming as asingh12a and asingh12b.These are the values of a and b.It should come from AA and AB.
Can someone please help me in this.?
**EDITED HERE**
Hi Aacini - Hope you are doing well. I am facing again an issues with .csv file convert in to .txt file. Now this time getting a data from .csv with Quotes(") that's why the batch script is not running properly. Please find the sample data from .csv in to .txt format-
"_portcode","_qbconId","_name","_address","_city","_state","_zip","_bmeth","_ffee","_brak1","_rate0","_brak2","_rate1","_brak3","_rate2","_brak4","_rate3","_brak5","_rate4","_rate5","_bre","_custact","_custody","_qbmgrid","_refby","_txstate","_pmtper","_minfee"
"schorira","001c000000WtrL1AAJ",,"9232 Collegeview Cir","Bloomington","MN","55437","T",,"1000000.0","1.25",,,,,,,,,"1.0","Robert M. Schofield IRA Rollover",,"Schwab","JTB","Patrick Stephens","MN","Quarterly","0". Previous time in .csv data there is no Quotes. Please help me out. If you want more clarification from my side please let me know. I appreciate your help in advance.
Hi Acini,
Till now we are waiting for your reply. Please help me out.
EDIT: I fixed a couple details, it should correctly run now.
EDIT #2: Subtle bug fixed, and an example added
#echo off
set "letter=abcdefghijklmnopqrstuvwxyz"
set "line1=!a!,cf,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,,,,,,,,,,,,,"
set "line2=!a!,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,qbconid,,,!b!,,,,,,,,,"
set "line26=!a!,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,txtstate,,,!z!,,,,,,,,,"
set "line27=!a!,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,pmtper,,,!aa!,,,,,,,,,"
set "line28=!a!,#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,minfee,!ab!,,,,,,,,,"
setlocal EnableDelayedExpansion
(for /F "usebackq skip=1 delims=" %%a in ("NewPortfolios.csv") do (
set i1=0
set "line=%%a"
for %%b in ("!line:,=" "!") do for /F "tokens=1,2" %%i in ("!i1! !i2!") do (
if %%i lss 26 (
set var=!letter:~%%i,1!
) else (
set var=a!letter:~%%j,1!
)
set "!var!=%%~b!
set /A i1+=1, i2=i1-26
)
echo %line1%
echo %line2%
echo %line26%
echo %line27%
echo %line28%
)) > file.trn
Previous Batch program can output up to a maximum of 52 columns (a..z aa..az), but it is very easy to increment this range. With this input data:
A, B, C, D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z, AA, AB
$portcode,$qbconId,$name, D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,$pmtper,#minfee
asingh12,123456789,Ajay Singh,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,ajay,123
the output is:
asingh12,cf,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,,,,,,,,,,,,,
asingh12,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,qbconid,,,123456789,,,,,,,,,
asingh12,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,txtstate,,,Z,,,,,,,,,
asingh12,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,pmtper,,,ajay,,,,,,,,,
asingh12,#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,y,minfee,123,,,,,,,,,

Add commas to txt file from batch script in Windows

I have this format on a .txt file:
"Rosina Merola (rosina_merola#hotmail.com)" <rosina_merola#hotmail.com>
"Sabina Morales (sabinamorales#gmail.com)" <sabinamorales#gmail.com>
"Sorella Blanco (zoreblanco#hotmail.com)" <zoreblanco#hotmail.com>
"Eduardo Schmilinsky Leal" <ejsl41#hotmail.com>
"Elba Rodríguez" <elbameister#gmail.com>
Ernesto Ramirez <ernestoramirezricca#hotmail.com>
Some of the names have ""and a few don't, as you can see in this example.
However, i need to add a comma ',' after each name and before the email line <>
I thought that first adding the "" to every name, it could be easier then to add the commas, i have this code:
#echo off
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (nombresemails.txt) do (
echo "%%a", >> nombresemailscomillas.txt
)
It works but it adds the "" in this format:
"Adam Podlinski <apodlinski#cantv.net> ",
"Adam Podlinski 2 <apodlinski#yahoo.com>",
""Aldo Gonzalez " <aldodanielg#hotmail.com>",
""Alejandr Rubin" <rubin4#cantv.net>",
""Alfredo Huguett " <alfredohuguettc#hotmail.com>",
""aainiguezf#gmail.com" <aainiguezf#gmail.com>",
No matter what are the characters it just adds the "" even if they already have it, and besides, it adds them to all the line, i just need to add them to every name, leaving the emails inside the <> without the "".
Anyway, this is just an approach i thought it could work, basically i just need to add a comma after every name and before the emails cointained in <>
there is actually some way achieve this on batch code?
Thanks in advance!
This works here:
#echo off
setLocal EnableDelayedExpansion
for /f "delims=" %%a in (nombresemails.txt) do (
set "line=%%a"
set "line=!line: <=,<!"
>>nombresemailscomillas.txt echo !line!
)

Resources