-2

I am a newbie in batch script and I am trying to achieve the following: loop through multiple files, count the # of commas on each line then remove extra commas if it is greater than 10. I can only get to the point where I get the count but I am stuck there. All fields are required. No carriage return. The extra comma will only happen in the field after the 9th comma

Example of data in csv file:

Row 1, (good data)

123,235252,6376,test1,08/11/2022,2,0,1,EA,Required text, pencil ,pen

Row 2, (bad data)

456,235252,6376,test2,08/11/2022,2,0,1,EA,Required,text, pencil ,pen

In row 2, Required text has an extra comma and should be removed. It should look like the row above

So the logic I would like to have is If the number of commas is 10 for the row, I will go to the next line If the number of commas greater than 10, then I will remove the one after the 9th comma since extra commas will only happen in that field Please note, I cannot put double quote around the field

@echo on
setlocal enabledexpansion enableddelayedexpansion

pause


set "inputFile=test.csv"
set "searchChar=,"

set count16=16
pause
for /f "delims=" %%a in ('
 findstr /n "^" "%inputFile%"
 ') do for /f "delims=:" %%b in ("%%~a") do (
    set "line=%%a"

    pause
    for /f %%c in ('
     cmd /u /v /e /q /c"(echo(!line:*:=!)"^|find /c "%searchChar%"
     ') do  set count=%%c echo  %%c echo here echo %count% echo  %count16% echo %%c line %%b has %%c characters 
        if %count16% equ %count% (echo   ***hit)
    )
    pause
)
pause
Compo
  • 36,585
  • 5
  • 27
  • 39
SForum
  • 3
  • 3
  • You need to also submit an example CSV content, and **must** confirm whether it is possible that one or more fields can be empty, whether any fields can contain carriage returns or line feeds, and if any of the fields in any record can contain commas. All of those things can seriously affect whether any code offered will work for you. My advice however is that you use PowerShell for this, not cmd.exe commands and other built-in utilities. – Compo Aug 25 '22 at 18:02
  • However, I have no knowledge of powershell – SForum Aug 25 '22 at 18:07
  • TBF, based upon this line alone, ```') do set count=%%c echo %%c echo here echo %count% echo %count16% echo %%c line %%b has %%c characters```, your knowledge of batch files requires some learning time too. Why waste time and effort learning to script using an old and outdated method, which was never designed for the task you require of it, when you can use that time to learn a better language, designed for those tasks. – Compo Aug 25 '22 at 18:13
  • I totally understand, but I was pressed for time with 5 days to complete this script. So I have to surf the net and try to understand as much as I could – SForum Aug 25 '22 at 18:17
  • Well that line is a complete failure regardless of your wanted solution, it is most certainly missing a opening parenthesis too, since your code has unbalanced parentheses. I indented it better for you to see that, when I edited your question again to format it for the second time for lack of formatting. So your question, as I understand it now, is you want any record containing more than twelve fields to have twelve, and to do that you want fields ten and eleven merged into one field with presumably a space separator. Is that correct? – Compo Aug 25 '22 at 18:31
  • Please also stop with the [edit] button, every time you do so, somebody else has to fix it. – Compo Aug 25 '22 at 18:37
  • Have you `confirmed whether it is possible that one or more fields can be empty` as requested? – Magoo Aug 25 '22 at 18:37
  • I would also say that any software which is creating/exporting to CSV with comma separated fields, should doublequote any fields which themselves contain a comma. If only those fields were doublequoted the task would be significantly simpler using a batch file, _(which could, at the same time as removing the unwanted commas in the strings, then remove those surrounding doublequotes)_. – Compo Aug 25 '22 at 18:41
  • Please [read about delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028). I'll not dupe-hammer your question, as there are other issues too. – Stephan Aug 25 '22 at 18:51
  • All fields are required – SForum Aug 25 '22 at 19:02
  • Unfortunately this file is also used by other process and cannot be altered. The only thing I can think of is copy it to new file then use script to double quote everything and then remove the comma in that file – SForum Aug 25 '22 at 19:04
  • We aren't intending doublequoting any file content. The software you are using to create the CSV files, if it is not doublequoting any field which includes the specified field separator is breaking your files. That is your issue, not ours. – Compo Aug 25 '22 at 19:18

3 Answers3

