We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › external file control
Page Index Toggle Pages: 1
external file control? (Read 1000 times)
external file control?
Oct 22nd, 2009, 5:36pm
 
this seems like a pretty straightforward question; I just can't find anything about it on the site.

Is there any way for processing to delete files or folders?
Re: external file control?
Reply #1 - Oct 22nd, 2009, 7:07pm
 
I was messing around with the java exec command a while ago...
You can probably modify this to do what you want:

Code:
import java.io.IOException;
import java.io.*;

void setup()
{
 background(0);
 size(500,500);
 textFont(createFont("FFScala", 12));
 try
 {
   String cm = "cmd.exe /C dir /W C:\\mp3";

   Process p = Runtime.getRuntime().exec(cm);
   InputStream in = p.getInputStream();
   int ch;
   StringBuffer sb = new StringBuffer(512);

   while ( ( ch = in.read() ) != -1 )
   {
     sb.append((char) ch);
   }

   println(sb.toString());
   String splitString[] = split(sb.toString(), "\n");
   splitString = trim(splitString);
   int numStrings = 0;
   for(int i = 0; i < splitString.length; i++)
   {
     if(splitString[i].indexOf(".mp3")>=0)
     {
       numStrings++;
     }
   }
   String[] pngFilesFound = new String[numStrings];
   numStrings=0;
   for(int i = 0; i < splitString.length; i++)
   {
     if(splitString[i].indexOf(".mp3")>=0)
     {
       pngFilesFound[numStrings]=splitString[i];
       text(pngFilesFound[numStrings], 10, numStrings*15+50);
       numStrings++;
     }
   }
   saveStrings("file.txt",splitString);
   saveStrings("mp3s.txt",pngFilesFound);
   println("Number of processors: " + Runtime.getRuntime().availableProcessors());
   println("free memory: " + (Runtime.getRuntime().freeMemory()/1024)/1024 + "MB");
   println("max memory: " + (Runtime.getRuntime().maxMemory()/1024)/1024 + "MB");
   println("total memory: " + (Runtime.getRuntime().totalMemory()/1024)/1024 + "MB");
 }
 catch(IOException e)
 {
 }
}

void keyPressed()
{
 if(key == ' ')
 {
   exit();
 }
}

Re: external file control?
Reply #2 - Oct 23rd, 2009, 1:26am
 
Look at Java's File class.
Should be something like:
Code:
File fileToDelete = new File(pathOfFileToDelete);
fileToDelete.delete();

starkes' method should be avoided, as it is non portable (OK for a quick sketch to run for you, not to share).
starkes, File also provides list() and listFiles() methods.
Re: external file control?
Reply #3 - Oct 23rd, 2009, 10:11am
 
mmm good to know.
Page Index Toggle Pages: 1