I have a batch script which should remove extra commas in a field, (field 13), and it is working fine.
However, if I have other rows that have no data in this field, some of the good rows become affected.
In my input, only row 2, (do not count the header), has an issue
8455,343248202,6528,IVAN ,08/29/2022,2,0,1,EA,Required Supplies,08/22/2022,12/31/2022,,37449257,ABC~~~EFG@YAHOO.COM,31917105,Required Supplies
However, when I run the script, row 3
, 7
, 11
got affected, (these are good rows).
(Showing only 3 row as an example, an extra comma was added to the row causing data shifted over when displayed in xls)
8455,343248202,6528,IVAN,08/29/2022,3,0,1,EA,Handling Charges * Applicable only to books & supplies,08/22/2022,12/31/2022,,,37449257,ABC~~~EFG@YAHOO.COM,31917105
Input:
StoreNumber,PurchaseOrderNumber,Last4DIG,ParticipantName,PurchaseOrderDate,POLineNumber,UnitPrice,Quantity,UnitOfMeasure,PartDescription,StartDate,EndDate,LineDetail,ParticipantID,BuyerContacts,FacilityCode,DetailsFreeText
8455,343248202,6528,IVAN,08/29/2022,1,0,1,EA,Required Books,08/22/2022,12/31/2022,,37449257,ABC~~~EFG@YAHOO.COM,31917105,Required Books
8455,343248202,6528,IVAN ,08/29/2022,2,0,1,EA,Required Supplies,08/22/2022,12/31/2022,,37449257,ABC~~~EFG@YAHOO.COM,31917105,Required Supplies
8455,343248202,6528,IVAN,08/29/2022,3,0,1,EA,Handling Charges * Applicable only to books & supplies,08/22/2022,12/31/2022,,37449257,ABC~~~EFG@YAHOO.COM,31917105,Handling Charges * Applicable only to books & supplies
8455,343248202,6528,IVAN,08/29/2022,4,0,1,EA,Mandatory Taxes,08/22/2022,12/31/2022,,37449257,ABC~~~EFG@YAHOO.COM,31917105,Mandatory Taxes
8094,459244329,7152,ALFRED,08/29/2022,1,0,1,EA,Required Books,08/22/2022,12/18/2022,Textbooks, ebooks and manuals,32818318,ABC~~~EFG@YAHOO.COM,31016230,Required Books
8094,459244329,7152,ALFRED,08/29/2022,2,0,1,EA,Required Supplies,08/22/2022,12/18/2022,$50 worth of school supplies,32818318,ABC~~~EFG@YAHOO.COM,31016230,Required Supplies
8094,459244329,7152,ALFRED,08/29/2022,3,0,1,EA,Handling Charges * Applicable only to books & supplies,08/22/2022,12/18/2022,No more than 10%,32818318,ABC~~~EFG@YAHOO.COM,31016230,Handling Charges * Applicable only to books & supplies
8094,459244329,7152,ALFRED,08/29/2022,4,0,1,EA,Mandatory Taxes,08/22/2022,12/18/2022,Detailed,32818318,ABC~~~EFG@YAHOO.COM,31016230,Mandatory Taxes
8257,344249022,5745,GUILLERMO,08/29/2022,1,0,1,EA,Required Books,08/22/2022,08/22/2023,,38424677,ABC~~~EFG@YAHOO.COM,29001332,Required Books
8257,344249022,5745,GUILLERMO,08/29/2022,2,0,1,EA,Required Supplies,08/22/2022,08/22/2023,$50 PER TERM MAX UNLESS APPROVED BY VRC,38424677,ABC~~~EFG@YAHOO.COM,29001332,Required Supplies
8257,344249022,5745,GUILLERMO,08/29/2022,3,0,1,EA,Handling Charges * Applicable only to books & supplies,08/22/2022,08/22/2023,,38424677,ABC~~~EFG@YAHOO.COM,29001332,Handling Charges * Applicable only to books & supplies
8257,344249022,5745,GUILLERMO,08/29/2022,4,0,1,EA,Mandatory Taxes,08/22/2022,08/22/2023,,38424677,ABC~~~EFG@YAHOO.COM,29001332,Mandatory Taxes
Output:
StoreNumber,PurchaseOrderNumber,Last4DIG,ParticipantName,PurchaseOrderDate,POLineNumber,UnitPrice,Quantity,UnitOfMeasure,PartDescription,StartDate,EndDate,LineDetail,ParticipantID,BuyerContacts,FacilityCode,DetailsFreeText
8455,343248202,6528,IVAN,08/29/2022,1,0,1,EA,Required Books,08/22/2022,12/31/2022,,37449257,ABC~~~EFG@YAHOO.COM,31917105,Required Books
8455,343248202,6528,IVAN ,08/29/2022,2,0,1,EA,Required Supplies,08/22/2022,12/31/2022,,37449257,ABC~~~EFG@YAHOO.COM,31917105,Required Supplies
8455,343248202,6528,IVAN,08/29/2022,3,0,1,EA,Handling Charges * Applicable only to books & supplies,08/22/2022,12/31/2022,,,37449257,ABC~~~EFG@YAHOO.COM,31917105
8455,343248202,6528,IVAN,08/29/2022,4,0,1,EA,Mandatory Taxes,08/22/2022,12/31/2022,,37449257,ABC~~~EFG@YAHOO.COM,31917105,Mandatory Taxes
8094,459244329,7152,ALFRED,08/29/2022,1,0,1,EA,Required Books,08/22/2022,12/18/2022,Textbooks ebooks and manuals,32818318,ABC~~~EFG@YAHOO.COM,31016230,Required Books
8094,459244329,7152,ALFRED,08/29/2022,2,0,1,EA,Required Supplies,08/22/2022,12/18/2022,$50 worth of school supplies,32818318,ABC~~~EFG@YAHOO.COM,31016230,Required Supplies
8094,459244329,7152,ALFRED,08/29/2022,3,0,1,EA,Handling Charges * Applicable only to books & supplies,08/22/2022,12/18/2022,,No more than 10%,32818318,ABC~~~EFG@YAHOO.COM,31016230
8094,459244329,7152,ALFRED,08/29/2022,4,0,1,EA,Mandatory Taxes,08/22/2022,12/18/2022,Detailed,32818318,ABC~~~EFG@YAHOO.COM,31016230,Mandatory Taxes
8257,344249022,5745,GUILLERMO,08/29/2022,1,0,1,EA,Required Books,08/22/2022,08/22/2023,,38424677,ABC~~~EFG@YAHOO.COM,29001332,Required Books
8257,344249022,5745,GUILLERMO,08/29/2022,2,0,1,EA,Required Supplies,08/22/2022,08/22/2023,$50 PER TERM MAX UNLESS APPROVED BY VRC,38424677,ABC~~~EFG@YAHOO.COM,29001332,Required Supplies
8257,344249022,5745,GUILLERMO,08/29/2022,3,0,1,EA,Handling Charges * Applicable only to books & supplies,08/22/2022,08/22/2023,,,38424677,ABC~~~EFG@YAHOO.COM,29001332
8257,344249022,5745,GUILLERMO,08/29/2022,4,0,1,EA,Mandatory Taxes,08/22/2022,08/22/2023,,38424677,ABC~~~EFG@YAHOO.COM,29001332,Mandatory Taxes
Code:
@echo off
setlocal EnableDelayedExpansion
rem Process all files with .csv extension in current folder
for %%F in (*.csv) do (
ECHO/
ECHO Input: "%%F"
TYPE "%%F"
rem Each file have comma-separated columns: may be 17 columns or more
rem Keep columns 1-12 the same. After that, generate 5 columns more:
rem the last 4 columns are the same
rem the 5th before last column contain the rest of columns separated by space
(for /F "usebackq tokens=1-12* delims=," %%a in ("%%F") do (
set "restAfter12=%%m"
set "last="
set "lastBut1="
set "lastBut2="
set "lastBut3="
set "lastBut4="
for %%A in ("!restAfter12:,=" "!") do (
set "lastBut4=!lastBut4! !lastBut3!"
set "lastBut3=!lastBut2!"
set "lastBut2=!lastBut1!"
set "lastBut1=!last!"
set "last=%%~A"
)
echo %%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,%%i,%%j,%%k,%%l,!lastBut4:~5!,!lastBut3!,!lastBut2!,!lastBut1!,!last!
)) > "%%~NF.out"
ECHO Output: "%%~NF.out"
TYPE "%%~NF.out"
)
I would appreciate if someone could point out what was missing in my code to cause this issue.