Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open list of files create another list of files

Status
Not open for further replies.

Majaws2

Programmer
Jul 3, 2003
15
US

I need to loop this script thru a list of about 100 files and perform the exact same operation on each file, then save the new as 100 new files. I have not been able to get that to work form me.. Any ideas? The files in question in this example are 701.cap and $aaa.txt...

Proc Parse

Count=0
num=0
num2=0
if fopen 0 "C:\Program Files\symantec\Procomm Plus\Aspect\traffic_fields.txt" READ ; open the Fields textfile
while not feof 0 ; Exit Loop at the end of the file
fgets 0 szline ; Retrieve the next line
strtok srchField[Count] szline "," 1 ; Enter the field header into an array
strtok holdthis szline "," 1 ; Retrieve the position for each field
atoi holdthis chrlen[Count] ; Convert the position to an integer and store in an array
Count += 1 ; Increment the counter
endwhile
fclose 0 ; Close the Fields textfile
endif

fopen 1 "C:\Program Files\symantec\Procomm Plus\Aspect\$aaa.txt" CREATE TEXT ; Create a new textfile
if fopen 0 "C:\Program Files\symantec\Procomm Plus\Capture\701.cap" READ TEXT ; Open the capture file (unless already open)
while not feof 0 ; Exit at the end of the file
fgets 0 szline ; Retrieve the next line
for Count2 = 0 upto Count-1 ; Loop to check each field
if strsearch szline srchField[Count2] ; Look for the field header in each line
strtok holdthis szline " " chrlen[Count2] ; Retrieve ASCII string according to field position (i.e. 9 = 9th field on the line)
strlen holdthis num ; Assign num the value of the length of the character string

if count2 == Count-1 ; places every entry on seperate line (csv)
fputs 1 holdthis
else

fwrite 1 holdthis num ; write the string to the output textfile
fwrite 1 "," 1 ; delimit the fields with a comma
endif
endif
endfor

If strsearch szline "Thu " ; Find the start line stats (day of week??)
strtok holdthis szline " " 4
atoi holdthis num2 ; Convert above number to an integer
fputs 1 holdthis
if num2 > 0 ; If the integer isn't null
fgets 0 szline ; then go to the 4th line down which is the
fgets 0 szline
fgets 0 szline
fgets 0 szline ; start of the line stat data.

while not strsearch szline "# exit"
strlen szline num2 ; Determine the character length of the line
if num2 > 45 ; If character length is >45 it is a line stats info line
fputs 1 szline ; Write the data to a new line in the output text file
endif
fgets 0 szline ; get the next line
endwhile
endif
endif

endwhile
endif

fclose 0
fclose 1

endproc



 
I would try having a text file that contained the 100 files plus 100 "new" filenames to use. I would have the source and target filenames on the same line, delimited in such a way that you can use strtok to split them into two values. For each line that you read from the file, you would call the parse procedure you already have and pass it two variables (one being the source file, the second being the target file). Then, in your already-existing fopen commands, you would replace the actual filenames with the proper string variables containing the data you read from the file.


aspect@aspectscripting.com
 
I tried to add something like that before and can't seem to get it to work.. I am still learning... Below is what I added to the middle of the script above... It does not even attempt to run. My text file contains 2 paths/filenames seperated by comma, just as my other text files do. Do you know of a sample I could compare to, in attempt to figure out... Thanks

if fopen 1 "C:\Program Files\symantec\Procomm Plus\Aspect\fields_fileLocation.txt" READ ; open the Fields textfile
while not feof 1 ; Exit Loop at the end of the file
fgets 0 szline1 ; Retrieve the next line
strtok srchField1[Count] szline "," 1 ; Enter the field header into an array
strtok holdthis szline1 "," 1 ; Retrieve the position for each field
atoi holdthis chrlen1[Count] ; Convert the position to an integer and store in an array
Count += 1 ; Increment the counter
endwhile
fclose 1 ; Close the Fields textfile
endif

;integer count = 0

