Solved Need help with a batch file to edit a csv

January 13, 2015 at 09:28:40
Specs: Windows 7
I have a regularly created csv file that always has the same file name, but we have changed the data requirements it needs to have in it. It would be a major hassle to update the program that creates the CSV so I am hoping that on an as needed basis I can just run a batch file to make the changes.

In the CSV I have a two lines that need to be duplicated then modified slightly. Those lines are:
P,PREHEAT,PREHEAT,TEMP [±10°F],900
P,PREHEAT,PREHEAT,TIME [MINUTES],120

The number at the end may change but the rest will be same. I need to duplicate and make the second PREHEAT into APREHEAT. I also need to do this for the same thing for DRAW where it says P,DRAW,DRAW,... that needs to be P,DRAW,ADRAW

I have created a few batch files, but they are not my forte. Can someone help me out with this?


See More: Need help with a batch file to edit a csv

Report •


✔ Best Answer
January 20, 2015 at 19:55:55
This puts the modified line immediately after the original.

::====== script starts here ===============
::
:: heat2.bat 21 January 2015 10:55
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

:main
for /f "tokens=* delims= " %%a in (myfile) do (
echo.%%a
set S=%%a
set S=!S:PREHEAT,PREHEAT=PREHEAT,APREHEAT!
set S=!S:P,DRAW,DRAW=P,DRAW,ADRAW!
if "%%a" neq "!S!" echo.!S!
) >> NEWFILE
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle



#1
January 13, 2015 at 21:34:19
Post a chunk of your CSV; 10 lines should do it.

=====================
M2 Golden-Triangle


Report •

#2
January 14, 2015 at 21:08:42
Here's a vbscript version:
'----- begin vbscript REPLASE
set fso=createobject("scripting.filesystemobject")
'filename is takens as first argument, but it could be "hard-wired"
fnam=wscript.arguments(0)
if fso.filexists(fnam)=false then wscript.quit
f=fso.opentextfile(fnam,1).readall
f=replace(f,"P,PREHEAT,PREHEAT","P,PREHEAT,APREHEAT")
f=replace(f,"P,DRAW,DRAW","P,DRAW,ADRAW")
fso.opentextfile(fnam,2).write f
'-------- end vbscript
to use this script:
CSCRIPT REPLASE.VBS filename
or put the above line into a batchfile:
cscript replase.vbs %1


Report •

#3
January 15, 2015 at 04:50:41
If I got the gist of the requirement, which I often don't...
:: 19:48 2015-01-15
::====== script starts here ===============
::
:: heat.bat 2015-01-14 15:23:07.20
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

:main
for /f "tokens=* delims= " %%a in (myfile) do (
echo.%%a
set S=%%a
set S=!S:PREHEAT,PREHEAT=PREHEAT,APREHEAT!
if "%%a" neq "!S!" echo.!S!
) >> NEWFILE
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

Related Solutions

#4
January 20, 2015 at 09:46:45
Thanks for the replies. Sorry I have been away on a business trip and didn't get a chance to reply sooner. As requested here is a snippet of the csv:

P,LOADING,LOAD TALL,NOTE,,,,,
,,,,,,,,
P,PREHEAT,PREHEAT,TEMP [±10°F], ,,,,
P,PREHEAT,PREHEAT,TIME [MINUTES], ,,,,
P,AUSTENITIZING,FURNACE,NAT [CFH] [±25],74,,,,
P,AUSTENITIZING,FURNACE,TIME [MINUTES],338,,,,
P,QUENCHING,QUENCH,WATER [ML] TALL,200,,,,
,,,,,,,,
P,DRAW,DRAW,TEMP [±10°F],400,,,,
P,DRAW,DRAW,TIME [MINUTES],240,,,,


I am not sure I understand what you posted nbrane. I need to duplicate the line and change it, not just replace it. Will your code do that?

Mechanix2go, your code also looks like it replaces the values and saves as a new file is that right?

I need to copy the line, then replace the values in the new line, and save the file as a replacement.

I need the preheat and draw lines duplicated, then modified to append the letter a in front of the second sets so using the above I need it to end up like this:

P,LOADING,LOAD TALL,NOTE,,,,,
,,,,,,,,
P,PREHEAT,PREHEAT,TEMP [±10°F], ,,,,
P,PREHEAT,PREHEAT,TIME [MINUTES], ,,,,
P,PREHEAT,APREHEAT,TEMP [±10°F], ,,,,
P,PREHEAT,APREHEAT,TIME [MINUTES], ,,,,

P,AUSTENITIZING,FURNACE,NAT [CFH] [±25],74,,,,
P,AUSTENITIZING,FURNACE,TIME [MINUTES],338,,,,
P,QUENCHING,QUENCH,WATER [ML] TALL,200,,,,
,,,,,,,,
P,DRAW,DRAW,TEMP [±10°F],400,,,,
P,DRAW,DRAW,TIME [MINUTES],240,,,,
P,DRAW,ADRAW,TEMP [±10°F],400,,,,
P,DRAW,ADRAW,TIME [MINUTES],240,,,,

Even if they are just added to the bottom is fine.


Report •

#5
January 20, 2015 at 19:55:55
✔ Best Answer
This puts the modified line immediately after the original.

::====== script starts here ===============
::
:: heat2.bat 21 January 2015 10:55
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

:main
for /f "tokens=* delims= " %%a in (myfile) do (
echo.%%a
set S=%%a
set S=!S:PREHEAT,PREHEAT=PREHEAT,APREHEAT!
set S=!S:P,DRAW,DRAW=P,DRAW,ADRAW!
if "%%a" neq "!S!" echo.!S!
) >> NEWFILE
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •


Ask Question