Solved Adding Header & trailer for existing csv file through batch

December 23, 2013 at 02:11:18
Specs: Windows 7
I need to add Header as current UTC time format as "MM-DD-YYYYHH:MM:SS" & trailer as number record found on existing csv through Batch scripting. I need help for how to script this.

message edited by HI


See More: Adding Header & trailer for existing csv file through batch

Report •


✔ Best Answer
December 24, 2013 at 22:37:47
Whert's post gave me much to learn!
Meantime, here's a crappy hack (happy crack?) to fill the voids:

@echo off
set ap=P
set d=%date:~4%
set d=%d:/=-%
:: note leading space in foll line at %time%, to dodge octal issues
set hr= %time:~0,2%
set hr=%hr: 0=%
set t2=%time:~2,6%
set /a ampm=hr/12
if %ampm% equ 0 set ap=A
set /a hr=hr"%%"12
set /a hr=(12-hr)/12*12+hr
set hr=0%hr%
>> new.csv echo A, ABC, %d%%hr:~-2%%t2% %ap%M
copy new.csv + xx.csv
for /f %%a in ('find /c /v "" ^<xx.csv') do set lc=%%a
>> new.csv echo t,%lc%
::========== end batch
edit:
TOTALLY missed the UTC requirement. My bad completely. Sorry!

message edited by nbrane



#1
December 23, 2013 at 02:56:22
For a start you can post the output of these:

echo %DATE%
echo %TIME%

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


Report •

#2
December 23, 2013 at 03:51:37
I need output like this

A, ABC, MM-DD-YYYYHH:MM:SS AM/PM
Data item.....................................................
.................................................................
.................................................................
.............................................................
..............................................................
T, 5


Report •

#3
December 23, 2013 at 20:40:05
Hi HI (Ha ha!). M2's query was never answered, so the date-time INPUT (%date%, %time%) need to be established before your well-defined output can be achieved. As for the line-count:
@for /f %%a in ('find /c /v "" ^<xx.csv') do set lc=%%a
@echo linecount: %lc%

Manipulation of date-time tokens is dependant on local environment settings, which are manifested (in batch) in the content of environment vars. %date% and %time%. Hence M2's request for output of %date% and %time% from your system (or target system) to finitsh answering your post.


Report •

Related Solutions

#4
December 23, 2013 at 21:23:41
There is a handy but kind of annoying way to get settings independent time and date. The time will probably be off buy processing time though:

http://www.dostips.com/forum/viewto...

The other problem will be getting UTC, since the script could be run with any time zone.

message edited by whert


Report •

#5
December 24, 2013 at 01:50:21
Output
>echo %date%
Tue 12/24/2013

>echo %time%
15:18:36.74


Report •

#6
December 24, 2013 at 02:05:58
::======  script starts here  ===============
:: gets date time into header & record count into footer
:: hi.bat  2013-12-24 16:33:40.98
@echo off & setLocal enableDELAYedeXpansioN

call :sub1

:main
set/a N=0
for /f "tokens=* delims= " %%a in (my.csv) do (
set/a N+=1
)
>> new.csv echo.!N! records
goto :eof

:sub1
@echo off > d.d

>> d.d echo E 0100 B4 2A CD 21 B4 4C CD 21
>> d.d echo N DAY.COM
>> d.d echo RCX
>> d.d echo 8
>> d.d echo W
>> d.d echo E 0100 B4 2A CD 21 88 F0 B4 4C CD 21
>> d.d echo N MONTH.COM
>> d.d echo RCX
>> d.d echo A
>> d.d echo W
>> d.d echo E 0100 B4 2A CD 21 89 C8 B4 4C CD 21
>> d.d echo N YEAR.COM
>> d.d echo RCX
>> d.d echo A
>> d.d echo W
>> d.d echo E 0100 B4 2A CD 21 88 D0 B4 4C CD 21
>> d.d echo N DAT.COM
>> d.d echo RCX
>> d.d echo A
>> d.d echo W
>> d.d echo Q

debug < d.d > nul
del d.d

dat
for %%a in (1 2 3 4 5 6 7 8 9                                                                  ) do if errorlevel %%a set DD=0%%a
for %%a in (                  10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31) do if errorlevel %%a set DD=%%a
set D=%errorlevel%

month
for %%a in (1 2 3 4 5 6 7 8 9         ) do if errorlevel %%a set MM=0%%a
for %%a in (                  10 11 12) do if errorlevel %%a set MM=%%a

year
if errorlevel 221 set YYYY=2013
if errorlevel 222 set YYYY=2014

set T=%TIME%
set T=!T:~0,-3!
>  new.csv echo.!YYYY!-!MM!-!DD! !T!
>> new.csv type my.csv

del day.com
del month.com
del year.com
del dat.com
goto :eof
::============================ DONE YMD20 ==========================
::======  script ends here  =================

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


Report •

#7
December 24, 2013 at 07:32:44
Thanks M2,
But it is not executing on WIN7 64 bit,

'debug' is not recognized as an internal or external command,
operable program or batch file.
'dat' is not recognized as an internal or external command,
operable program or batch file.
'month' is not recognized as an internal or external command,
operable program or batch file.
'year' is not recognized as an internal or external command,
operable program or batch file.


Report •

#8
December 24, 2013 at 08:14:22
http://www.golden-triangle.com/DBUG...


dunno if w7 will run it

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


Report •

#9
December 24, 2013 at 19:13:22
@HI

what other tools do you have on your system ? It seem you have Win7. Try use powershell if cannot use other programming language.
http://technet.microsoft.com/en-us/...

http://blogs.technet.com/b/heyscrip...

message edited by brianadams


Report •

#10
December 24, 2013 at 22:37:47
✔ Best Answer
Whert's post gave me much to learn!
Meantime, here's a crappy hack (happy crack?) to fill the voids:

@echo off
set ap=P
set d=%date:~4%
set d=%d:/=-%
:: note leading space in foll line at %time%, to dodge octal issues
set hr= %time:~0,2%
set hr=%hr: 0=%
set t2=%time:~2,6%
set /a ampm=hr/12
if %ampm% equ 0 set ap=A
set /a hr=hr"%%"12
set /a hr=(12-hr)/12*12+hr
set hr=0%hr%
>> new.csv echo A, ABC, %d%%hr:~-2%%t2% %ap%M
copy new.csv + xx.csv
for /f %%a in ('find /c /v "" ^<xx.csv') do set lc=%%a
>> new.csv echo t,%lc%
::========== end batch
edit:
TOTALLY missed the UTC requirement. My bad completely. Sorry!

message edited by nbrane


Report •

#11
December 24, 2013 at 22:42:05
Yeah so much easier to get the date in powershell:

powershell (Get-Date).ToUniversalTime().ToString('MM-dd-yyyyHH:MM:ss')

I tried doing it in batch, but it became a monstrosity. Between using tzutil to get a timezone offset, getting the date in a predictable way and doing the math to convert to UTC it becomes a horribly convoluted approach.

I'm not very good with powershell, but I think it wouldn't be too hard to get this whole thing into quite a small script...

Hopefully OP can use something other than batch...

Edit

Oh and Hi nbrane and M2! I was Judago in a past computing.net life.

message edited by whert


Report •

#12
December 24, 2013 at 23:48:38
Hi Judago,

I was M2 in a previous life. LOL

Just think, if #1 had been answered, we might be done.

N2 says Merry Christmas

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


Report •

Ask Question