[SOLVED] DeleteFile("Name*.*)

Just starting out? Need help? Post your questions and find answers here.
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

[SOLVED] DeleteFile("Name*.*)

Post 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)
Last edited by StarWarsFan on Thu Jun 04, 2020 11:44 am, edited 1 time in total.
Image - 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.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: DeleteFile("Name*.*)

Post 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)
Egypt my love
ebs
Enthusiast
Enthusiast
Posts: 530
Joined: Fri Apr 25, 2003 11:08 pm

Re: DeleteFile("Name*.*)

Post 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
User avatar
skywalk
Addict
Addict
Posts: 3997
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: DeleteFile("Name*.*)

Post by skywalk »

Though less elegant, you can use RunProgram() and your choice of command line argument or batch file.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

Re: DeleteFile("Name*.*)

Post 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?
Image - 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.
infratec
Always Here
Always Here
Posts: 6873
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: DeleteFile("Name*.*)

Post 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.
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: DeleteFile("Name*.*)

Post by Marc56us »

StarWarsFan wrote: Is there really no easy way to simply delete all files starting with "Name*" in the current directory?

Code: Select all

DeleteDirectory(".", "Name*.*")
User avatar
Derren
Enthusiast
Enthusiast
Posts: 313
Joined: Sat Jul 23, 2011 1:13 am
Location: Germany

Re: DeleteFile("Name*.*)

Post by Derren »

Now that is counter-intuitive :D
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: DeleteFile("Name*.*)

Post by Marc56us »

Code: Select all

DeleteDirectory(".", "Name*.*")
Derren wrote:Now that is counter-intuitive :D
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. 8)
ebs
Enthusiast
Enthusiast
Posts: 530
Joined: Fri Apr 25, 2003 11:08 pm

Re: DeleteFile("Name*.*)

Post 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.
BarryG
Addict
Addict
Posts: 3324
Joined: Thu Apr 18, 2019 8:17 am

Re: DeleteFile("Name*.*)

Post by BarryG »

Derren wrote:Now that is counter-intuitive :D
Macros to the rescue!

Code: Select all

Macro DeleteWithWildcards(dir,pattern)
  DeleteDirectory(dir,pattern)
EndMacro

DeleteWithWildcards(".","Name*.*")
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

Re: DeleteFile("Name*.*)

Post 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."
Image - 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.
User avatar
Derren
Enthusiast
Enthusiast
Posts: 313
Joined: Sat Jul 23, 2011 1:13 am
Location: Germany

Re: DeleteFile("Name*.*)

Post 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()"
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

Re: DeleteFile("Name*.*)

Post 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
Image - 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.
User avatar
NicTheQuick
Addict
Addict
Posts: 1226
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: [SOLVED] DeleteFile("Name*.*)

Post 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?
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.
Post Reply