QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: hanness on April 14, 2022, 02:25:08 pm

Title: _FileExists not working with wildcards
Post by: hanness 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?


Title: Re: _FileExists not working with wildcards
Post by: Cobalt 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.
Title: Re: _FileExists not working with wildcards
Post by: hanness 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.
Title: Re: _FileExists not working with wildcards
Post by: RhoSigma 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.  
Title: Re: _FileExists not working with wildcards
Post by: RhoSigma 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.
Title: Re: _FileExists not working with wildcards
Post by: hanness 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.
Title: Re: _FileExists not working with wildcards
Post by: bplus on April 14, 2022, 07:05:05 pm
I would use a shell command to delete files with wildcards.

Title: Re: _FileExists not working with wildcards
Post by: luke 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
Title: Re: _FileExists not working with wildcards
Post by: hanness 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.
Title: Re: _FileExists not working with wildcards
Post by: bplus 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!
Title: Re: _FileExists not working with wildcards
Post by: SMcNeill 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
Title: Re: _FileExists not working with wildcards
Post by: bplus 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.