Results 1 to 9 of 9
  1. #1
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    150
    Rep Power
    7

    Thumbs up [SOLVED] Folder Watching

    Hi Folks,

    I am having some files(.txt) in a directory called as Unprocessed folder,i would like to take one by one file and send it to the processing path say it as..C:\processingfolder
    Each time when an UnprocessFolder get any new file it should be sent to the processing folder.

    Any Idea....thanks in advance


    -jazz

  2. #2
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default

    Have u started with itatleast getting .txt files from a directory?
    Rest would be next step, Do it step by step and in a simple way.
    To finish sooner, take your own time....
    Nivedithaaaa

  3. #3
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    150
    Rep Power
    7

    Default

    yeah i am able to List the content of a folder...but the main prob is to pick up the file from the folder...

    suggest some ideas..

    thanks for ur reply

    -jazz

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,278
    Rep Power
    25

    Default

    pick up the file
    Does that mean moving the file from one directory to another?
    One way would be to copy the file from one directory to the other and then deleting the file from the one directory.

  5. #5
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    150
    Rep Power
    7

    Default

    Ur Right...it need to copy from the folder and send it to another path:cool:
    If any new file comes into the Source folder it should be copied to the Dest Folder

  6. #6
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default

    Is this what u wanted?

    Java Code:
    public static void copy(File source, File dest) {
            try {
                BufferedReader br = new BufferedReader(new FileReader(source));
                BufferedWriter bw = new BufferedWriter(new FileWriter(dest)); 
                
                int read = 0;
                while((read = br.read()) != -1) {
                    System.out.println((char)read);
                    bw.write(read);
                }
                
                br.close();
                bw.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    To finish sooner, take your own time....
    Nivedithaaaa

  7. #7
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default

    To finish sooner, take your own time....
    Nivedithaaaa

  8. #8
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default Copy file into different folder and get the file created time

    Here is the code

    Java Code:
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class copyFiles {
    
    	public static void copy(File source, File dest) {
    	          
    		File cpDestFile = null;
    		try {
    			if (dest.isDirectory()) {
    				if (source.isFile()) {
    					System.out.println("File name = " + source.getName());
    					System.out.println("File name dest = " + dest.getPath());
    					System.out.println("File name cpDestFile = "
    							+ dest.getPath() + File.separator
    							+ source.getName());
    					cpDestFile = new File(dest.getPath() + File.separator
    							+ source.getName());
    
    				} else {
    					System.out.println("Directory or File Not Found");
    					return;
    				}
    			}
    			BufferedReader br = new BufferedReader(new FileReader(source));
    			BufferedWriter bw = new BufferedWriter(new FileWriter(cpDestFile));
    			int read = 0;
    			while ((read = br.read()) != -1) {
    						bw.write(read);
    			}
    			br.close();
    			bw.close();
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	public static void main(String args[]) {
    		copyFiles fcpy = new copyFiles();
    		File cpSrcFile = new File("D:\\Unprocessed_folder\\text1.txt");
    		File cpDestDir = new File("D:\\processingfolder");
    		fcpy.copy(cpSrcFile, cpDestDir);
    
    	}
    }
    and check if any file has been created after a specific time which u need to store in some variable and compare it with file created time with the following code and then copy it into ur destination folder.

    Java Code:
    file = "C:\\yourFile.txt";
    try 
    { //NOTE FILES MUST NOT HAVE SPACES IN THE NAMES OTHERWISE THIS DONT WORK!
    Process ls_proc = Runtime.getRuntime().exec("cmd.exe /c dir " + getFilePath() + " /tc");
    DataInputStream in = new DataInputStream(ls_proc.getInputStream());
    for (int i = 0; i < 5; i++ )
    {
    in.readLine(); 
    }
    String stuff = in.readLine();
    StringTokenizer st = new StringTokenizer(stuff);
    String dateC = st.nextToken();
    String timeC = st.nextToken();//TIME CREATED
    in.close();
    }
    catch (IOException e1) 
    {
    System.out.println("Error in getting create time");
    }
    To finish sooner, take your own time....
    Nivedithaaaa

  9. #9
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    150
    Rep Power
    7

Similar Threads

  1. folder cloning
    By jad in forum Advanced Java
    Replies: 1
    Last Post: 07-01-2008, 12:28 AM
  2. How can i copy a folder from one place to another..
    By rajeshgubba in forum New To Java
    Replies: 4
    Last Post: 06-14-2008, 02:21 AM
  3. add password to folder
    By ismailsaleh in forum AWT / Swing
    Replies: 1
    Last Post: 01-08-2008, 05:46 AM
  4. how to use class in upper folder
    By osval in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:55 PM
  5. creation of new folder in the mail
    By an8086 in forum Advanced Java
    Replies: 1
    Last Post: 07-15-2007, 05:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •