Help with launch()

Looking into how to trigger a task, script, or executable in Processing.

Basically I want Processing to run a script that deletes all files if there are any files in a folder during startup.

I found this script works great:

ForFiles /p “C:\Users…” /s /d 0 /c “cmd /c del @file

However, it needs to be launched with PowerShell, This doesn’t work with launch() so I found a way to turn it into a .exe.

If I double click the .exe it runs the script perfectly. However, when I use the launch() command it never runs. In fact, the .exe only runs after I close the Processing sketch.

Why is that? Why won’t it run when the launch() is called?

Here is the code for the sketch.

void setup() {
size(200, 200);
}

void draw() {
// draw() must be present for mousePressed() to work
background(0);
rect(10, 10, mouseX, mouseY);
}

void mousePressed() {
launch(“C:/Users/DeleteAllFiles in BookExport.exe”);
println(“Trying to delete”);
}

I found that in the properties tab of the .exe that you can select “Run as Administrator”. This has that full screen yes/no pop-up asking you if it you want to run. At least it won’t freeze but it is still not automated…

Thanks! So it looks like you can have Processing do the deleting of the files? Awesome.

I see this is the result of the link. below. Please forgive me but I am not a power user and do not know how to translate the words on this page into meaningful code in Processing.

Do I just paste in delete() somewhere?

#### delete

public boolean delete()

Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted.

Note that the [ `Files` ](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Files.html) class defines the [ `delete` ](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Files.html#delete(java.nio.file.Path)) method to throw an [ `IOException` ](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/IOException.html) when a file cannot be deleted. This is useful for error reporting and to diagnose why a file cannot be deleted.

Returns:

`true`  if and only if the file or directory is successfully deleted;  `false`  otherwise

Throws:

`SecurityException`  - If a security manager exists and its [ `SecurityManager.checkDelete(java.lang.String)` ](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/SecurityManager.html#checkDelete(java.lang.String)) method denies delete access to the file
void setup() {
  final File files[], dir = dataFile(""); // get folder path as variable dir
  println(dir, ENTER);

  files = dir.listFiles(); // get everything from folder as array variable files
  printArray(files);
  println();

  // Delete everything from folder by iterating over files[]:
  if (files != null)  for (final File f : files)  print(f.delete(), TAB);
  exit();
}

Thank you GoToLoop that was very kind of you to share. I will do my best to try and implement this.

Just in case for the future if my script in Windows 10 is not able to be replicated in Processing how do I achieve the launch() without the program hanging as described in the first post?

I am sure there will be a time for you, I, or others, where they too will want to launch() a simple script in Windows 10 and may encounter this problem.

Thank you!

Once again this code worked wonderfully. Thank you GoToLoop!

Here is what I used in the code. Pretty much the same as above:

“textImagePath” is a String variable I used to hold the long path of the specific folder I am using. I commented out “exit()” so that the program would keep running.

  // Try to delete all files in folder
  final File files[], dir = dataFile(textImagePath); // get folder path as variable dir
  println(dir, ENTER);

  files = dir.listFiles(); // get everything from folder as array variable files
  printArray(files);
  println();
  
  if (files != null)  for (final File f : files)  print(f.delete(), TAB);
  println();
  //exit();

Diggin back into the problem with the script as a .exe I am unsure if it is a problem genrally with any .exe file as I am able to launch a simple application without the hanging part.

Maybe there is an issue with the way to .exe is transl;ated from txt file, to script, to .exe? I wish there was a more straightforward way to run a Windows 10 script in Processing.

Once I figure it out I will report back!

void setup() {
  size(200, 200);
  background(0);
}

void draw() { 
  rect(mouseX, mouseY, 20, 20);
}

void mousePressed() {
  background(0);
  println("Opening Process_4");
  launch("C:/Program Files (x86)/CamShop/CamShop.exe");
}