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