computing
  • 7

CSV File Editing Through BATCH File

  • 7

hi!!!!!!!!!!! guys

i have a csv file of following format!!!

ALPHA TANGO CHARLEE GRAVO
aa bb cc dd
aa dd ee ff
aa gg jj jj
aa ff tt kk

i want to change the format in followng manner

ALPHA TANGO CHARLEE GRAVO
aa bb cc dd
dd ee ff
gg jj jj
ff tt kk

i want to remove aa from next all fields and those fields should remain blank.

any suggesstions to make these changes through batch script

thanks a lot!!!!!!!!!!!!

Share

1 Answer

  1. Try this:

    @echo off>output.csv
    cls
    setlocal enabledelayedexpansion
    
    set counter=1
    
    for /f "tokens=*" %%1 in (input.csv) do (
        set inline=%%1
    
        if !counter! gtr 2 (set inline=!inline:~3!)
    
        >>output.csv echo !inline!
        set /a counter+=1
    )
    
    

    If that won’t work because the variable in column 1 is not fixed at three bytes as you show, try the following:

    @echo off>output.csv
    cls
    setlocal enabledelayedexpansion
    
    set counter=1
    
    for /f "tokens=1* delims=," %%1 in (input.csv) do (
        
        if !counter! gtr 2 (
           >>output.csv echo ,%%2
           ) else (
           >>output.csv echo %%1,%%2
           )
    
        set /a counter+=1
    )
    
    

    • 0