while 1

strtok holdthis szline1 "," 1

if fopen 0 holdthis READ TEXT ; Open the capture file (unless already open)
if nullstr holdthis ; Check to see if item is null.
exitwhile ; If so, exit loop.
else
Count=0
strtok holdthis szline1 "," 1
fopen 1 holdthis CREATE TEXT ; Create a new textfile
endif
exitwhile
endif
endwhile

while not feof 0 ; Exit at the end of the file
fgets 0 szline
 
I have made a few changes and made some progress, I think anyway. It will compile ok, but I get an ERROR 5 upon running. All of the files and names appear to be valid. One thing that worked for some odd reason was to change the if nullstr holdthisjohn (line 142) to holdthis12 it would run and create a blank text file, but I didn't get the error 5... pulling out my hair.. any help is appreciated.


string Timestr ; declare string to contain time
string SysInfo ; declare string array
string holdthis ; declare string variable
string capdata ; declare string array
string holdthisjohn ; declare string variable
string srchField[200] ; declare array for field names
integer chrlen[200] ; declare array for field postition
integer num2 ;declare variable
integer Num ;Declare variables
integer Count = 0 ;Declare variables
integer Count2 = 0 ;Declare variables
string szLine ;Declare variables

proc main

Timestr = "23:00:00" ; Assign time for script to run

clear ; Clear the Procomm Screen
set capture overwrite on ; Overwrite Existing capture File
set capture query off ; Do Not Request Query of File Name


if fopen 0 "C:\Program Files\symantec\Procomm Plus\Aspect\Traffic_master.txt" READ ; open textfile TEXT,LOGIN,IP
while not feof 0 ; Exit Loop at the end of the file
fgets 0 SysInfo ;Retrieve the next line place in public variable

;Call LOGIN

endwhile
fclose 0 ; Close the Fields textfile
else
errormsg "Couldn't open file Traffic_master.txt."
endif

if fopen 0 "C:\Program Files\symantec\Procomm Plus\Aspect\Traffic_files.txt" READ ; open textfile TEXT,LOGIN,IP
while not feof 0 ; Exit Loop at the end of the file
fgets 0 capdata ;Retrieve the next line place in public variable

Call Parse

endwhile
fclose 0 ; Close the Fields textfile
else
errormsg "Couldn't open file Traffic_files.txt."
endif


;Call beforeParse
Call excel

endproc

proc LOGIN

integer Count = 0 ; declare variables
winmaximize $pwmainwin

while 1 ; Loop forever, or until EXITWHILE.
strtok holdthis SysInfo "," 1 ; Retrieve next piece of data from system info text and populate to Serverdata array.
set capture file holdthis ; Set the Capture File Name

if nullstr holdthis ; Check to see if item is null.
exitwhile ; If so, exit loop.
else
Count=0

strtok holdthis SysInfo "," 1 ; Retrieve IP address from system info text.
DIALNUMBER TELNET holdthis ; Contact IP address of server
waitfor "Enter Terminal type : "
transmit "ansi^M" ; keystroke "ansi"
waitfor "Login: "
transmit "root^M" ; keystroke "HD_spec" and enter
waitfor "Password: "

