Name of variable (file exists)

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

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

If those files are inside subfolder “data/” you can use dataFile() for it:
File filePath = dataFile("positions" + index + EXTENSION);

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

File positions = dataFile("positions");

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

I believe you should check out this listPaths() example:

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:

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
//

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:

// 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();
  }

:)