WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: markpierzchala on November 27, 2021, 01:07:54 PM

Title: FileClose not working?
Post by: markpierzchala on November 27, 2021, 01:07:54 PM
I have a FileOpen, FileWrite, FileClose within a WHILE loop. The FileClose does not seem to work. The code follows. DD is a string variable that I compute above. As I watch the program execute, I can see in every WHILE loop that the value of zz decrements by 1: 127, 126, 125, etc. Sometimes I want to run the loop well over 128 times, but it stops after it exhausts the number of file handles. Then I get WinBatch error 3078.

   handle3 = FileOpen("History\KeyLists.csv", "APPEND")
   FileWrite(handle3, DD)
   FileClose(handle3)
   zz = IntControl(77, 21, 0, 0, 0)
   Display(2, "Number of file slots open: ", %zz%)
Title: Re: FileClose not working?
Post by: JTaylor on November 27, 2021, 01:19:48 PM
Why are you opening and closing within the While Loop?   If the file open count is decreasing then you shouldn't run out of handles anyway but still doesn't make sense, from what you have shared, why you are doing it this way.

Jim
Title: Re: FileClose not working?
Post by: markpierzchala on November 27, 2021, 04:09:18 PM
Thank you for your quick reply. I moved the opening and closing of the file outside the WHILE loop and that seems to have solved the problem.

As to why I tried it that way, who knows. I was trying many different things to make a number of things work. I don't think I started out that way. I knew it was inefficient, but it should have worked anyway. Where I come from that's a bug.

I use WinBatch every 2 or 3 years. I like WinBatch. I find the language strong, and the help mostly useful. But to determine how WinBatch works with the Windows operating system is a learning curve.

Nevertheless, that problem seems to be resolved. Thank you.
Title: Re: FileClose not working?
Post by: td on November 27, 2021, 04:55:08 PM
A bug? Following produces the expected result.
Code (winbatch) Select
Before = IntControl(77,21,0,0,0)
for x = 1 to 256
   hFile = FileOpen('C:\temp\dummy.txt','read')
   junk = FileRead(hFile)
   FileClose(hFile)
next
After = IntControl(77,21,0,0,0)

Message('File Handle Table Entries Available', 'Before: ':Before:@lf:'After: ':After)
exit


Title: Re: FileClose not working?
Post by: stanl on November 28, 2021, 02:40:01 AM
Works fine with FileWrite() as well:
Code (WINBATCH) Select


Before = IntControl(77,21,0,0,0)
for x = 1 to 256
   hFile = FileOpen('C:\temp\dummy.txt','append')
   junk = FileWrite(hFile,x)
   FileClose(hFile)
next
After = IntControl(77,21,0,0,0)


Message('File Handle Table Entries Available', 'Before: ':Before:@lf:'After: ':After)
exit
Title: Re: FileClose not working?
Post by: td on November 29, 2021, 08:05:21 AM
The FileOpen mode matters when closing a filehandle. The FileClose function does not consider or use the mode information in any way.
Title: Re: FileClose not working?
Post by: stanl on November 29, 2021, 04:31:10 PM
Quote from: td on November 29, 2021, 08:05:21 AM
The FileOpen mode matters when closing a filehandle. The FileClose function does not consider or use the mode information in any way.


Understood. Just tried to support with same modes used by OP.
Title: Re: FileClose not working?
Post by: td on November 30, 2021, 07:33:47 AM
That was a typo on my part (nothing new). The statement should have read "the FileOpen mode does not matter when closing a filehandle." Marty would have been proud...
Title: Re: FileClose not working?
Post by: kdmoyers on November 30, 2021, 11:17:45 AM
Whew. I had read those two sentences twenty times and finally concluded that there are things mortal man was never meant to understand.
-K
Title: Re: FileClose not working?
Post by: td on December 02, 2021, 11:45:35 AM
At least any of sound mind.
Title: Re: FileClose not working?
Post by: seckner on December 14, 2021, 11:45:56 AM
Speaking of Marty, can I decompile this script?