strtok holdthis SysInfo "," 1 ; Retrieve password from system info text.
transmit holdthis
transmit "^M" ; keystroke enter
capture on
waitfor "COMMAND (N/Q/V/P/D/X): " 5 ; This command may never come on some systems causing a 5 second delay
transmit "N^M" ; keystroke "N" and enter (if delayed from previous step, this will cause the (M/R/S/X):prompt to occur
waitfor "COMMAND (M/R/S/X): " 5 ; This command may never come on some systems causing a 5 second delay
transmit "q^M" ; keystroke "q" and enter
waitfor "Go To QNX? [y/N] "
transmit "y^M" ; keystroke "y" and enter
waitfor "# "
transmit "cd //1/usr/vm/sprint^M"
waitfor "# "
transmit "ls -l^M"
waitfor "# "
transmit "statistics -v^M" ;speech blocks
waitfor "# "
transmit "st_lmenu^M" ; keystroke "ad" and Enter
waitfor ": " ; wait for server response
transmit "d16^M"
waitfor ": "
transmit "e18^M"
waitfor ": "
transmit "f4^M"
waitfor ": "
transmit "g4^M"
waitfor ": "
transmit "hy^M"
waitfor ": "
transmit "r^M"
waitfor "/F): "
transmit "c^M"
waitfor "X): "
transmit "x^M"
waitfor "# "
capture off
transmit "logout^M"
endif
endwhile
endproc



Proc Parse
string holdthisjohn

chdir "C:\Program Files\symantec\Procomm Plus\capture"

Count=0
num=0
num2=0
if fopen 0 "C:\Program Files\symantec\Procomm Plus\Aspect\traffic_fields.txt" READ ; open the Fields textfile
while not feof 0 ; Exit Loop at the end of the file
fgets 0 szline ; Retrieve the next line
strtok srchField[Count] szline "," 1 ; Enter the field header into an array
strtok holdthis szline "," 1 ; Retrieve the position for each field
atoi holdthis chrlen[Count] ; Convert the position to an integer and store in an array
Count += 1 ; Increment the counter
endwhile
fclose 0 ; Close the Fields textfile
endif

while 1 ; Loop forever, or until EXITWHILE.
strtok holdthisjohn capdata "," 1 ; Retrieve next piece of data from system info text and populate to Serverdata array.
fopen 1 holdthisjohn Create TEXT ; Create a new textfile

if nullstr holdthisjohn ; Check to see if item is null.
exitwhile ; If so, exit loop.
else
Count=0

strtok holdthisjohn capdata "," 1 ; Retrieve IP address from system info text.
fopen 0 holdthisjohn read TEXT ;Open the capture file

endif

endwhile
while not feof 0 ; Exit at the end of the file
fgets 0 szline ; Retrieve the next line
for Count2 = 0 upto Count-1 ; Loop to check each field
if strsearch szline srchField[Count2] ; Look for the field header in each line
strtok holdthis szline " " chrlen[Count2] ; Retrieve ASCII string according to field position (i.e. 9 = 9th field on the line)
strlen holdthis num ; Assign num the value of the length of the character string

if count2 == Count-1 ; places every entry on seperate line (csv)
fputs 1 holdthis
else

fwrite 1 holdthis num ; write the string to the output textfile
fwrite 1 "," 1 ; delimit the fields with a comma
endif
endif
endfor

If strsearch szline "Thu " ; Find the start line stats (day of week??)
strtok holdthis szline " " 4
atoi holdthis num2 ; Convert above number to an integer
fputs 1 holdthis
if num2 > 0 ; If the integer isn't null
fgets 0 szline ; then go to the 4th line down which is the
fgets 0 szline
fgets 0 szline
fgets 0 szline ; start of the line stat data.

while not strsearch szline "# exit"
strlen szline num2 ; Determine the character length of the line
if num2 > 45 ; If character length is >45 it is a line stats info line
fputs 1 szline ; Write the data to a new line in the output text file
endif
fgets 0 szline ; get the next line
endwhile
endif
endif

endwhile

;endif
fclose 0

endproc

Proc excel

string prog = "T:\Projects_2004\John\Traffic\LLCJ.xls" ;Excel file w/ Macro to open
run prog

endproc
 
Sorry I didn't get back on this one, looks like it slipped my mind.

Error 5 will come up whenever your script tries to access a file ID that is not open. I see that most of the fopen commands in your script use if fopen to check that the file opened successfully before accessing it, but there are two calls at the end that do not do this, those lines are:

fopen 1 holdthisjohn Create TEXT
fopen 0 holdthisjohn read TEXT

My hunch is that the value you expect to be in holdthisjohn is not correct when the file is opened.

I also noticed that you had a global variable called holdthisjohn, as well as a local string variable in the parse procedure of the same name. I would eliminate one or the other as I think this would cause problems.




aspect@aspectscripting.com
 
If Practice makes perfect, I should be getting close... just kidding still far from being close understanding this as I want to. Closer though.. I have done some good.. I have the script running with No Run Time errors at all, I think it was combo of not checking file open status as you said and mixed up file id, and I rearranged some of the code a bit. Next issue is that the script runs, but it does not create my new file, nor does it read my existing file. (I removed from dir and got no error) I have tried to move the endif and endwhile statments around a bit still not working. It does open the Traffic_files.txt ok though, it contains one row (testjohn.txt,701.cap) Below is a snipit of variables and the Parse proc that I am working on. Note the 2 usermsg in the code I was using to assist in finding where errors were... worked good ;-)

