Scanning directory and moving pictures in processing

Viewed 68

I have to make a code for a school project, which will scan a certain directory ( in this case c:/test ) for any image files which appear in it and transfer those to another directory, where I will store their file location and then upload this to my database. For testing purposes, I am assuming that the image files will appear in the c:/test directory formatted as 1.jpeg, 2.jpeg and so on. RIght now, if I drag a 1.jpeg file into the test folder, the program works. However when I try to drag in 2.jpeg the code does nothing. This is due to the fact the nr_of_files is increasing for some reason. Could someone please point me in the right direction?

import java.nio.file.*;
import de.bezier.data.sql.*;
import java.io.*;
import java.util.Date;
String[] filenames;
String path = "C:\\test\\";
int nr_of_files = 0;
int picnames = 1;
String imgonefilepath;
MySQL db;
String user = "root";
String pass = "";
String database = "imageloc";



void setup () {
  db = new MySQL(this, "localhost", database, user, pass);
  checkdir();
}



void draw () {

  if ( checkdir() > 0 ) {
    while ( nr_of_files < filenames.length ) {
      move();
      println( "new file in test folder: " + filenames[nr_of_files++] );
    }
    getPath();
    //sendData();
  }
  println("nr_of_files: " + nr_of_files);
  println("filenames.length" + filenames.length);
  println("picnames: " + picnames);

}

void move() {

  String newpath = "C:\\test123\\";
  Path source = Paths.get(path + picnames + ".jpeg");
  Path newdir = Paths.get(newpath + picnames + ".jpeg");
  int numberofnewfiles = checkdir();

  if (numberofnewfiles > 0) {

    try {
      Files.move(source, newdir);
    } 
    catch (IOException e) {
      println(e);
    }
  }
  //picnames++;
  println(picnames);
}
void getPath() {
  File img_one = dataFile("C:\\test123\\1.jpeg");
  String img_onefile_path = img_one.getPath();
  boolean exist = img_one.isFile();

  if (exist == true) {
    println(img_onefile_path);

    if ( db.connect() ) {
      db.query("INSERT INTO ogloc (location) VALUES ('%s')", img_onefile_path);

      println("success");
      println(img_onefile_path);
    } else {
      println("failure");
    }
  }
}
void sendData() {
  if ( db.connect() ) {
    db.query("INSERT INTO ogloc (location) VALUES ('%s')", imgonefilepath);
    // numeric++;
    println("success");
    println(imgonefilepath);
    // Files.move(Paths.get("C:/test/) );
  } else {
    println("failure");
  }
  //println( "INSERT INTO test (b64) VALUES ('" + b64string + "')" );
}


int checkdir() {
  //  println("Listing all control filenames in a directory: ");
  filenames = listFileNames( path );
  //  print("difference in length:  ");
  //  println(filenames.length - nr_of_files);
  return filenames.length - nr_of_files;
}



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;
  }
}
1 Answers

One of the problems is that you are hard-coding paths for just image 1.jpeg and therefore any other images within the folder are not moved. Take a look at this code snippet:

String path = "C:\\...\\test\\";

void setup() {
    move();
}

void draw() {
}

void move() {

    File dir1 = new File(path);
    if (dir1.isDirectory()) {

        //Get all files as array from the source directory
        File[] content = dir1.listFiles();

        //Iterate through each file
        for (int i = 0; i < content.length; i++) {
            //Get the file original Path
            Path source = Paths.get(content[i].getPath());
            //Get the file destination path by appending File name to the new Path
            Path newpath = Paths.get("C:\\...\\test123\\" + source.getFileName());

            //Move the files
            try {
                Files.move(source, newpath);
            } catch (IOException e) {
                print(e);
            }
        }
    }
}

This code snippet will scan the source directory containing several images and will move these images to the destination directory. What is left for you is to add the database storing aspects - e.g. you can iterate through the destination folder and retrieve the file paths in similar fashion as shown above, or you can do it however you prefer.

P.S. Be aware that the draw function is launched 60 times every second - therefore, you would probably be better off not putting your methods in it - draw() should really be used for drawing animations and similar.

Related