Author Topic: Equivalent of PB DIR$ function  (Read 157 times)

0 Members and 1 Guest are viewing this topic.

chrisc

  • Guest
Equivalent of PB DIR$ function
« on: April 26, 2018, 06:44:00 AM »
In PB we have the DIR$ function which can return a list of filenames matching a given mask

for example

      DIR$( "C:\temp\*.exe")   

will return all exe files inside  C:\Temp folder

http://www.manmrk.net/tutorials/basic/PowerBASIC/pbcc/dir_function.htm

what is the equivalent of this function in O2 ?

i need to use it to enumerate or list out the contents of a folder



Josť Roca

  • Jr. Member
  • **
  • Posts: 50
Re: Equivalent of PB DIR$ function
« Reply #1 on: April 26, 2018, 07:00:22 AM »
Look at FirndFirstFile, FindNextFile and  FindClose.

https://msdn.microsoft.com/en-us/library/windows/desktop/aa364418(v=vs.85).aspx

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3691
    • Oxygen Basic
Re: Equivalent of PB DIR$ function
« Reply #2 on: April 26, 2018, 07:30:34 AM »
We have a ready-made function returning a file or folder list in one string

Code: [Select]
/*
  typedef struct _WIN32_FIND_DATA {
  DWORD    dwFileAttributes;
  FILETIME ftCreationTime;
  FILETIME ftLastAccessTime;
  FILETIME ftLastWriteTime;
  DWORD    nFileSizeHigh;
  DWORD    nFileSizeLow;
  DWORD    dwReserved0;
  DWORD    dwReserved1;
  TCHAR    cFileName[MAX_PATH];
  TCHAR    cAlternateFileName[14];
  } WIN32_FIND_DATA, *PWIN32_FIND_DATA, *LPWIN32_FIND_DATA;
*/

'https://msdn.microsoft.com/en-us/library/windows/desktop/gg258117(v=vs.85).aspx
'selected File Attribute Constants
% FILE_ATTRIBUTE_ARCHIVE          0x20
% FILE_ATTRIBUTE_DEVICE           0x40
% FILE_ATTRIBUTE_DIRECTORY        0x10
% FILE_ATTRIBUTE_HIDDEN           0x2
% FILE_ATTRIBUTE_NORMAL           0x80
% FILE_ATTRIBUTE_READONLY         0x1
% FILE_ATTRIBUTE_SYSTEM           0x4
% FILE_ATTRIBUTE_TEMPORARY        0x100


  function GetFileList(string filter, sys*count,optional dirq) as string
  ======================================================================
  '
' sys xyz
  '
  macro appendList(s,w,i,  ls,lw) '(string *s,w, sys *i)
    sys ls=len s
    sys lw=len w
    if i+lw>ls
      s+=nuls 1024 '+lw
    end if
    mid s,i+1,w
    i+=lw
  end macro
  '
  WIN32_FIND_DATA f
  sys    a,e,h,fi
  string flist
  string cr=chr(13,10)
  '
  h=FindFirstFile filter, @f
  count=0
  if h then
    do
      a=0
      if f.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY
        if dirq then a=1
        if asc(f.cFileName)=46 then  a=0 '.' ' ..'
      else 'NOT A DIRECTORY
        if not dirq then a=1
      end if
      if a then
        count++
        appendlist flist, f.cFileName+cr, fi
      end if
      e=FindNextFile h, @f
      if e=0 then
        e=GetLastError() 'should be 18 when no more files
        exit do
      end if
    end do
    FindClose h
  end if
  return left (flist,fi)
  '
  end function


int c
print getFileList "*.txt",c
print "list count " c

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3691
    • Oxygen Basic
Re: Equivalent of PB DIR$ function
« Reply #3 on: April 26, 2018, 09:00:11 AM »
I would change the prototype, since an int file counter is more appropriate.

Code: [Select]
  function GetFileList(string filter, int*count,optional dirq) as string