string Timestr ; declare string to contain time
string capdata
string johntest
string SysInfo ; declare string array
string holdthis ; declare string variable
string srchField[200] ; declare array for field names
integer chrlen[200] ; declare array for field postition
integer num2 ;declare variable
integer Num ;Declare variables
integer Count = 0 ;Declare variables
integer Count2 = 0 ;Declare variables
string szLine ;Declare variables

Proc Parse

chdir "C:\Program Files\symantec\Procomm Plus\capture"

Count=0
num=0
num2=0
if fopen 2 "traffic_fields.txt" READ ; open the Fields textfile

while not feof 2 ; Exit Loop at the end of the file

fgets 2 szline ; Retrieve the next line
strtok srchField[Count] szline "," 1 ; Enter the field header into an array
strtok holdthis szline "," 1 ; Retrieve the position for each field
atoi holdthis chrlen[Count] ; Convert the position to an integer and store in an array
Count += 1 ; Increment the counter
endwhile
usermsg "9"
fclose 2 ; Close the Fields textfile
endif



if fopen 0 "Traffic_files.txt" READ ; open textfile TEXT,LOGIN,IP
while not feof 0 ; Exit Loop at the end of the file
fgets 0 capdata ;Retrieve the next line place in public variable
endwhile
fclose 0 ; Close the Fields textfile
else
errormsg "Couldn't open file Traffic_files.txt."
endif
while 1 ; Loop forever, or until EXITWHILE.
strtok johntest capdata "," 1 ; Retrieve next piece of data from system info text and populate to Serverdata array.



fopen 1 johntest CREATE TEXT ; Create a new textfile
strtok johntest capdata "," 1

if nullstr johntest ; Check to see if item is null.
exitwhile ; If so, exit loop.
else
Count=0







fopen 0 johntest READ TEXT ; Open the capture file (unless already open)
endif
endwhile

while not feof 0 ; Exit at the end of the file


fgets 0 szline ; Retrieve the next line
for Count2 = 0 upto Count-1 ; Loop to check each field
if strsearch szline srchField[Count2] ; Look for the field header in each line
strtok holdthis szline " " chrlen[Count2] ; Retrieve ASCII string according to field position (i.e. 9 = 9th field on the line)
strlen holdthis num ; Assign num the value of the length of the character string

if count2 == Count-1 ; places every entry on seperate line (csv)
fputs 1 holdthis
else

fwrite 1 holdthis num ; write the string to the output textfile
fwrite 1 "," 1 ; delimit the fields with a comma
endif
endif
endfor

If strsearch szline "Thu " ; Find the start line stats (day of week??)
strtok holdthis szline " " 4
atoi holdthis num2 ; Convert above number to an integer
fputs 1 holdthis
if num2 > 0 ; If the integer isn't null
fgets 0 szline ; then go to the 4th line down which is the
fgets 0 szline
fgets 0 szline
fgets 0 szline ; start of the line stat data.
while not strsearch szline "# exit"
strlen szline num2 ; Determine the character length of the line
if num2 > 45 ; If character length is >45 it is a line stats info line
fputs 1 szline ; Write the data to a new line in the output text file
endif
fgets 0 szline ; get the next line
endwhile
endif
endif

