It is currently Wed Jul 08, 2020 11:54 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 15 posts ] 
Author Message
 Post subject: [SOLVED] DeleteFile("Name*.*)
PostPosted: Mon Jun 01, 2020 10:37 am 
Offline
User
User

Joined: Sat Mar 14, 2015 11:53 am
Posts: 74
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:
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)

_________________
There's usually a lot of "try this, do that" but ONLY an example source that one can test for themself and get an immediate result brings people forward.


Last edited by StarWarsFan on Thu Jun 04, 2020 11:44 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: DeleteFile("Name*.*)
PostPosted: Mon Jun 01, 2020 2:11 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3645
Try
Code:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: DeleteFile("Name*.*)
PostPosted: Mon Jun 01, 2020 2:26 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Apr 25, 2003 11:08 pm
Posts: 456
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


Top
 Profile  
Reply with quote  
 Post subject: Re: DeleteFile("Name*.*)
PostPosted: Mon Jun 01, 2020 3:13 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 3299
Location: Boston, MA
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


Top
 Profile  
Reply with quote  
 Post subject: Re: DeleteFile("Name*.*)
PostPosted: Wed Jun 03, 2020 12:30 pm 
Offline
User
User

Joined: Sat Mar 14, 2015 11:53 am
Posts: 74
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's usually a lot of "try this, do that" but ONLY an example source that one can test for themself and get an immediate result brings people forward.


Top
 Profile  
Reply with quote  
 Post subject: Re: DeleteFile("Name*.*)
PostPosted: Wed Jun 03, 2020 12:53 pm 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 4799
Location: Germany
Hm...

it is not standard in C, C#, VB

https://forums.asp.net/t/1899755.aspx?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.


Top
 Profile  
Reply with quote  
 Post subject: Re: DeleteFile("Name*.*)
PostPosted: Wed Jun 03, 2020 12:53 pm 
Offline
Addict
Addict

Joined: Sat Feb 08, 2014 3:26 pm
Posts: 903
StarWarsFan wrote:
Is there really no easy way to simply delete all files starting with "Name*" in the current directory?

Code:
DeleteDirectory(".", "Name*.*")

_________________
(English is not my native language, I use an online translator.)


Top
 Profile  
Reply with quote  
 Post subject: Re: DeleteFile("Name*.*)
PostPosted: Wed Jun 03, 2020 1:13 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Jul 23, 2011 1:13 am
Posts: 297
Location: Germany
Now that is counter-intuitive :D


Top
 Profile  
Reply with quote  
 Post subject: Re: DeleteFile("Name*.*)
PostPosted: Wed Jun 03, 2020 1:23 pm 
Offline
Addict
Addict

Joined: Sat Feb 08, 2014 3:26 pm
Posts: 903
Code:
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)

_________________
(English is not my native language, I use an online translator.)


Top
 Profile  
Reply with quote  
 Post subject: Re: DeleteFile("Name*.*)
PostPosted: Wed Jun 03, 2020 1:50 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Apr 25, 2003 11:08 pm
Posts: 456
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: DeleteFile("Name*.*)
PostPosted: Thu Jun 04, 2020 12:45 am 
Offline
Addict
Addict

Joined: Thu Apr 18, 2019 8:17 am
Posts: 894
Derren wrote:
Now that is counter-intuitive :D

Macros to the rescue!

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

DeleteWithWildcards(".","Name*.*")


Top
 Profile  
Reply with quote  
 Post subject: Re: DeleteFile("Name*.*)
PostPosted: Thu Jun 04, 2020 10:56 am 
Offline
User
User

Joined: Sat Mar 14, 2015 11:53 am
Posts: 74
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."

_________________
There's usually a lot of "try this, do that" but ONLY an example source that one can test for themself and get an immediate result brings people forward.


Top
 Profile  
Reply with quote  
 Post subject: Re: DeleteFile("Name*.*)
PostPosted: Thu Jun 04, 2020 11:28 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Jul 23, 2011 1:13 am
Posts: 297
Location: Germany
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()"


Top
 Profile  
Reply with quote  
 Post subject: Re: DeleteFile("Name*.*)
PostPosted: Thu Jun 04, 2020 11:44 am 
Offline
User
User

Joined: Sat Mar 14, 2015 11:53 am
Posts: 74
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's usually a lot of "try this, do that" but ONLY an example source that one can test for themself and get an immediate result brings people forward.


Top
 Profile  
Reply with quote  
 Post subject: Re: [SOLVED] DeleteFile("Name*.*)
PostPosted: Thu Jun 04, 2020 1:48 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Jun 22, 2003 7:43 pm
Posts: 551
Location: Germany, Saarbrücken
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:
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 22 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye