Header and trailer for csv file in batch

June 19, 2009 at 08:01:20
Specs: Windows XP
Hi, I am new to batch script and need help. I have below a2.csv file:

HDR20090619a2
aaa,111,222,333,bbb
bbb,222,111,444,ccc
ccc,333,444,111,ddd
TRL000000003

In the batch script need to read the csv file.
In header - need to match if the current date is what's in the HDR row (20090619- today's date) and name of the file is what we are reading from (which is a2).
In trailer - Need to make sure total # of rows match what's in the file. 000000003 (9 char length) is the length of the total # of rows. So is there are 12 rows it would 000000012. If the trailer and header match then need to copy the file to say c:\temp\output.


See More: Header and trailer for csv file in batch

Report •


#1
June 19, 2009 at 12:21:38
Please, post your date format as it is country dependent.
Report what is displayed by typing at prompt

echo.%date%


Report •

#2
June 19, 2009 at 14:25:08
ok, that is just for date. how abt the trailer info and how do I read the data from the csv file. I am knew to batch script. Any help would be great. Thx

Report •

#3
June 19, 2009 at 16:56:11
If you don't want to answer requests for more info, you will soon test the patience of those best able to help.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions

#4
June 19, 2009 at 20:59:04
:: chk csv for today's date in header, chk num records in tail

@echo off & setLocal enableDELAYedexpansion

call :YMD8
  set today=!year!!month!!dat!
  echo today is !today!
  echo.

:main 
for /f "tokens=* delims= " %%a in ('dir/b *.csv') do (
  echo %%a

    :count lines; set T & R
    for /f "tokens=* delims= " %%d in ('find /v /c "" ^< %%a') do (
      set T=%%d
      set /a R=!T!-2
    )

  :chk contents of one csv & set head & tail
  set N=0
    for /f "tokens=* delims= " %%i in (%%a) do (
      set /a N+=1
      if !N! equ 1 set head=%%i
      if !N! equ !T! set tail=%%i
    )
  echo head !head!
  echo tail !tail!

  : chk date and record cout from head and tail
  if !head:~3^,8! equ !today! echo %%a current
  if !tail:~3! equ !R!        echo record count OK
  if !head:~3^,8! equ !today! (
  if !tail:~3! equ !R!        (
  echo copy %%a c:\temp\output
  ))
  echo.
  )
  :end chk contents of one csv & set head & tail
)
:end main

goto :eof

:YMD8

::== YMD8.bat

@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 dat=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 dat=%%a
::echo Date %dat%

day
if errorlevel 0 set day=Sunday
if errorlevel 1 set day=Monday
if errorlevel 2 set day=Tuesday
if errorlevel 3 set day=Wednesday
if errorlevel 4 set day=Thursday
if errorlevel 5 set day=Friday
if errorlevel 6 set day=Saturday
::echo Day %day%

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

year
if errorlevel 215 set Year=2007
if errorlevel 216 set Year=2008
if errorlevel 217 set Year=2009
if errorlevel 218 set Year=2010
::echo Year %Year%

del day.com
del month.com
del year.com
del dat.com
::== DONE

goto :eof


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#5
June 19, 2009 at 21:40:17
@OP , if you have gawk for windows (see my sig)

BEGIN{
	today = strftime("%Y%0m%0d",systime())
	m=split(ARGV[1],filename,".")
	FILE = filename[1]
	f=0
}
NR==1{
	date=substr($0,4,8)	
	file=substr($0,length-1)	
	if( (date == today) && (FILE == file) ){f=1	}
}
NR>1 && !/^TRL/{count++} #get row count
/^TRL/{
	o = $0
	gsub(/^TRL/,"",o) #get the number
	if ( o+0 == count && f){
		print "all ok"
		cmd = "copy "FILENAME" c:\\temp\\outpt"
		print cmd
		#system(cmd) #uncomment to use
	}
}

save as myscript.awk and on the command line

C:\test>gawk -f test.awk a2.csv
all ok
copy a2.csv c:\temp\outpt


make your life easier, learn to code in a good programming language.

GNU win32 packages | Gawk


Report •

#6
June 19, 2009 at 22:12:06
@M2, great batch code, but i doubt OP understands assembly or how to use debug enough to understand your code. Furthermore, its hard to maintain. A requirement change in the header date format would require OP to know how to debug the assembly code. I would still recommend to use tools that provide good date functions among others, eg vbscript ( if he can't download other good programming language :) )


Set objFS = CreateObject("Scripting.FileSystemObject")
Set objArg = WScript.Arguments
FILE= Split(objArg(0),".")
FILENAME=FILE(0)
strFile = objArg(0)
today = Now
yr = Year(today)
mth = Month(today)
dy = Day(today)
If Len(mth) <2 Then 
	mth="0"&mth
End If
If Len(dy) <2 Then 
	dy="0"&dy
End If
strDate = yr&mth&dy
Set objFile = objFS.OpenTextFile(strFile)
count=0
flag=0
Do Until objFile.AtEndOfStream
	strLine = objFile.ReadLine	
	If InStr(strLine,"HDR") > 0 Then
		dat8 = Mid(strLine,4,8)		
		filename = Right(strLine,2)
		If filename = FILENAME And dat8 = strDate Then
			WScript.Echo "header ok"
			flag=1
		Else
			WScript.Echo "header not ok"		
		End If 
	End If 	
	If InStr(strLine,"HDR") <=0 And InStr(strLine,"TRL") <=0 Then
		count=count+1		
	End If
	If InStr(strLine,"TRL") > 0 Then
		number = Mid(strLine,4)
		If number + 0 = count And flag = 1 Then
			WScript.Echo "trailer ook"
			WScript.Echo "copy "&strFile&" c:\tmp"
			objFS.CopyFile strFile,"c:\tmp\"&strFile 
		Else
			WScript.Echo "trailer not ok"
		End If 
	End If 	
Loop


GNU win32 packages | Gawk


Report •

#7
June 20, 2009 at 00:54:27
Hi ghostdog,

Points taken. I guess one day I should learn vbs.

While I've got you on the horn, can you do this calc in python?

The ^ means exponent, as in:

2^3

means '2 to the 3rd'

===========================
(2^43112609-1) + 64=?


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#8
June 20, 2009 at 01:12:29
hi m2

C:\test>python
ActivePython 2.5.4.4 (ActiveState Software Inc.) based on
Python 2.5.4 (r254:67916, Apr 27 2009, 15:41:14) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print 2**3
8

you can give it a try for that big a number and see how it goes, i believe there will be some limitation. if need be, please ask at comp.lang.python newsgroup. there are better experienced people there that does such kind of maths who can answer you.

GNU win32 packages | Gawk


Report •

#9
June 22, 2009 at 10:00:07
thank u so much, it looks like it is working.

Report •


Ask Question