Author Topic: _FileExists not working with wildcards  (Read 14053 times)

0 Members and 1 Guest are viewing this topic.

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
_FileExists not working with wildcards
« on: April 14, 2022, 02:25:08 pm »
I'm having a problem with _FileExists. It seems like this function does not handle wildcards.

My goal here is to see if there are any files in the same folder where the program is located named Trash1,txt, Trash2.txt, Trash3.txt, etc.

Take this as an example:

If _FileExists("Trash*.txt") Then
    Kill "Trash*.txt"
End If

The above program would fail to delete those files because _FileExists appears to be unable to handle wildcards.

If I would run the kill command by itself, it would successfully delete those files. However if there are no files named Trash*.txt, then the kill command would cause a failure in the program so I have no choice but to determine if any files named Trash*.txt exist first and that is what the "if" statement is supposed to do.

Since _FileExists won't work for this, can anyone suggest a workaround for me?



Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: _FileExists not working with wildcards
« Reply #1 on: April 14, 2022, 02:39:16 pm »
_FILEEXISTS isn't supposed to accept wildcards. Its purpose is to check if a specific file exists. if you need to check if a series of files exists then you will either need to make a loop that iterates through the total possible instances of a specific file, say if TRASH0000.TXT to TRASH9999.TXT was the naming convention. Otherwise you will need to probably look into a SHELL style command or some form of API call.
Granted after becoming radioactive I only have a half-life!

Marked as best answer by hanness on April 14, 2022, 10:51:34 am

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
Re: _FileExists not working with wildcards
« Reply #2 on: April 14, 2022, 02:39:32 pm »
Actually, I think that I just figured out a little kludge of a workaround.

Before executing the kill command, simply create a fake file called Trash0.txt. That guarantees a file of the format Trash*.txt exists and that will prevent the kill command from throwing an error.

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: _FileExists not working with wildcards
« Reply #3 on: April 14, 2022, 02:40:06 pm »
Code: QB64: [Select]
  1. For n% = 0 To 1000 'change if you expect more files
  2.     f$ = "Trash" + LTrim$(Str$(n%)) + ".txt"
  3.     If _FileExists(f$) Then Kill f$
  4. Next n%
  5.  
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: _FileExists not working with wildcards
« Reply #4 on: April 14, 2022, 02:42:47 pm »
Actually, I think that I just figured out a little kludge of a workaround.

Before executing the kill command, simply create a fake file called Trash0.txt. That guarantees a file of the format Trash*.txt exists and that will prevent the kill command from throwing an error.

Of course, easy workaround, if it's ok for you to create a file just to immediately delete it again.
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
Re: _FileExists not working with wildcards
« Reply #5 on: April 14, 2022, 02:51:20 pm »
Thanks for the suggestions. My example was simplified. The actual files in questions and not straight incrementing serial numbers. They actually contain a version number. Here is an example:

WIM_TOOLS_5.1.1.100.txt

I just tried my little fake file idea, and it works perfectly.

Funny, I was stumped by this for 2 hours. 2 minutes after I posted my question that idea spring into my head.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: _FileExists not working with wildcards
« Reply #6 on: April 14, 2022, 07:05:05 pm »
I would use a shell command to delete files with wildcards.


Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: _FileExists not working with wildcards
« Reply #7 on: April 14, 2022, 08:58:04 pm »
It's fundamentally the wrong idea to rely on _fileexists to say whether or not an IO operation will succeed. The file might exist but you have insufficient permission to open/delete it, another program might remove the file between your program checking and using the file, there may be a network error (if the file is from a network share). A more robust solution is to simply catch the error and respond accordingly:
Code: [Select]
'At the top of the program
On Error Goto general_error
Dim Shared Error_happened As Long

'Main Program

End
general_error:
Print "Fatal error"; err; "at line"; _errorline
end

io_error:
Error_happened = err
Resume Next

Sub delete_files(names$)
Error_happened = 0
On Error Goto io_error
Kill names$
On Error Goto general_error
If Error_happened then
  'Ignore or inform user, depending on use case
End If
End Sub

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
Re: _FileExists not working with wildcards
« Reply #8 on: April 15, 2022, 02:00:29 am »
Luke, I understand what you are saying, but in my particular use scenario, those concerns will never apply. I will ALWAYS have sufficient permission to delete those files, and it is 100% guaranteed that nothing else will remove the file.

But I do still understand and appreciate your point.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: _FileExists not working with wildcards
« Reply #9 on: April 15, 2022, 12:05:01 pm »
I would use a shell command to delete files with wildcards.

Really it's just one line of code? Why not that? Too easy!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: _FileExists not working with wildcards
« Reply #10 on: April 15, 2022, 12:46:52 pm »
Really it's just one line of code? Why not that? Too easy!

Maybe you can ask the guy who suggested it to write you up a demo on how to do it?  ;D
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: _FileExists not working with wildcards
« Reply #11 on: April 15, 2022, 04:57:02 pm »
Maybe you can ask the guy who suggested it to write you up a demo on how to do it?  ;D

OK this is what he came up with for Windows 10:
Attachment creates a safe folder to make and delete files, use exe's or edit and compile your own.

I did T*.txt instead of Trash*.txt
Code: QB64: [Select]
  1. shell _hide "del /f t*.txt"
  2.