First time here? Check out the FAQ!
x

Collect text files into Collection? fileNamesInSameFolder ???

0 votes
594 views
I'm working on creating a batch script that takes a folder of text files (single column) and exports out as audio (one row of data per sample).  I've got a working script for a single text file to an audio file. Now I want to expand on this and convert a folder of text files.

I can see how a folder of samples are collected using

filesInSameFolder := fileName sampleFileNamesInSameFolder.

"Collect the filenames into an array."
filenameArray := 1 to: filesInSameFolder size collect: [:i | i].

Is there a way to collect an array for .txt files?  I've tried sampleFileNamesInSameFolder and fileNamesInSameFolder with no luck.

Here's the chunk of code (only included what I have before my first error related to fileNamesInSameFolder..

| f file fileName lineValArray samplesFilename samplesFile writeBuffer maxVal filenameArray filesInSameFolder |

maxVal := 0. "init maxVal"

"Type your text file name inside single quotes. This will create a Kyma auto search for the file."

fileName := '0.txt'.

file := Preferences

locateFileNamed: fileName

                 ofType: HostDriverInterface textFileType

locateMessage: ' locate the text file named '.

file == nil ifTrue: [^ self abortForKyma].

 

filesInSameFolder := file fileNamesInSameFolder.

 

"for multiple files in one batch process we will need this."

"Collect the filenames into an array."

filenameArray := 1 to: filesInSameFolder size collect: [:i | i].

 

filenameArray do: [:i | | samplePair |

f := (filesInSameFolder at: i) asFilename textFileStream.

 

         "read through file line by line"

         "collect maximum for normalization in first iteration"

        [f atEnd] whileFalse: [

        lineValArray := f nextParameters. "index 1 for 0th element"

                 maxVal := maxVal vmax: ((lineValArray at: 1) abs).

         ].

         f reset. "reset pointer to start of the text file"

         self debugWithLabel: 'maxVal' value: (maxVal).

].
asked Feb 4, 2019 in Capytalk & Smalltalk by jonbellona (Adept) (1,360 points)

1 Answer

0 votes

This line doesn't look right to me:

filenameArray := 1 to: filesInSameFolder size collect: [:i | i].

That's going to make an array of integers from 1 to the number of files in the folder. 

This should work:

filenameArray := filesInSameFolder asArray.

 


 

I just tried evaluating these expressions with the following results:

 

| filesInSameFolder filenameArray |


filesInSameFolder := 'count.aif' sampleFileNamesInSameFolder.

"Collect the filenames into an array."
filenameArray := 1 to: filesInSameFolder size collect: [:i | i].

filenameArray. 

(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25)

 

and


| filesInSameFolder filenameArray |


filesInSameFolder := 'count.aif' sampleFileNamesInSameFolder.

"Collect the filenames into an array."
filenameArray := filesInSameFolder asArray.

filenameArray. 


 ('Kyma 7 Folder:Samples:Speech:Alien threat.aif' 'Kyma 7 Folder:Samples:Speech:Alien threat2.aif' ...

 

So yes, "asArray" should work. The sampleFileNamesInSameFolder message returns an OrderedCollection and you can convert those to arrays using the asArray message. 


 

The next thing is the "filesNamesInSameDirectory" message needs to be sent to a Filename object, not a string. In your code the variable "file" is a String. 

Try converting it into a Filename after the Preferences expression:

file := file asFilename.

 

and then this should work (note it's ...Directory not ...Folder):

filesInSameFolder := file fileNamesInSameDirectory.

answered Feb 4, 2019 by alan-jackson (Virtuoso) (15,840 points)
edited Feb 4, 2019 by alan-jackson
Hi Jon & Alan, in case it's helpful you can send the following messages directly to a FileName (a string) without creating a file object first:

'Kyma/Texts/b1.txt' fileNamesOfSameTypeInSameFolder

or

'Kyma/Texts/b1.txt' fileNamesInSameFolderWithSameExtension
Awesome Alan and SSC. All three of those will work
fileNamesInSameDirectory
fileNamesOfSameTypeInSameFolder
fileNamesInSameFolderWithSameExtension

I was trying to use samples as my working example and syntax of
sampleFileNamesInSameFolder got me.  
SSC, by any chance, will any of those three messages above also work for .csv or .tsv ?

Thanks again!
I think that .tsv files are text files; you might be able to just change the extension to .txt (as long as it is plain text and doesn't have any special characters in it).
...