Getting Files recusively - Windows

I’ve been trying with PathName, File, Strings, Arrays, and routines.

I’m just trying to get all the files recursively, from within multiple folders, select the ones with a certain file extension, then do something to them,.

I’ve been through all the help files.

Doing something like this only returns the last folder in the list:

(
var endResult;
t = "C:/Users/melas/AppData/Local/SuperCollider/test/*";
t.pathMatch.do({|it,i| endResult = (it ++ "*").pathMatch.postln;});
endResult;
)

I was hoping there would be a way to just use wildcards, like

"C:/path/*/*.txt" 

to only get the .txt files in the folders in a folder, but that doesn’t work.

I’m in Windows…

you can use PathName method filesDo, e.g.:

(
a = PathName.new(PathName.tmp);
a.filesDo({arg file; file.postln;});
)

Best,
Paul

I’m trying to collect all the files in multuple folders recursively. Like C:/folder/folder1/* and C:/folder/folder2/* etc.

As in, I have a Samples/ folder, and it has a bunch of folders in it i.e. kicks/ hats/ etc. and each has files in it. I want to get all the files in those folders in one list, and iterate over them.

Oh I see, you just can’t have a wildcard with .filesDo

This doesn’t work:

(
(
a = PathName.new("C:/Users/melas/AppData/Local/SuperCollider/test/");
a.filesDo({arg file; file.postln;});
)
)

But without the * does

Thanks!

Do you want this, probably?

(
~pathBase = "C:/folder" // ~pathBase = "sounds".resolveRelative;
~subfoldersOfpathBase = PathName(~pathBase).folders;
~allSoundFiles = ~subfoldersOfpathBase.collect{ |aPathNameInstance| SoundFile.collect(aPathNameInstance.fullPath +/+ "*") }.flat;
~path_allSoundFiles = ~allSoundFiles.collect{ |aSoundFileInstance| aSoundFileInstance.path};
Post <<< ~path_allSoundFiles
)
1 Like

For what it’s worth, PathName("").filesDo{ } is the only method in SC that will recursively iterate over every file in every subfolder for a path’s location.

Without it, solutions are apt to become complex or less reusable.

a = List[];

PathName
(
	Platform.userAppSupportDir +/+ \test
)
.filesDo
{
	|pathname|
	
	if(
		pathname.extension == "scd"
	){
		a add: pathname.fullPath
	}
};

a.printAll.size.postln