# Name of variable (file exists)

**URL:** <https://discourse.processing.org/t/name-of-variable-file-exists/38527>\
**Category:** Beginners\
**Created:** [August 27, 2022, 11:45pm UTC](https://discourse.processing.org/t/name-of-variable-file-exists/38527 "2022-08-27T23:45:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jensemand](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jensemand](https://discourse.processing.org/u/jensemand)\
**Post date:** [August 27, 2022, 11:45pm UTC](https://discourse.processing.org/t/name-of-variable-file-exists/38527/1 "2022-08-27T23:45:33Z")

</div>

Hi, I need to check if i have a specific file named positions1, positions2, positions3,…  
I thought I could combine a string with an integer and then write something like

```auto
File nameofString+number= new File(sketchPath())

```

however this doesn’t seem to work.  
If you have any good ideas how I can solve this problem I would really appreciate if you could help.  
Thank you for taking the to time answer

---

<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:** [August 28, 2022, 12:14am UTC](https://discourse.processing.org/t/name-of-variable-file-exists/38527/2 "2022-08-28T00:14:00Z")

</div>

> [@jensemand](#):
>
> Hi, I need to check if I have specific files named “positions1”, “positions2”, “positions3”, …

If those files are inside subfolder “data/” you can use [**dataFile()**](http://processing.github.io/processing-javadocs/core/processing/core/PApplet.html#dataFile-java.lang.String-) for it:  
`File filePath = dataFile("positions" + index + EXTENSION);`

---

<div class="post-metadata">

**Author:** ![jensemand](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jensemand](https://discourse.processing.org/u/jensemand)\
**Post date:** [August 28, 2022, 12:28am UTC](https://discourse.processing.org/t/name-of-variable-file-exists/38527/3 "2022-08-28T00:28:18Z")

</div>

Sadly it doesn’t seem to work for me. I don’t know if I am doing anything wrong but when I write

```auto
File positions = dataFile("positions");

```

and check if a file named “positions” exists by using .exists() it does not work.

---

<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:** [August 28, 2022, 1:09am UTC](https://discourse.processing.org/t/name-of-variable-file-exists/38527/4 "2022-08-28T01:09:02Z")

</div>

I believe you should check out this [**listPaths()**](http://processing.github.io/processing-javadocs/core/processing/core/PApplet.html#listPaths-java.lang.String-java.lang.String...-) example:

> [@How do I slowly layer photos to appear on top of eachother?](https://discourse.processing.org/t/how-do-i-slowly-layer-photos-to-appear-on-top-of-eachother/36698/5):
>
> This example loads all images inside subfolder “data/” and assign them to a PImage[] array: framed_picture /\*\* \* Load All Images (v1.0.0) \* GoToLoop (2022/May/04) \* \* https://Discourse.Processing.org/t/ \* how-do-i-slowly-layer-photos-to-appear-on-top-of-eachother/36698/5 \*/ static final String PICS\_EXTS = "extensions=,png,jpg,jpeg,gif,tif,tiff,tga,bmp"; static final float FPS = .25; PImage[] images; void setup() { size(600, 500); frameRate(FPS); imageMode(CENTER); images =…

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [August 28, 2022, 1:04pm UTC](https://discourse.processing.org/t/name-of-variable-file-exists/38527/5 "2022-08-28T13:04:56Z")

</div>

> [@jensemand](#):
>
> check if i have a specific file named positions1, positions2, positions3,…

Under windows normally all files have an extension like “.txt”.

It might be the case that your windows doesn’t show the extensions which would be bad. You can change this in the _Windows explorer_.

**Sketch**

Then you want to check whether a certain file is present.

Here you just get a file list of the files present:

```auto
StringList inventory;  

//---------------------------------------------------------------------------
// processing core functions 

void setup() {
  size(880, 800);
  background(111); // gray
}

void draw() {
  background(111); // gray
  updateList(); 
  showList();
}

//---------------------------------------------------------------------------
// Other functions 

void updateList() {
  File file = new File(dataPath(""));
  String[] fileList = file.list();

  if (fileList==null) {
    println("Folder is empty.");
  } else {
    // success
    inventory = new StringList();
    for (String s1 : fileList) { 
      inventory.append(s1);
    }

    inventory.sort();
  }//else
}//func 

void showList() {
  if (inventory==null) 
    return; // leave 

  int i=0; 
  for (String s1 : inventory) { 
    text(s1, 33, 44+i*22);
    i++;
  }//for
  //
}//func
//

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 28, 2022, 6:01pm UTC](https://discourse.processing.org/t/name-of-variable-file-exists/38527/6 "2022-08-28T18:01:21Z")

</div>

> [@jensemand](#):
>
> check if a file named “positions” exists by using .exists() it does not work.

Hello @jensemand,

Create a \data folder in your sketch with some various file types.

This example will list the files and then do a check:

```auto
// Files and directories
// v1.0.0
// GLV 2022-08-27

void setup() 
  {
  size(200, 200);
  println(sketchPath());
  println(dataPath(""));
  
  // List file names in \data folder
  File f = new File(dataPath(""));
  println(f);
  printArray(f.list());
  
  // Check if a file exists  
  f = dataFile("New Text Document.txt"); // Checks for this file name
  if (f.exists())
    println("exists");
  else
    println("! exists");
    
  f = dataFile("positions"); // Checks for this file name
  if (f.exists())
    println("exists");
  else
    println("! exists");
  
  noLoop();
  }

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/3/6/36b30516f5f091ed250dba21bca50d3e9ad17679.png)

`:)`