endwhile
endif


usermsg "10"


endproc
 
This one may be slightly diff, but still not even reading or writing the files, but does not fail, runs thru completely. I get the usermsg 9 and 10 and the next proc runs. I am almost bald now, pulling my hair out...

Proc Parse

chdir "C:\Program Files\symantec\Procomm Plus\capture"

Count=0
num=0
num2=0
if fopen 2 "traffic_fields.txt" READ ; open the Fields textfile

while not feof 2 ; Exit Loop at the end of the file

fgets 2 szline ; Retrieve the next line
strtok srchField[Count] szline "," 1 ; Enter the field header into an array
strtok holdthis szline "," 1 ; Retrieve the position for each field
atoi holdthis chrlen[Count] ; Convert the position to an integer and store in an array
Count += 1 ; Increment the counter
endwhile
usermsg "9"
fclose 2 ; Close the Fields textfile
endif



if fopen 0 "Traffic_files.txt" READ ; open textfile TEXT,LOGIN,IP
while not feof 0 ; Exit Loop at the end of the file
fgets 0 capdata ;Retrieve the next line place in public variable
endwhile
fclose 0 ; Close the Fields textfile
else
errormsg "Couldn't open file Traffic_files.txt."
endif
while 1 ; Loop forever, or until EXITWHILE.
strtok johntest capdata "," 1 ; Retrieve next piece of data from system info text and populate to Serverdata array.



fopen 1 johntest CREATE TEXT ; Create a new textfile
strtok johntest capdata "," 1

if nullstr johntest ; Check to see if item is null.
exitwhile ; If so, exit loop.
else
Count=0
fopen 0 johntest READ TEXT ; Open the capture file (unless already open)
;endif
;endwhile

while not feof 0 ; Exit at the end of the file


fgets 0 szline ; Retrieve the next line
for Count2 = 0 upto Count-1 ; Loop to check each field
if strsearch szline srchField[Count2] ; Look for the field header in each line
strtok holdthis szline " " chrlen[Count2] ; Retrieve ASCII string according to field position (i.e. 9 = 9th field on the line)
strlen holdthis num ; Assign num the value of the length of the character string

if count2 == Count-1 ; places every entry on seperate line (csv)
fputs 1 holdthis
else

fwrite 1 holdthis num ; write the string to the output textfile
fwrite 1 "," 1 ; delimit the fields with a comma
endif
endif
endfor

If strsearch szline "Thu " ; Find the start line stats (day of week??)
strtok holdthis szline " " 4
atoi holdthis num2 ; Convert above number to an integer
fputs 1 holdthis
if num2 > 0 ; If the integer isn't null
fgets 0 szline ; then go to the 4th line down which is the
fgets 0 szline
fgets 0 szline
fgets 0 szline ; start of the line stat data.
while not strsearch szline "# exit"
strlen szline num2 ; Determine the character length of the line
if num2 > 45 ; If character length is >45 it is a line stats info line
fputs 1 szline ; Write the data to a new line in the output text file
endif
fgets 0 szline ; get the next line
endwhile
endif
endif

endwhile
endif
endwhile


usermsg "10"


endproc
 
One thing I noticed is that you use the variable johntest to hold the filename for two files that are open at the same time (fopen 1 johntest CREATE TEXT and fopen 0 johntest READ TEXT). I would change one of the file (either file ID 1 or 0) to use a different variable pointing to a different filename. My hunch is that since the file is opened already in read mode that the attempt to open the file in create mode fails and that is why you are not getting the expected results.


aspect@aspectscripting.com
 
I had rearranged the entire code before I saw your reply, with no luck, so I put it back and changed johntest to johntest1 for id 0. It ran thru the first time and created the new file, but it was blank. I went to run it again to see why the parse of data failed, but it would not create the file again. It also would not read the 1st file again. Odd huh, it works once, but not again? I tried on a diff. machine, but it didn't work the first time. [dazed]
 
