Active Forums > QB64 Discussion

_FileExists not working with wildcards

(1/3) > >>

hanness:
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?


Cobalt:
_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.

hanness:
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.

RhoSigma:

--- Code: QB64: ---For n% = 0 To 1000 'change if you expect more files    f$ = "Trash" + LTrim$(Str$(n%)) + ".txt"    If _FileExists(f$) Then Kill f$Next n% 

RhoSigma:

--- Quote from: 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.

--- End quote ---

Of course, easy workaround, if it's ok for you to create a file just to immediately delete it again.

Navigation

[0] Message Index

[#] Next page

Go to full version