# The count of files in the sketch folder?

**URL:** <https://discourse.processing.org/t/the-count-of-files-in-the-sketch-folder/17931>\
**Category:** Beginners\
**Created:** [February 19, 2020, 12:44pm UTC](https://discourse.processing.org/t/the-count-of-files-in-the-sketch-folder/17931 "2020-02-19T12:44:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamjustcurious](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/iamjustcurious/32/8016_2.png) [@iamjustcurious](https://discourse.processing.org/u/iamjustcurious)\
**Post date:** [February 19, 2020, 12:44pm UTC](https://discourse.processing.org/t/the-count-of-files-in-the-sketch-folder/17931/1 "2020-02-19T12:44:08Z")

</div>

How can I get the number of files in the sketch folder?

**Second Question**  
Is it possible to get the number of files in the desktop?

Thanks.

---

<div class="post-metadata">

**Author:** ![anon68884268](https://avatars.discourse-cdn.com/v4/letter/a/59ef9b/32.png) [@anon68884268](https://discourse.processing.org/u/anon68884268)\
**Post date:** [February 19, 2020, 1:43pm UTC](https://discourse.processing.org/t/the-count-of-files-in-the-sketch-folder/17931/2 "2020-02-19T13:43:05Z")

</div>

How about this hacky solution

```auto
String[] listFileNames(String dir) {
  File file = new File(dir);
  if (file.isDirectory()) {
    String names[] = file.list();
    return names;
  } else {
    // If it's not a directory
    return null;
  }
}

void getNumberOfFiles(String dir) {
  return listFileNames(dir).length
}

```

From somewhere of the examples

---

<div class="post-metadata">

**Author:** ![iamjustcurious](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/iamjustcurious/32/8016_2.png) [@iamjustcurious](https://discourse.processing.org/u/iamjustcurious)\
**Post date:** [February 19, 2020, 3:58pm UTC](https://discourse.processing.org/t/the-count-of-files-in-the-sketch-folder/17931/3 "2020-02-19T15:58:32Z")

</div>

Sorry mate I don’t understand the solution. I haven’t found anything about this in reference webpage of processing;  
“**File file = new File(dir);**”  
Can you send the whole example code or something?

---

<div class="post-metadata">

**Author:** ![anon68884268](https://avatars.discourse-cdn.com/v4/letter/a/59ef9b/32.png) [@anon68884268](https://discourse.processing.org/u/anon68884268)\
**Post date:** [February 19, 2020, 5:44pm UTC](https://discourse.processing.org/t/the-count-of-files-in-the-sketch-folder/17931/4 "2020-02-19T17:44:54Z")

</div>

```auto
File file = new File(dir);

```

is from Java. [Here you find the Java reference for the File object.](https://docs.oracle.com/javase/7/docs/api/java/io/File.html) Processing is built on top of Java, so (almost) everything that works in Java can be done in Processing.

> **Example Code**
>
> ```auto
> /**
> * Listing files in directories and subdirectories
> * by Daniel Shiffman.  
> * 
> * This example has three functions:<br />
> * 1) List the names of files in a directory<br />
> * 2) List the names along with metadata (size, lastModified)<br /> 
> * of files in a directory<br />
> * 3) List the names along with metadata (size, lastModified)<br />
> * of files in a directory and all subdirectories (using recursion) 
> */
> 
> import java.util.Date;
> 
> void setup() {
> 
> // Using just the path of this sketch to demonstrate,
> // but you can list any directory you like.
> String path = sketchPath();
> 
> println("Listing all filenames in a directory: ");
> String[] filenames = listFileNames(path);
> printArray(filenames);
> 
> println("\nListing info about all files in a directory: ");
> File[] files = listFiles(path);
> for (int i = 0; i < files.length; i++) {
> File f = files[i];    
> println("Name: " + f.getName());
> println("Is directory: " + f.isDirectory());
> println("Size: " + f.length());
> String lastModified = new Date(f.lastModified()).toString();
> println("Last Modified: " + lastModified);
> println("-----------------------");
> }
> 
> println("\nListing info about all files in a directory and all subdirectories: ");
> ArrayList<File> allFiles = listFilesRecursive(path);
> 
> for (File f : allFiles) {
> println("Name: " + f.getName());
> println("Full path: " + f.getAbsolutePath());
> println("Is directory: " + f.isDirectory());
> println("Size: " + f.length());
> String lastModified = new Date(f.lastModified()).toString();
> println("Last Modified: " + lastModified);
> println("-----------------------");
> }
> 
> noLoop();
> }
> 
> // Nothing is drawn in this program and the draw() doesn't loop because
> // of the noLoop() in setup()
> void draw() {
> }
> 
> // This function returns all the files in a directory as an array of Strings  
> String[] listFileNames(String dir) {
> File file = new File(dir);
> if (file.isDirectory()) {
> String names[] = file.list();
> return names;
> } else {
> // If it's not a directory
> return null;
> }
> }
> 
> // This function returns all the files in a directory as an array of File objects
> // This is useful if you want more info about the file
> File[] listFiles(String dir) {
> File file = new File(dir);
> if (file.isDirectory()) {
> File[] files = file.listFiles();
> return files;
> } else {
> // If it's not a directory
> return null;
> }
> }
> 
> // Function to get a list of all files in a directory and all subdirectories
> ArrayList<File> listFilesRecursive(String dir) {
> ArrayList<File> fileList = new ArrayList<File>(); 
> recurseDir(fileList, dir);
> return fileList;
> }
> 
> // Recursive function to traverse subdirectories
> void recurseDir(ArrayList<File> a, String dir) {
> File file = new File(dir);
> if (file.isDirectory()) {
> // If you want to include directories in the list
> a.add(file);  
> File[] subfiles = file.listFiles();
> for (int i = 0; i < subfiles.length; i++) {
> // Call this function on all files in this directory
> recurseDir(a, subfiles[i].getAbsolutePath());
> }
> } else {
> a.add(file);
> }
> }
> 
> ```

The example was located in Examples =\> Topics =\> File IO =\> Directory List (On Java Mode, on Python mode simply use the `os` module and `os.listdir()`.)

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 20, 2020, 9:30am UTC](https://discourse.processing.org/t/the-count-of-files-in-the-sketch-folder/17931/5 "2020-02-20T09:30:58Z")

</div>

> [@File Listing Order](https://discourse.processing.org/t/file-listing-order/7148/41):
>
> [Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html#sort(java.lang.Object[])](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html#sort(java.lang.Object%5B%5D)) static final String PICS\_EXTS = "extensions=,png,jpg,jpeg,gif,tif,tiff,tga,bmp,wbmp"; static final float FPS = .25; PImage[] images; void setup() { size(1200, 600); frameRate(FPS); imageMode(CENTER); final File dir = dataFile(""); println(dir); String[] imagePaths = {}; int imagesFound = 0; if (dir.isDirectory()) { imagePaths = listPaths(dir.getPath(), "files", "recursi…
