-1

I have a csv file with 18 fields. I need to copy the file to a txt file, delete the first four lines, replace the data in field #8, and save the file with a new name.
The data in field #8 is an integer (for example, 1, 2, 3, etc). Each integer needs to be replaced with a separate value (for example, I need to replace 1 with 1005 and 3 with 1008). I am trying to modify/fix the following batch file:

@echo off
More +4 datatest.csv > datacopy.txt
( FOR /f "tokens=8 delims=," %%h in (datacopy.txt) do (
    if "%%h"=="3" (echo 1008) else ( 
      echo %%a %%b %%c`  echo %%a %%b %%c
    ) 
  )
)>paygoinvoice.txt
@echo on

1 Answers1

1
  • With only one token selected, you'll get only one column (%%h)
  • parsing the more command directly, there is no need for a temporary file.
  • depending on how many integers to replace, you may use a pseudo array with the int as an index/pointer.
  • you may either get all columns separately (tokens=1-18,%%A..%%R) or gather the rest * in one for variable.

@echo off & Setlocal EnableDelayedExpansion
( FOR /f "tokens=1-8* delims=," %%A in ('More +4 datatest.csv') do (
    Set "H=%%H"
    if "%%H"=="1" Set "H=1005"
    if "%%H"=="3" Set "H=1008"
    echo %%A,%%B,%%C,...,!H!,%%I
  )
)>paygoinvoice.txt
@echo on
  • Some of the data (not all) is surrounded by quotes. Is there a way to modify tne batch file to delete all the quotes? – Steve Richter Dec 24 '18 at 19:24
  • to remove (outer) double quotes, use the `~` tilde modifier like this `%%~A` etc. –  Dec 24 '18 at 19:27
  • If an answer solves your question or you find it helpful you should consider to [accept the answer](http://stackoverflow.com/help/accepted-answer) and/or [vote up](https://stackoverflow.com/help/why-vote) –  Dec 24 '18 at 19:35
  • 1
    The green checkmark is for "accept"? I tried to vote up but since I am a new user I don't think it recorded my vote. I appreciate your help! – Steve Richter Dec 24 '18 at 19:41
  • Yes, but you may have to wait some time for it to enable. –  Dec 24 '18 at 19:45
  • Thank you for your help. I need to further manipulate the file. If I cannot find what I need online or in other questions/answers, should I create a new request(s) or continue to edit this one? – Steve Richter Dec 24 '18 at 19:48
  • To get better audience and allow others to also gain reputation from good answers, YES - ask a new question. If the quesion is related to this one, include a reference. –  Dec 24 '18 at 19:51