0
@ECHO OFF
SETLOCAL
rem The following settings for the directories and filenames are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q73491865.txt"
SET "destdir=u:\your results"
SET "outfile=%destdir%\outfile.txt"

(
FOR /f "usebackqtokens=1-12*delims=," %%g IN ("%filename1%") DO (
IF "%%s"=="" (
 ECHO %%g,%%h,%%i,%%j,%%k,%%l,%%m,%%n,%%o,%%p,%%q,%%r
 ) ELSE (
 ECHO %%g,%%h,%%i,%%j,%%k,%%l,%%m,%%n,%%o,%%p %%q,%%r,%%s
 )
)
)>"%outfile%"

GOTO :EOF

Always verify against a test directory before applying to real data.

I've assumed that no empty fields exist.

There are 12 fields normally, 13 if the extra comma exists. Your column-count and comma-count are incorrect.

Assign each field to %%g..%%s. If %%s is empty, then there are 12 fields, so regurgitate %%g..%%r.

If %%s is not empty, remove the comma between %%p and %%q and append %%s.

=== Revision ==== following extra specification

@ECHO Off
SETLOCAL
rem The following settings for the directories and filenames are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q73491865.txt"
SET "destdir=u:\your results"
SET "outfile=%destdir%\outfile.txt"

:again
SET "modified="
(
FOR /f "usebackqtokens=1-12*delims=," %%g IN ("%filename1%") DO (
IF "%%s"=="" (
 ECHO %%g,%%h,%%i,%%j,%%k,%%l,%%m,%%n,%%o,%%p,%%q,%%r
 ) ELSE (
 SET "modified=y"
 ECHO %%g,%%h,%%i,%%j,%%k,%%l,%%m,%%n,%%o,%%p %%q,%%r,%%s
 )
)
)>"%outfile%"
IF DEFINED modified (
 SET "filename1=%outfile%"
 IF "%outfile:~-1%"=="1" (SET "outfile=%outfile:~0,-1%") ELSE SET "outfile=%outfile%1"
 GOTO again
)
IF "%outfile:~-1%"=="1" SET "outfile=%outfile:~0,-1%"&GOTO again
DEL "%outfile%1" 2>NUL

TYPE "%outfile%"

GOTO :EOF

The revision is somewhat inelegant, repeatedly reprocessing the result file until no more substitutions are made.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • thanks for your reply... the only issue is there could be multiple extra commas,not just one – SForum Aug 25 '22 at 19:27
  • ***"The extra comma will only happen in the field after the 9th comma"*** is what you said. – Magoo Aug 25 '22 at 19:28
  • Yes, but it can have multiple extra commas – SForum Aug 25 '22 at 19:37
  • so this is the logic I am looking for... say each line should have 12 commas, I want to use a IF statement. If the line contains 15 commas, that means there are 3 extra ones in that field, so I will remove 3 commas after the 9th comma – SForum Aug 25 '22 at 19:42
0

Here's something which may 'remove' the commas you require, (it will not however 'look like the row above' as you stated, because that would require replacing commas with space characters):

I will not support any changes to your requirements or any script modification beyond changing S:\omewhere\AAA708270467-11-08-2022_12-15-36-819-A-2376058.csv on line 4

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

Set "SourceCSV=S:\omewhere\AAA708270467-11-08-2022_12-15-36-819-A-2376058.csv"

If Not Exist "%SourceCSV%" Exit /B 
For /F UseBackQ^ Delims^=^ EOL^= %%G In ("%SourceCSV%") Do Call :Sub "%%G"
Pause
Exit /B

