How to make the selectFolder() function open using a default path

I’m trying to figure out how to make the selectFolder() function open using a select path eg instead of opening up with the documents folder selected open with the sketch paths one?

1 Like

This may help

Tools for you: Search file name and others



I was able to use the File datatype for file using this syntax for selectFolder():

selectFolder(prompt, callback, file)

And set the default path to c:\test:



1 Like


// This is the Filetype we're gonna look for
String extensionToLookFor = ".jpg";

// ArrayList of loadedFiles
ArrayList<File> loadedFiles;
int fileToLoad = 0;
PImage loadedImage;

void setup()

	//selectFolder(String, String);

void draw()
	if(loadedImage == null || loadedFiles == null || loadedFiles.size() <= 0) 


	image(loadedImage, 0, 0, width, height);

void keyPressed()
		case ' ':
		fileToLoad ++;
		if(fileToLoad >= loadedFiles.size())
			fileToLoad = 0;
		String imgPath = loadedFiles.get(fileToLoad).getAbsolutePath();
		println("Load: " + imgPath);
		loadedImage = loadImage(imgPath);

		case 'l':

void selectFolder()
	// Opens Window to select a folder or file from
	// then Calls a Method with the selected File as Parameter
	selectFolder("Select a Folder", "loadFiles");

void loadFiles(File selection)
	// if nothing was selected
	if(selection == null)
		println("User hit cancel");
		// open the Window again
		// and skip all code below by returning

	// this is the absolutePath (just for Debugging)
	String selectedPath = selection.getAbsolutePath();
	println("Path selected: " + selectedPath); 

	// now get all Files with the right Extension
	loadedFiles = getFilesFromFolder(selection, extensionToLookFor);
	println("Found Files: " + loadedFiles.size());
	fileToLoad = 0;
	loadedImage = loadImage(loadedFiles.get(fileToLoad).getAbsolutePath());
	//loadedImages = loadImagesFromFiles(loadedFiles);

// Method that returns all Files with a specific Extension in a Folder
ArrayList<File> getFilesFromFolder(File selectedFolder, String extension)
	// all files
	File[] allFiles = selectedFolder.listFiles();
	// only Files with the extension:
	ArrayList<File> files = new ArrayList<File>();
	// loop all Files
	for (int i = 0; i < allFiles.length; i++) 
		File f = allFiles[i];
		// skip files without the right extension
		if( !hasExtension(f, extension) ) continue;
		// if not skipped -> add it to arraylist
	// return found files
	return files;

// Helper Method to check for a files Extension
boolean hasExtension(File file, String extension)
	// the absolute path to the file
	String filePath = file.getAbsolutePath();
	// make the Path and the Extension to LowerCase (because ".JPG" == ".jpg" !!!)

	// get the last characters of the path as they should contain the extension of the file
	// like for example: /someFolder/someFile.jpg
	// last character minus the length of the extension = index of first extension char
	int extensionStart = filePath.length() - extension.length();
	// index of last char in filePath
	int extensionEnd = filePath.length();
	// just the last chars of the filePath:
	String filePathExtension = filePath.substring( extensionStart, extensionEnd );
	// if the filePathExtension = is the extension we're looking for
		// end here with true
		println("Right Extension: " + extension);
		return true;
		println("Wrong Extension: " + filePathExtension);

	// file has not the extension we looked for
	return false;

// Unused Methods:

// Method for Loading all Files in PImage ArrayList
// Beware: might be very memory consuming
ArrayList<PImage> loadImagesFromFiles(ArrayList<File> files)
	ArrayList<PImage> imgs = new ArrayList<PImage>();

	for(File f : files)
		PImage img;
		String imgPath = f.getAbsolutePath();
		println("Load Image: " + imgPath);
		img = loadImage(imgPath);

	return imgs;

// to be checked out:

// have a look in the data folder folder = new""));

// this is the filter (returns true if file's extension is .jpg) jpgFilter = new {
 boolean accept(File dir, String name) {
   return name.toLowerCase().endsWith(".jpg");

// list the files in the data folder
String[] filenames = folder.list(jpgFilter);


Yeah, well, under Win it’s not reliable.

I think there’s a bug

It stores the last folder but when you open another folder inbetween it won’t return to the folder from the actual command

So you first think it works until you open another folder

I did not observe any issues.

If there is a bug can you open an issue on Github please?