0 votes
by ProFrame (230 points)
Hello,

I need a function to get a list of file-names in a known directory.

Like find the file-names: "*.csv", give as result a list of filenames, with extension ".csv" in the

known directory.

Have someone an example or could help me on the way to develope this function?

with regards,

Hugo Overbeek

1 Answer

0 votes
by Daniel Lee (400 points)

You can do this by capturing the output of "ls -1 *.csv".  The -1 option tells ls to list each file on a separate line. 

The >pShell word captures the output of a shell command and sends it to xtype.  The xtype word then splits the output on each line feed character and prints the output line by line.

First we'll create a word to process each filename:

: process-file  
  s" Next file is >> " type  // display message
  type   // now show the filename
  ;

now we'll update the xtype word.  Change line 86 in shell.fth from:

if type else 2drop endif            \ type left string

to the following:

if process-file else 2drop endif    \ do something for each file

The >pShell word will continue to use the old copy of xtype until you redefine it, so just copy and paste the code for >pShell into the terminal again.

cr z" ls -1 Doc/*pdf" >pShell
Next file is >> Doc/FPgoldberg.pdf
Next file is >> Doc/ProgramForth.pdf
Next file is >> Doc/VfxLin64.pdf

 

...