Author Topic: FileClose not working?  (Read 491 times)

markpierzchala

  • Newbie
  • *
  • Posts: 2
FileClose not working?
« 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%)

JTaylor

  • Pundit
  • *****
  • Posts: 1813
    • Data & Stuff Inc.
Re: FileClose not working?
« Reply #1 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

markpierzchala

  • Newbie
  • *
  • Posts: 2
Re: FileClose not working?
« Reply #2 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.

td

  • Tech Support
  • *****
  • Posts: 4064
    • WinBatch
Re: FileClose not working?
« Reply #3 on: November 27, 2021, 04:55:08 pm »
A bug? Following produces the expected result.
Code: Winbatch
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
 

"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

  • Pundit
  • *****
  • Posts: 1633
Re: FileClose not working?
« Reply #4 on: November 28, 2021, 02:40:01 am »
Works fine with FileWrite() as well:
Code: Winbatch

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
 

td

  • Tech Support
  • *****
  • Posts: 4064
    • WinBatch
Re: FileClose not working?
« Reply #5 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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

  • Pundit
  • *****
  • Posts: 1633
Re: FileClose not working?
« Reply #6 on: November 29, 2021, 04:31:10 pm »
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.

td

  • Tech Support
  • *****
  • Posts: 4064
    • WinBatch
Re: FileClose not working?
« Reply #7 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...
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

kdmoyers

  • Sr. Member
  • ****
  • Posts: 457
Re: FileClose not working?
« Reply #8 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
The mind is everything; What you think, you become.

td

  • Tech Support
  • *****
  • Posts: 4064
    • WinBatch
Re: FileClose not working?
« Reply #9 on: December 02, 2021, 11:45:35 am »
At least any of sound mind.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

seckner

  • Jr. Member
  • **
  • Posts: 70
Re: FileClose not working?
« Reply #10 on: December 14, 2021, 11:45:56 am »
Speaking of Marty, can I decompile this script?