Page 1 of 2
[SOLVED] DeleteFile("Name*.*)
Posted: Mon Jun 01, 2020 10:37 am
by StarWarsFan
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!
Code: Select all
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)
Re: DeleteFile("Name*.*)
Posted: Mon Jun 01, 2020 2:11 pm
by RASHAD
Try
Code: Select all
ExamineDirectory(0,GetCurrentDirectory(), "*.*")
While NextDirectoryEntry(0)
If DirectoryEntryType(0) = #PB_DirectoryEntry_File
Filename$ = DirectoryEntryName(0)
If Left(Filename$,4) = "name"
DeleteFile(Filename$,#PB_FileSystem_Force)
EndIf
EndIf
Wend
FinishDirectory(0)
Re: DeleteFile("Name*.*)
Posted: Mon Jun 01, 2020 2:26 pm
by ebs
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()'.
Regards,
Eric
Re: DeleteFile("Name*.*)
Posted: Mon Jun 01, 2020 3:13 pm
by skywalk
Though less elegant, you can use RunProgram() and your choice of command line argument or batch file.
Re: DeleteFile("Name*.*)
Posted: Wed Jun 03, 2020 12:30 pm
by StarWarsFan
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?
Re: DeleteFile("Name*.*)
Posted: Wed Jun 03, 2020 12:53 pm
by infratec
Hm...
it is not standard in C, C#, VB
https://forums.asp.net/t/1899755.aspx?H ... h+wildcard+
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.
Re: DeleteFile("Name*.*)
Posted: Wed Jun 03, 2020 12:53 pm
by Marc56us
StarWarsFan wrote:
Is there really no easy way to simply delete all files starting with "Name*" in the current directory?
Re: DeleteFile("Name*.*)
Posted: Wed Jun 03, 2020 1:13 pm
by Derren
Now
that is counter-intuitive

Re: DeleteFile("Name*.*)
Posted: Wed Jun 03, 2020 1:23 pm
by Marc56us
Derren wrote:Now
that is counter-intuitive

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.

Re: DeleteFile("Name*.*)
Posted: Wed Jun 03, 2020 1:50 pm
by ebs
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.
Re: DeleteFile("Name*.*)
Posted: Thu Jun 04, 2020 12:45 am
by BarryG
Derren wrote:Now
that is counter-intuitive

Macros to the rescue!
Code: Select all
Macro DeleteWithWildcards(dir,pattern)
DeleteDirectory(dir,pattern)
EndMacro
DeleteWithWildcards(".","Name*.*")
Re: DeleteFile("Name*.*)
Posted: Thu Jun 04, 2020 10:56 am
by StarWarsFan
Wow! Thanks!
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."
Re: DeleteFile("Name*.*)
Posted: Thu Jun 04, 2020 11:28 am
by Derren
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()"
Re: DeleteFile("Name*.*)
Posted: Thu Jun 04, 2020 11:44 am
by StarWarsFan
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
Re: [SOLVED] DeleteFile("Name*.*)
Posted: Thu Jun 04, 2020 1:48 pm
by NicTheQuick
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.
See this example:
Code: Select all
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?