Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I trigger an event went directory contents change? 1

Status
Not open for further replies.

no137

Programmer
Feb 5, 2002
7
US
How can I trigger an event when files in a filesystem directory changes(either file added or removed)? Please help.

Thanks
Mike
 
If you are wanting to catch files added or removed from
out side your program then you'll have to start a thread
to monitor the contents of the directory.

public class monitor extends Thread
{
public String list[];
public String directory;
public monitor(String direc)
{
directory=direc;
File f=new File(direc);
list=File.list();
}
public void run()
{
File f= new File(directory);
String[] listing=f.list();
if(listing.length != list.length)
{
//event
}else {
for(int i=0;i<listing.length;i++)
if(!(listing.equals(list);
//event
}
try{
sleep(3000);//sleeps 3 seconds
}catch(InterruptedException ie)
{}
}
}

However bear in mind that this will not catch changes to
subdirectories. Also you can get into trouble with
sym-links since it will see sym-links the same as files
ie people will be able to fool it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top