Page 1 of 1 1
Topic Options
#174795 - 2007-03-20 10:50 PM reading file and comparing string
booey Offline
Getting the hang of it

Registered: 2005-07-25
Posts: 76
Loc: USA
I am trying to write a kix script to read the first line in a pipe delimited csv file and delete the file if it contains the string "||||||". Basically, we have automated the running of crsytal report and their exports. If the report is empty, the automated process still saves the blank file, but the delimiters stay in the csv file. These blank files cause problems with the automated import, so I am trying to find a way to search for these files and delete them by a script. I can't seem to get this script working. Here is what I have so far:
 Code:
DIM $site $dir $file

$dir = "C:\temp\"
$site = "site1","site2","site3"
For Each $element in $site
$file = $dir + $element + " " + "report" + "\*.csv"
	If Open(3, $file)  = 0
	$x = Readline(3)
	? @serror
		IF INSTR($x, "|||||")
		@serror
		DEL $file
		? "file is empty"
	? @serror
		ELSE
	? "Data is not empty"
	? @serror
	Close (3)
	Endif


Any help would be appreciated. Thanks.

Top
#174796 - 2007-03-20 11:34 PM Re: reading file and comparing string [Re: booey]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
What are the errors you are getting?

At first glance I would say that you need to close the file before trying to delete it, otherwise it is marked "in use".
_________________________
Today is the tomorrow you worried about yesterday.

Top
#174834 - 2007-03-21 06:20 PM Re: reading file and comparing string [Re: Gargoyle]
booey Offline
Getting the hang of it

Registered: 2005-07-25
Posts: 76
Loc: USA
Thanks for the reply. I just changed where I had the DEL command and it worked for me.

However, is it possible to just read the first line of the file instead of the whole thing and compare a string to that?

Thanks.

Top
#174837 - 2007-03-21 07:43 PM Re: reading file and comparing string [Re: booey]
eriqjaffe Offline
Hey THIS is FUN

Registered: 2004-06-24
Posts: 214
Loc: Arlington Heights, IL USA
You should be able to use the ReadLineEx() UDF for that.
Top
#174839 - 2007-03-21 09:00 PM Re: reading file and comparing string [Re: booey]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
 Originally Posted By: booey
However, is it possible to just read the first line of the file instead of the whole thing and compare a string to that?
That IS what your code is doing. ReadLine() reads only one line at a time.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#174852 - 2007-03-22 10:35 AM Re: reading file and comparing string [Re: booey]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
You might use GetFileSize() instead, and delete the file if it is very small.
Top
#174876 - 2007-03-22 06:00 PM Re: reading file and comparing string [Re: Richard H.]
booey Offline
Getting the hang of it

Registered: 2005-07-25
Posts: 76
Loc: USA
Thanks for the reply. I ended up using getfilesize() to only look at files that are 0-1KB in size.

Everything is working in my script now, but it's taking longer to run that it should. I have three different types of files that I need to run this script on so I have three "For Each" sections that are almost the same except for the $file (filename) variable and the INSTR search string. I put the "For Each" sections back to back. I thought I had to put a Next after each section, but give me an error when I do that. The script works fine other than it loops through each file in the array every time it performs the action on one file. It takes a couple minutes to run the script below, but if I comment out all but one "For Each" section it only takes a couple seconds. I'm sure I'm just missing something easy.

Here's my code:
 Code:
DIM $site $dir $file

$dir = "\\server\data\"
$site = "site1","site2","site3"

For Each $element In $site
$billingfile = $dir + $element + " " + "billing report" + ".csv"
	$sizebill = GetFileSize($billingfile)
	If ($sizebill > "0") and ($sizebill <= "1024")
		? "$billingfile size is $sizebill"
		Open(3, $billingfile) = 0
		$x1 = ReadLine(3)
	? "Line read: [" + $x1 + "]"
	Close (3)
	If InSTR($x1, "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||")
		? "$billingfile file is empty and will be deleted"
		DEL $billingfile
	;? @SERROR
		Else
	? "$billingfile file is not empty"
	EndIf

For Each $element In $site
$glfile = $dir + $element + " " + "ledger report" + ".csv"
	$sizegl = GetFileSize($glfile)
	If ($sizegl > "0") and ($sizegl <= "1024")
		? "$glfile size is $sizegl"
		Open(3, $glfile) = 0
		$x2 = ReadLine(3)
	? "Line read: [" + $x2 + "]"
	Close (3)
	If InSTR($x2, "|||||||||1")
		? "$glfile file is empty and will be deleted"
		DEL $glfile
	;? @SERROR
		Else
	? "$glfile file is not empty"
	EndIf
	

For Each $element In $site
$dxfile = $dir + $element + " " + "dx report" + ".csv"
	$sizedx = GetFileSize($dxfile)
	If ($sizedx > "0") and ($sizedx <= "1024")
		? "$dxfile size is $sizedx"
		Open(3, $dxfile) = 0
		$x3 = ReadLine(3)
	? "Line read: [" + $x3 + "]"
	Close (3)
	If InSTR($x3, "||||||1")
		? "$dxfile file is empty and will be deleted"
		DEL $dxfile
	;? @SERROR
		Else
	? "$dxfile file is not empty"
	EndIf		


So, my question is, how to I get each section to finish before the next one starts? Thanks.


Edited by booey (2007-03-22 06:03 PM)

Top
#174885 - 2007-03-22 07:18 PM Re: reading file and comparing string [Re: booey]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
You are missing an EndIF on your If ($Sizexx... sections. Then place your NEXT at the end of each section, and it should work for you.
_________________________
Today is the tomorrow you worried about yesterday.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2375 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.057 seconds in which 0.024 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org