files in a specific directory with a specific extension

Just starting out? Need help? Post your questions and find answers here.
mikeschn
User
User
Posts: 11
Joined: Sat Jun 30, 2012 12:22 am

files in a specific directory with a specific extension

Post by mikeschn »

I've looked and looked, and I can not see how to do this...

I need a list of files in a specific directory with a specific extension. And I need to be able to fill and array with the file names.

for example:

I need a list of all the files in c:\myprogram with the file extension *.m_p

and I need that list put into an array file_names$(500)

Can someone point me in the right direction?

Thanks,

Mike...
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: files in a specific directory with a specific extension

Post by Bisonte »

have a look to the parameter "Pattern$" of ExamineDirectory()
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
em_uk
Enthusiast
Enthusiast
Posts: 366
Joined: Sun Aug 08, 2010 3:32 pm
Location: Manchester UK

Re: files in a specific directory with a specific extension

Post by em_uk »

You mean like this, using a structured list:

Code: Select all

Structure Files
  ; using a structured list allows us to store more than one bit of information per item
  ; Right now I only want to store a name and an item number
  name.s
  number.i
EndStructure 

NewList file.Files()

Count=0

Directory$ = "c:\myprog"   ; Change this to your path

Debug Directory$
With File() ; Start using our list

If ExamineDirectory(0, Directory$, "*.m_p")  ; change the extension
    While NextDirectoryEntry(0)
      If DirectoryEntryType(0) = #PB_DirectoryEntry_File
        Filename$ = DirectoryEntryName(0)
        AddElement(file())  ; and an element
        Count+1
        \name=Filename$  ; adds a name
        \number=count    ; adds the count number
      EndIf 
    Wend
    FinishDirectory(0)
  EndIf
EndWith

ResetList(File()) ; Go to the beginning of our list

Debug "Found "+Str(Count)+" file(s)"

ForEach File()
  Debug Str(file()\number)+Space(10)+file()\name
  Delay(50)  
Next

Last edited by em_uk on Mon Jun 24, 2013 10:32 pm, edited 1 time in total.
----

R Tape loading error, 0:1
User avatar
em_uk
Enthusiast
Enthusiast
Posts: 366
Joined: Sun Aug 08, 2010 3:32 pm
Location: Manchester UK

Re: files in a specific directory with a specific extension

Post by em_uk »

Or if answering your request correctly using an array :

Code: Select all

 
Dim File_names$(500)

Count=0

Directory$ = "c:\myprog"   ; Change this to your path

Debug Directory$

If ExamineDirectory(0, Directory$, "*.m_p")  ; change the extension
  While NextDirectoryEntry(0)
    If DirectoryEntryType(0) = #PB_DirectoryEntry_File
      Count+1
      File_names$(Count)=Filename$
    EndIf 
    Debug DirectoryEntryName(0) + Type$ + "- Size in byte: " + Str(DirectoryEntrySize(0))
  Wend
  FinishDirectory(0)
EndIf

Debug "Found "+Str(Count)+" file(s)"

For files=1 To Count
  Debug Str(files)+Space(10)+File_names$(files)
Next

----

R Tape loading error, 0:1
Post Reply