Solved Amending a CSV file via a batch file

April 28, 2014 at 07:53:09
Specs: Windows 7
I need to amend a csv file that could contain 1 line or 100 lines.

The file will be sent thus:

123456789,01/08/14,12,08:00,28/04/14

Each line has to be changed to
P,123456789,01/08/14,12,08.00,28/04/14

That is add to first column P change the : to . (semicolon to full stop.
The first number (123456789) could be any length from 1 digit to 9 digits.

Hope someone can help Please (pretty please)


See More: Amending a CSV file via a batch file

Report •

#1
April 28, 2014 at 08:28:26
✔ Best Answer
@echo off & setlocal EnableDelayedExpansion
type nul > "file_new.csv"
for /F "delims=" %%j in ('type "myfile.csv"') do (
  set row=%%j
  set row=!row::=.!
  echo.P,!row!
) >> "file_new.csv"

message edited by IVO


Report •

#2
April 28, 2014 at 10:24:29
perfect, many thanks.

Worked first time


Report •

#3
April 28, 2014 at 22:17:49
Hi, whilst that worked. I also need for another csv a similar change, but this time without adding P to the first column of each row, just changing the : to .

I have tried to modify the batch but have not been successful

What have I done wrong?


Report •

Related Solutions

#4
April 29, 2014 at 01:04:04
What you did wrong I don't know since you did not post your change. Anyway to achieve what you need it is sufficient to replace

echo.P,!row! with echo.!row!

or use the following more tense script

@echo off
type nul > "FileOut.csv"
for /F "tokens=1,2 delims=:" %%j in ('type "FileIn.csv"') do (echo.%%j.%%k) >> "FileOut.csv"


Report •

#5
April 29, 2014 at 01:16:45
Many thanks I did remove the "echo.P,!row! with echo.!row!". But the files was created with no entries in it.

I really appreciate your assistance. Got me out of a big jam.


Report •

#6
April 29, 2014 at 02:29:28
Hmm... your issue remains a mistery to me as you apparently did what I suggest. I hope you had the job done. If more support is needed, contact me freely by a private message (click on my avatar then click again on my nickname).

Report •

Ask Question