Hey good morning dear friends. I am stuck with a very basic problem.
Is there really no easy way to simply delete all files starting with "Name*" in the current directory?
DeleteFile() seems to be unable to do that.
Am I blind or is that option so well hidden?
I am currently using this routine as workaround, but would like to learn!
ExamineDirectory(0,GetCurrentDirectory(), "Name*.*")
While NextDirectoryEntry(0)
If DirectoryEntryType(0) = #PB_DirectoryEntry_File And Left(DirectoryEntryName(0),4)="Name"
DeleteFile(DirectoryEntryName(0)) : EndIf
Wend : FinishDirectory(0)
// Edit: Code-Tags added (Kiffi)
Last edited by StarWarsFan on Thu Jun 04, 2020 11:44 am, edited 1 time in total.
- There is usually a lot of "try this, maybe do that" but ONLY an example that one can test for themself and get an immediate result actually brings people forward.
Since wildcards are not supported for 'DeleteFile()', I use a routine pretty much like yours.
One suggestion - you should be able to remove the check of the leftmost four characters in the filename,
since you have already used a pattern of "Name*.*" in 'ExamineDirectory()'.
ebs wrote:Since wildcards are not supported for 'DeleteFile()', I use a routine pretty much like yours.
Well, then I am not totally blind. Thank you for that :----)
Are such useful (and otherwise standard) wildcards ever be implemented in PB?
- There is usually a lot of "try this, maybe do that" but ONLY an example that one can test for themself and get an immediate result actually brings people forward.
If you want it simple, use RunProgram("cmd, "/c delete *.*")
But I don't think they do it in an other way than you and I don't think that it is faster.
Yes, but it's a trick of the DOS era « . » (dot) is a pseudo file that contains the list of files in directory. You can use it for different things. Older system administrators are familiar with it and use it in batch files for years.
Actually, the "." and ".." entries are shortcuts for the current directory and the parent directory respectively, not their contents.
DeleteDirectory() can be used to delete multiple files from any directory by specifying the directory path as the first parameter.
However, you have to be careful:
If you delete all files in the directory, (Pattern$ = "*.*"), it will also delete the directory itself.
If you leave any files in the directory, then the directory will remain.
ebs wrote:DeleteDirectory() can be used to delete multiple files from any directory by specifying the directory path as the first parameter.
I was totally mislead by PureBasic's documentation on that command stating: "Deletes the specified Directory$."
That should really be amended to: "Deletes the specified Directory$ or files in that Directory$ matching the provided pattern."
- There is usually a lot of "try this, maybe do that" but ONLY an example that one can test for themself and get an immediate result actually brings people forward.
That's what I meant by counter-intuitive.
Imo the wildcard should be moved to DeleteFile and then you have 2 commands that work as expected. As of now, we have 2 commands that don't wor as expected. One is missing a vital function. Another has a functionality that we don't expect it to have.
At the very least, there should be a big, bold reference in the help, pointing from DeleteFile() to DeleteDirectory().
"Looking for a way to delete multiple files at once where you can also use wildcards? ---> DeleteDirectory()"
Derren wrote:Another has a functionality that we don't expect it to have.
True! Definitely and by far not! I did not even think of looking into that command - as explained above.
Derren wrote:At the very least, there should be a big, bold reference in the help, pointing from DeleteFile() to DeleteDirectory().
"Looking for a way to delete multiple files at once where you can also use wildcards? ---> DeleteDirectory()"
ACK +1
- There is usually a lot of "try this, maybe do that" but ONLY an example that one can test for themself and get an immediate result actually brings people forward.
On Linux wildcards like '*' and '?' and many more things are usually interpreted by the shell. You totally could create a file named "*" or "?" because on linux file systems every character is allowed except of the forward slash (/). So it would be an issue if DeleteFile() would interpret wildcards when you really want to delete a file with a filename containing "*" or "?". If often create files with a question mark in it.
nicolas@tp-w530:~/tmp/purebasic/wildcards$ echo "what?" > "*"
nicolas@tp-w530:~/tmp/purebasic/wildcards$ echo "yes" > "?"
nicolas@tp-w530:~/tmp/purebasic/wildcards$ ll
insgesamt 16
-rw-rw-r-- 1 nicolas nicolas 6 Jun 4 14:47 '*'
drwxrwxr-x 2 nicolas nicolas 4096 Jun 4 14:47 ./
drwxrwxr-x 7 nicolas nicolas 4096 Jun 4 14:47 ../
-rw-rw-r-- 1 nicolas nicolas 4 Jun 4 14:47 '?'
nicolas@tp-w530:~/tmp/purebasic/wildcards$ cat *
what?
yes
nicolas@tp-w530:~/tmp/purebasic/wildcards$ cat "*"
what?
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.