Almost perfect now... if I try to read/create files from a text file, it will read all file names correctly (i.e. johntest & johnstest1) it even creates all files, but it only runs the code starting with fgets 0 szline on any except last group in the text file (so all created files are empty except for last).

But if i substitute the text file and define capdata in the script, instead of reading traffic_files.txt it will search, parse, and create all files perfectly. (i.e. capdata = "johntest1.txt,701.cap,johntest2.txt,702.cap")

It may not be pretty, but I am learning.. Any ideas??

Proc Parse

chdir "C:\Program Files\symantec\Procomm Plus\capture"

Count=0
num=0
num2=0
if fopen 0 "traffic_fields.txt" READ ; open the Fields textfile
while not feof 0 ; Exit Loop at the end of the file
fgets 0 szline ; Retrieve the next line
strtok srchField[Count] szline "," 1 ; Enter the field header into an array
strtok holdthis szline "," 1 ; Retrieve the position for each field
atoi holdthis chrlen[Count] ; Convert the position to an integer and store in an array
Count += 1 ; Increment the counter
endwhile
fclose 0 ; Close the Fields textfile
endif
if fopen 3 "Traffic_files.txt" READ ; open textfile TEXT,LOGIN,IP
while not feof 3 ; Exit Loop at the end of the file
fgets 3 capdata ;Retrieve the next line place in public variable
capdata = "johntest1.txt,701.cap,johntest2.txt,702.cap"
while 1 ; Loop forever, or until EXITWHILE.
strtok johntest capdata "," 1 ; Retrieve next piece of data from system info text and populate to Serverdata array.
if nullstr johntest ; Check to see if item is null.
exitwhile ; If so, exit loop.
else
;usermsg johntest
fopen 1 johntest CREATE TEXT ; Create a new textfile
strtok johntest1 capdata "," 1
;usermsg johntest1
if fopen 0 johntest1 READ TEXT ; Open the capture file (unless already open)

while not feof 0 ; Exit at the end of the file
fgets 0 szline ; Retrieve the next line
for Count2 = 0 upto Count-1 ; Loop to check each field
if strfind szline srchField[Count2] ; Look for the field header in each line
usermsg "parse"
strtok holdthis szline " " chrlen[Count2] ; Retrieve ASCII string according to field position (i.e. 9 = 9th field on the line)
strlen holdthis num ; Assign num the value of the length of the character string
if count2 == Count-1 ; places every entry on seperate line (csv)
fputs 1 holdthis
else
fwrite 1 holdthis num ; write the string to the output textfile
fwrite 1 "," 1 ; delimit the fields with a comma
endif
endif
endfor
If strsearch szline "Thu " ; Find the start line stats (day of week??)
strtok holdthis szline " " 4
atoi holdthis num2 ; Convert above number to an integer
fputs 1 holdthis
if num2 > 0 ; If the integer isn't null
fgets 0 szline ; then go to the 4th line down which is the
fgets 0 szline
fgets 0 szline
fgets 0 szline ; start of the line stat data.
while not strsearch szline "#"
strlen szline num2 ; Determine the character length of the line
if num2 > 45 ; If character length is >45 it is a line stats info line
fputs 1 szline ; Write the data to a new line in the output text file
endif
fgets 0 szline ; get the next line
endwhile
endif
endif
endwhile
endif
endif
endwhile
fclose 1
endwhile
else
errormsg "Couldn't open file Traffic_files.txt."
endif
fclose 0
fclose 3
usermsg "10"
endproc
 
Looks like you need to move the fclose 0 statement at the end of the script to the end of the "if fopen 0 johntest1 READ TEXT" structure (i.e. just before the corresponding endif). What I think is happening is that either the "if fopen 0 johntest1 READ TEXT" is failing since the file had been opened previously, or that the fgets commands are failing since all of the data has been read from the file. If you move the fclose 0 command into the if/endif structure, then file ID 0 will be closed when the fopen command is called again.


aspect@aspectscripting.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top