Hi copeg, thanks for your reply! I couldn't quite figure out how to use the code that you gave me but it did point me in a good direction and I got it the window logging to work in the end, after a lot of googling and trying things out.
The code works now as I want it but I'd really appreciate it if someone could take a look and tell me if there are any glaring coding mistakes/bad practices in there. I'm pretty sure there are, as I suspect the way I use variables isn't the best, but there are probably others too.
import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JTextArea;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.SwingWorker;
import javax.swing.JScrollPane;
import java.util.logging.*;
import java.awt.BorderLayout;
public class FileWatcher extends JPanel {
private static String line2 = "error";
private JFrame frame = new JFrame();
private JTextArea textArea = new JTextArea();
private JScrollPane pane = new JScrollPane(textArea);
private static FileWatcher watcher = new FileWatcher();
private void sample(String path) throws Exception {
showInfo("Monitoring: " + path);
int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED;
boolean watchSubtree = true;
int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());
Thread.sleep(1000000);
boolean res = JNotify.removeWatch(watchID);
if (!res) {
//System.out.println("Invalid");
showInfo("Invalid");
}
}
public void TouchFile(String fName) {
if ((fName.indexOf("Thumbs") == -1) && (fName.indexOf(".rar") == -1) && (fName.indexOf(".part") == -1)){
String TodayDate = "";
String fModifyDate = "";
String line;
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
File filename = new File(fName);
try {
} catch (Exception e) {
e.printStackTrace();
}
TodayDate = dateFormat.format(calendar.getTime());
fModifyDate = dateFormat.format(filename.lastModified());
try {
Thread.currentThread().sleep(1000);
while ((line2.indexOf("error") != -1) && (!fModifyDate.equals(TodayDate))){
//System.out.println(fName + " modified on: " + fModifyDate);
showInfo(fName + " modified on: " + fModifyDate);
line2 = " ";
Process p = Runtime.getRuntime().exec("touch.exe " + "\"" + fName + "\"");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
line2 = line2 + "\n" + line;
}
input.close();
showInfo(line2);
p.waitFor();
}
}
catch (Exception err) {
err.printStackTrace();
}
}
}
public void showInfo(String data) {
watcher.textArea.append(data+"\n");
watcher.frame.getContentPane().validate();
watcher.textArea.setCaretPosition(watcher.textArea.getText().length() - 1);
}
class Listener implements JNotifyListener {
public void fileRenamed(int wd, String rootPath, String oldName,String newName) {
//print("renamed " + rootPath + " : " + oldName + " -> " + newName);
showInfo("renamed " + rootPath + "\\" + oldName + " -> " + newName);
}
public void fileModified(int wd, String rootPath, String name) {
//print("modified " + rootPath + " : " + name);
TouchFile(rootPath + "\\" + name);
showInfo("modified " + rootPath + "\\" + name);
}
public void fileDeleted(int wd, String rootPath, String name) {
//print("deleted " + rootPath + " : " + name);
showInfo("deleted " + rootPath + "\\" + name);
}
public void fileCreated(int wd, String rootPath, String name) {
//print("created " + rootPath + " : " + name);
TouchFile(rootPath + "\\" + name);
showInfo(rootPath + "\\" + name);
}
void print(String msg) {
//System.err.println(msg);
showInfo(msg);
}
}
public static void main(String[] args) {
String path = "H:\\download";
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
watcher.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
watcher.frame.setSize(500,200);
watcher.frame.setVisible(true);
watcher.frame.getContentPane().add(watcher.pane);
watcher.frame.setVisible(true);
watcher.frame.setLocation(screenSize.width-500,screenSize.height-250);
try {
if (args.length > 0){
new FileWatcher().sample(args[0]);
}
else {
new FileWatcher().sample(path);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}