#174795 - 2007-03-20 10:50 PM
reading file and comparing string
|
booey
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:
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
|
|
|
|
#174834 - 2007-03-21 06:20 PM
Re: reading file and comparing string
[Re: Gargoyle]
|
booey
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
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
|
|
|
|
#174876 - 2007-03-22 06:00 PM
Re: reading file and comparing string
[Re: Richard H.]
|
booey
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:
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
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 2009 anonymous users online.
|
|
|