:Sub
Set "r=%~1"
For /F "Delims==" %%G In ('"(Set f[) 2>NUL"') Do Set "%%G="
Set "i=1"
SetLocal EnableDelayedExpansion
Set "f[!i!]=%r:,=" & Set /A i += 1 & Set "f[!i!]=%"
If !i! Lss 13 (Echo(%~1& GoTo :EOF) Else Set /A c=i-2
For /L %%G In (1,1,!i!) Do If %%G Equ 1 (Set /P "=!f[%%G]!" 0<NUL
    ) Else If %%G GTR !c! (Set /P "=,!f[%%G]!" 0<NUL) Else If %%G Lss 11 (
        Set /P "=,!f[%%G]!" 0<NUL) Else Set /P "=!f[%%G]!" 0<NUL
Echo(
GoTo :EOF

Note: This code should allow for empty fields, and having multiple commas next to each other in the problem field, (field 10).


As a courtesy, the following example does exactly the same as the above code, but does not remove the commas, as your question specifically asks, but instead replaces them with spaces.

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

Set "SourceCSV=S:\omewhere\AAA708270467-11-08-2022_12-15-36-819-A-2376058.csv"

If Not Exist "%SourceCSV%" Exit /B 
For /F UseBackQ^ Delims^=^ EOL^= %%G In ("%SourceCSV%") Do Call :Sub "%%G"
Pause
Exit /B

:Sub
Set "r=%~1"
For /F "Delims==" %%G In ('"(Set f[) 2>NUL"') Do Set "%%G="
Set "i=1"
SetLocal EnableDelayedExpansion
Set "f[!i!]=%r:,=" & Set /A i += 1 & Set "f[!i!]=%"
If !i! Lss 13 (Echo(%~1& GoTo :EOF) Else Set /A c=i-2
For /L %%G In (1,1,!i!) Do If %%G Equ 1 (Set /P "=!f[%%G]!" 0<NUL
    ) Else If %%G GTR !c! (Set /P "=,!f[%%G]!" 0<NUL) Else If %%G Lss 11 (
        Set /P "=,!f[%%G]!" 0<NUL) Else (Set "Prompt=$S!f[%%G]:$=$$!"
            %SystemRoot%\System32\cmd.exe /D /K 0<NUL)
Echo(
GoTo :EOF

S:\omewhere\AAA708270467-11-08-2022_12-15-36-819-A-2376058.csv

123,235252,6376,test1,08/11/2022,2,0,1,EA,Required text, pencil ,pen
456,235252,6376,test2,08/11/2022,2,0,1,EA,Required,text, pencil ,pen
789,235252,6376,test3,08/11/2022,2,0,1,EA,Re,qu,ir,ed,te,xt, pencil ,pen
012,235252,6376,test4,08/11/2022,2,0,1,,Required,text, pencil ,pen
789,235252,6376,test5,08/11/2022,2,0,1,,Re,qu,,,te,xt, pencil ,pen
396,32124191,6376,CD1,08/11/2022,1,0,1,EA,Required Books,08/22/2022,12/10/2022,$60 basic supplies,37246613bA0,11800118,Required Books
396,32124191,6376,CD2,08/11/2022,2,0,1,EA,Required Supplies,08/22/2022,12/10/2022,up to $60.00 basic supplies with comma,37246613bA1,11800118,Required Supplies
396,32124191,6376,CD3,08/11/2022,2,0,1,EA,Required Supplies,08/22/2022,12/10/2022,up to $60.00 basic supplies with comma,37246613bA2,11800118,Required Supplies

intended output second example:

123,235252,6376,test1,08/11/2022,2,0,1,EA,Required text, pencil ,pen
456,235252,6376,test2,08/11/2022,2,0,1,EA,Required text, pencil ,pen
789,235252,6376,test3,08/11/2022,2,0,1,EA,Re qu ir ed te xt, pencil ,pen
012,235252,6376,test4,08/11/2022,2,0,1,,Required text, pencil ,pen
789,235252,6376,test5,08/11/2022,2,0,1,,Re qu   te xt, pencil ,pen
396,32124191,6376,CD1,08/11/2022,1,0,1,EA,Required Books 08/22/2022 12/10/2022 $60 basic supplies 37246613bA0,11800118,Required Books
396,32124191,6376,CD2,08/11/2022,2,0,1,EA,Required Supplies 08/22/2022 12/10/2022 up to $60.00 basic supplies with comma 37246613bA1,11800118,Required Supplies
396,32124191,6376,CD3,08/11/2022,2,0,1,EA,Required Supplies 08/22/2022 12/10/2022 up to $60.00 basic supplies with comma 37246613bA2,11800118,Required Supplies

Actual output image second example:

Compo
  • 36,585
  • 5
  • 27
  • 39
  • Compo, thanks very much for you help. I will consider you a life saver as I am really pressing for time and it will not be feasible for me to learn all these syntax in couple days. I am sending you a screen shot of the cmd screen – SForum Aug 26 '22 at 14:59
  • 396,32124191,6376,CD1,08/11/2022,1,0,1,EA,Required Books08/22/202212/10/2022$60 basic supplies37246613bA0,11800118,Required Books 396,32124191,6376,CD2,08/11/2022,2,0,1,EA,Required Supplies08/22/202212/10/2022up to $60.00 basic supplies with comma37246613bA1,11800118,Required Supplies 396,32124191,6376,CD3,08/11/2022,2,0,1,EA,Required Supplies08/22/202212/10/2022up to $60.00 basic supplies with comma37246613bA2,11800118,Required Supplies – SForum Aug 26 '22 at 15:01
  • I will try to put some effort and see if I can fix it. I have to understand your code first. Basically as you can see, the output from your code - 'Required Supplies08/22/202212/10/2022up to $60.00 basic supplies with comma37246613' should be 'Required Supplies,08/22/2022,12/10/2022,up to $60.00 basic supplies with comma,37246613,' – SForum Aug 26 '22 at 15:08
  • Two questions - 1. does your code handle multiple files already? 2. the output I show you was from the cmd prompt, when I opened the file, it was still showing the old way with the extra commas. Any idea to fix it ? Again, thank you so much. I will at least have something to base on. If I hit a hurdle, which I think I will, please help me again. – SForum Aug 26 '22 at 15:09
  • @SForum, I'm assuming from whatever you've tried to post in the first and second of the above comments, that **your question was wrong**. It specifically asked to remove commas, when in fact what you wanted to do was to replace commas with spaces. I have edited my answer to include an additional version for such a scenario. As for your third comment, no it does not, clearly there is a space at the top for defining one input file. I'm not going to add additional code to the above, so you should use the search facility at the top of the page to learn how to loop through/action on multiple files – Compo Aug 26 '22 at 15:22
  • This is the output I got from cmd prompt – SForum Aug 26 '22 at 15:44
  • 396,32124191,6376,CD1,08/11/2022,1,0,1,EA,Required Books 08/22/2022 12/10/2022 $60 basic supplies 37246613 bA0,11800118,Required Books 396,32124191,6376,CD2,08/11/2022,2,0,1,EA,Required Supplies 08/22/2022 12/10/2022 up to $60.00 basic supplies with comma 37246613 bA1,11800118,Required Supplies – SForum Aug 26 '22 at 15:45
  • What I am expecting is 396,32124191,6376,CD2,08/11/2022,2,0,1,EA,Required Supplies, 08/22/2022, 12/10/2022, up to $60.00 basic supplies with comma, 37246613, bA1,11800118,Required Supplies . It should not remove any commas from the other fields e.g. after Required Supplies (the first one), the dates and the comma before 37246613. The extra commas should only be removed after the 12th comma to the extent only the extra comma within this description field... I apologize for not being very clear on this part – SForum Aug 26 '22 at 15:52
  • My code does exactly what your question asked, it removed all commas after the ninth, but not the last two. The second version does the same thing, but instead of removing them, it replaces them with spaces. I have added the example file to my answer, the intended output, and the actual output. I am not changing it, if your asked question was wrong. You have not removed the commas in your provided comment above according to your question parameters which were eventually clear enough for two of us to answer. Please see my examples in the answer which match what you asked for, and what I coded! – Compo Aug 26 '22 at 15:58
  • I know what the problem is. It was my question at the beginning. It should be after the 12th comma. I make it the 9th because I was thinking I am making it easier for the one who help me, thus the misunderstanding... sorry about that – SForum Aug 26 '22 at 16:15
  • I made some mods and got it come to the point where the commas are all where they supposed to be except for the ones inside that description field, please see my code below... – SForum Aug 26 '22 at 16:51
  • :Sub Set "r=%~1" For /F "Delims==" %%G In ('"(Set f[) 2>NUL"') Do Set "%%G=" Set "i=1" SetLocal EnableDelayedExpansion Set "f[!i!]=%r:,=" & Set /A i += 1 & Set "f[!i!]=%" If !i! Lss 13 (Echo(%~1& GoTo :EOF) Else Set /A c=i-4 For /L %%G In (1,1,!i!) Do If %%G Equ 1 (Set /P "=!f[%%G]!" 0 – SForum Aug 26 '22 at 16:51
  • after that, it's beyond me because I don't understand the logic inside all these syntax – SForum Aug 26 '22 at 16:51
  • I said I would not support any modifications, or subsequent changes to your question. You were given ample opportunity, and were told to, post the source file, explain exactly what it looked like, and your task. All you needed to submit was a representative example file, and a view of the intended output, as I have above. You posted a two line example, I commented what I thought you wanted, and you didn't tell me that was wrong. As you can see above my code matches what I commented, and outputs the result you specified for those two lines. I cannot be responsible for your failures in [ask]. – Compo Aug 26 '22 at 17:27
  • Compo, please accept my apology for not adhering to the rules. This is my 1st time posting a question. I am not really familiar with the format. However, I will still vote u as a good mentor. It’s definitely my bad. I will continue to work on what you have coded and hopefully I can figure it out. Thanks again – SForum Aug 27 '22 at 00:03
0

Your question is very confusing. You had not clearly explained the details. More important: you have not posted in the question an example of the input data and the desired output; this would remedy the lack of details. So we can only guess what you want...

I think your problem could be better explained if you pay attention to the columns that both input and output data have. Are you interested in the commas, or in the columns?

This is my (attempt of a) solution. I used the example input file posted by Compo.

@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 12 columns or more
   rem Keep columns 1-9 the same. After that, generate 3 columns more:
   rem the last and one-before-last columns are the same
   rem the two-before-last column contain the rest of columns separated by space

   (for /F "usebackq tokens=1-9* delims=," %%a in ("%%F") do (

      set "restAfter9=%%j"
      set "last="
      set "lastBut1="
      set "lastBut2="
      for %%A in ("!restAfter9:,=" "!") do (
         set "lastBut2=!lastBut2! !lastBut1!"
         set "lastBut1=!last!"
         set "last=%%~A"
      )
      echo %%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,%%i,!lastBut2:~3!,!lastBut1!,!last!

   )) > "%%~NF.out"

ECHO Output: "%%~NF.out"
TYPE "%%~NF.out"

)

Output example:

Input: "test1.csv"
123,235252,6376,test1,08/11/2022,2,0,1,EA,Required text, pencil ,pen
456,235252,6376,test2,08/11/2022,2,0,1,EA,Required,text, pencil ,pen
789,235252,6376,test3,08/11/2022,2,0,1,EA,Re,qu,ir,ed,te,xt, pencil ,pen
012,235252,6376,test4,08/11/2022,2,0,1,,Required,text, pencil ,pen
789,235252,6376,test5,08/11/2022,2,0,1,,Re,qu,,,te,xt, pencil ,pen
Output: "test1.out"
123,235252,6376,test1,08/11/2022,2,0,1,EA,Required text, pencil ,pen
456,235252,6376,test2,08/11/2022,2,0,1,EA,Required text, pencil ,pen
789,235252,6376,test3,08/11/2022,2,0,1,EA,Re qu ir ed te xt, pencil ,pen
012,235252,6376,test4,08/11/2022,2,0,1,Required,text, pencil ,pen
789,235252,6376,test5,08/11/2022,2,0,1,Re,qu   te xt, pencil ,pen

Input: "test2.csv"
396,32124191,6376,CD1,08/11/2022,1,0,1,EA,Required Books,08/22/2022,12/10/2022,$60 basic supplies,37246613bA0,11800118,Required Books
396,32124191,6376,CD2,08/11/2022,2,0,1,EA,Required Supplies,08/22/2022,12/10/2022,up to $60.00 basic supplies with comma,37246613bA1,11800118,Required Supplies
396,32124191,6376,CD3,08/11/2022,2,0,1,EA,Required Supplies,08/22/2022,12/10/2022,up to $60.00 basic supplies with comma,37246613bA2,11800118,Required Supplies
Output: "test2.out"
396,32124191,6376,CD1,08/11/2022,1,0,1,EA,Required Books 08/22/2022 12/10/2022 $60 basic supplies 37246613bA0,11800118,Required Books 
396,32124191,6376,CD2,08/11/2022,2,0,1,EA,Required Supplies 08/22/2022 12/10/2022 up to $60.00 basic supplies with comma 37246613bA1,11800118,Required Supplies
396,32124191,6376,CD3,08/11/2022,2,0,1,EA,Required Supplies 08/22/2022 12/10/2022 up to $60.00 basic supplies with comma 37246613bA2,11800118,Required Supplies

EDIT: New simpler solution added

@echo off
setlocal EnableDelayedExpansion

rem General method to keep the first N columns the same
rem and group additional fields in column N+1

rem Define the number of "same" and "total" columns:
set /A "same=12, last=17"

rem Process all files with .csv extension in current folder
for %%F in (*.csv) do (

ECHO/
ECHO Input: "%%F"
TYPE "%%F"

   rem Process all lines of current file
   (for /F "usebackq delims=" %%a in ("%%F") do (

      set "line=%%a"
      set "head="
      set "tail="
      set "i=0"

      rem Split current line in comma-separated fields
      for %%b in ("!line:,=" "!") do (
         set /A i+=1
         if !i! leq %same% (         rem Accumulate field in "head" columns
            set "head=!head!%%~b,"
         ) else if !i! leq %last% (  rem Accumulate field in "tail" columns
            set "tail=!tail!%%~b,"
         ) else (  rem Combine one field from beginning of "tail" and accumulate last field
            for /F "tokens=1* delims=," %%x in ("!tail!") do set "tail=%%x %%y%%~b,"
         )
      )

      echo !head!!tail:~0,-1!

   )) > "%%~NF.out"

ECHO Output: "%%~NF.out"
TYPE "%%~NF.out"

)

Output example:

Input: "test1.csv"
field 1, field 2, field 3, field 4, field 5, field 6, field 7, field 8, field 9, field 10, field 11, field 12, field 13, field 14, field 15, field 16, field 17
field 1, field 2, field 3, field 4, field 5, field 6, field 7, field 8, field 9, field 10, field 11, field 12, field 13, field 13a, field 13b, field 14, field 15, field 16, field 17 
field 1, field 2, field 3, field 4, field 5, field 6, field 7, field 8, field 9, field 10, field 11, field 12, field 13, field 13a, field 13b, field 13c, field 14, field 15, field 16, field 17
Output: "test1.out"
field 1, field 2, field 3, field 4, field 5, field 6, field 7, field 8, field 9, field 10, field 11, field 12, field 13, field 14, field 15, field 16, field 17
field 1, field 2, field 3, field 4, field 5, field 6, field 7, field 8, field 9, field 10, field 11, field 12, field 13  field 13a  field 13b, field 14, field 15, field 16, field 17 
field 1, field 2, field 3, field 4, field 5, field 6, field 7, field 8, field 9, field 10, field 11, field 12, field 13  field 13a  field 13b  field 13c, field 14, field 15, field 16, field 17
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Aacini, thanks very much for your reply. I know my last post was not clear at all. I am using your solution and replace the data with 16 commas and 17 fields. I will try to do a better job this time. Please see my input and output – SForum Aug 29 '22 at 21:05
  • Input file field 1, field 2, field 3, field 4, field 5, field 6, field 7, field 8, field 9, field 10, field 11, field 12, field 13, field 14, field 15, field 16, field 17 field 1, field 2, field 3, field 4, field 5, field 6, field 7, field 8, field 9, field 10, field 11, field 12, field 13, field 13a, field 13b, field 14, field 15, field 16, field 17 field 1, field 2, field 3, field 4, field 5, field 6, field 7, field 8, field 9, field 10, field 11, field 12, field 13, field 13a, field 13b, field 13c, field 14, field 15, field 16, field 17 – SForum Aug 29 '22 at 22:38
  • output file field 1, field 2, field 3, field 4, field 5, field 6, field 7, field 8, field 9, field 11, field 12, field 13, field 14, field 15, field 16, field 17,,,,,%n field 1, field 2, field 3, field 4, field 5, field 6, field 7, field 8, field 9, field 11, field 12, field 13, field 13a, field 13b, field 14, field 15, field 16, field 17,,,,,%n field 1, field 2, field 3, field 4, field 5, field 6, field 7, field 8, field 9, field 11, field 12, field 13, field 13a, field 13b, field 13c, field 14, field 15, field 16, field 17,,,,,%n – SForum Aug 29 '22 at 22:42
  • @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 – SForum Aug 29 '22 at 22:54
  • (for /F "usebackq tokens=1-12* delims=," %%a in ("%%F") do ( set "restAfter12=%%n" set "last=" set "lastBut1=" set "lastBut2=" set "lastBut3=" set "lastBut4=" for %%A in ("!restAfter12:,=" "!") do ( set "lastBut4=!lastBut4! !lastBut3! !lastBut2! !lastBut1!" set "lastBut3=!lastBut2!" set "lastBut2=!lastBut1!" set "lastBut1=!last!" set "last=%%~A" ) echo %%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,%%i,%%k,%%l,%%m,!lastBut4:~5!,!lastBut3!,!lastBut2!,!lastBut1!,!last! – SForum Aug 29 '22 at 22:55
  • )) > "%%~NF.out" ECHO Output: "%%~NF.out" TYPE "%%~NF.out" ) – SForum Aug 29 '22 at 22:55
  • Aacini, I have posted my input , my out and I have modified your code to work with data, but it is not working as expected. The output that I showed you was the output from my code , which is wrong. – SForum Aug 29 '22 at 22:57
  • You must [edit your question](https://stackoverflow.com/posts/73491865/edit) and post there the correct description of your problem and both the input file and desired output file. I will _not_ try to copy any data _from comments_... **`:(`** – Aacini Aug 30 '22 at 00:44
  • After doing that, post a new comment here as an advice for me... Ah! And upvote my answer if you think it is useful! – Aacini Aug 30 '22 at 00:52
  • Aacini - I got it. I modified your code so that it will work for 16 commas and 17 fields. Thank you so much for your help. This is my first time posted. How can I give you credit for your great advice? Please let me know – SForum Aug 30 '22 at 03:41
  • I clicked on the check mark. Is this what I am supposed to do? – SForum Aug 30 '22 at 03:43
  • About my reward: yes, thanks! **`:)`** You may also _upvote_ my answer, that is, click on triangle up above the green check mark. However, you _should_ also [edit the question](https://stackoverflow.com/posts/73491865/edit) and complete the requested changes: modify the problem description (use "preserve columns" instead of "delete commas") and include the example input and output files. This is in the benefit of future readers that may review your question because it's not nice to have to read _the comments_ in several answers in order to understand the problem... – Aacini Aug 30 '22 at 09:26
  • You should never post data or code in comments because doing that will force the reader to edit and format the copied data. Also, you should always post data or code in the question enclosed in appropriate **`{}`** code tags. If you want to know the reason for this, then here it is the modified code that correctly works on 12 fixed columns and 17 total columns (try to copy it to a working Batch file): – Aacini Aug 30 '22 at 09:26
  • @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 – Aacini Aug 30 '22 at 09:26
  • (for /F "usebackq tokens=1-12* delims=," %%a in ("%%F") do ( set "restAfter12=%%m" set "last=" for /L %%i in (1,1,4) do set "lastBut%%i=" 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" ) – Aacini Aug 30 '22 at 09:27
  • Anyway, I written a new simpler solution to your problem that works on any number of fixed/total columns by changing just 2 numbers in the code. In this way, to solve your first problem use `set /A "same=9, last=12"` and for the last problem just change it to `set /A "same=12, last=17"`. The new solution is in my answer... – Aacini Aug 30 '22 at 09:27
  • Hi, Aacini- I have a new issue now. I tried with both of your solutions , but still doesn't work. The issue is in column 13, when there is no data for that column, it will show as ,,. In this case, the column is shifted 1 over. – SForum Aug 30 '22 at 14:51
  • In your question, and in all your numerous comments, you never indicated that could be _less columns_. You always specified _"remove **extra** commas"_. If your _real data_ may have less fields, why you didn't posted an example of it? And more important: what is the result you want when there are less columns? Are you expecting us to solve a problem we've never heard of before? Sorry, but we are not mind readers... **`:(`** – Aacini Aug 30 '22 at 18:41
  • When I was doing testing, the test data work fine. When I start using data from production, the users told me the field can be blank but the column remains, which means it's still 17 columns and 16 commas. I used both your solutions and they are not working. On top of that, some rows that have data in that field also shifted, but it is random. Some worked, some don't. BTW, how do I add example or input so you can see it. – SForum Aug 31 '22 at 02:43
  • Aacini, I am trying to post the input and output sample but it said the edit space is full. What can I do? – SForum Aug 31 '22 at 13:20
  • Just post a _small section_ of the input file (10-12 lines), a section that include the problematic records... And don't forget to post what is _the output_ you want from such a problematic records! – Aacini Aug 31 '22 at 14:06
  • Hi, Aacini, I have posted another question so I can state the problem with input and out with code. Please search title 'Remove extra commas in a field from csv file affecting other rows with good data' – SForum Aug 31 '22 at 15:25
  • thanks again for trying to follow up with my issue – SForum Aug 31 '22 at 15:27