Results 1 to 6 of 6
  1. #1
    phaniakella is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Move files to different folders based on the value in the first line

    Dear Experts

    I am writing a Java code to move files from Source folder to different target folders based on the value in first line. The code is compiling fine but files are not getting moved. Can you please check below code and let me know where I am wrong?

    Also please feel free to change my code if this can be done better and in much efficient way. Thanks in advance.

    Java Code:
    package com.test;
    
    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.FilenameFilter;
    import java.io.IOException;
    
    public class MoveSunIDFiles {
    
    	/**
    	 * @param args
    	 * @throws IOException
    	 */
    	public static void main(String[] args)  {
    		// TODO Auto-generated method stub
    
    		File src = new File("C:\\Users\\akellap\\RND\\Test\\Source");
    
    		// create new filename filter for .dat files
    
    		File[] result = src.listFiles(new FilenameFilter() {
    			public boolean accept(File dir, String name) {
    				if (name.lastIndexOf('.') > 0) {
    					// get last index for '.' char
    					int lastIndex = name.lastIndexOf('.');
    
    					// get extension
    					String str = name.substring(lastIndex);
    
    					// match path name extension
    					if (str.equals(".dat")) {
    						return true;
    					}
    				}
    				return false;
    			}
    		});
    
    		if (result != null && result.length > 0) {
    			for (int i = 0; i < result.length; i++) {
    				BufferedReader in = null;
    				try {
    					in = new BufferedReader(
    							new FileReader(result[i]));
    				} catch (FileNotFoundException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    
    				String line = "";
    
    				try {
    					while ((line = in.readLine()) != null) {
    						if (line.contains("645559")) {
    							move645559(result[i]);
    							break;
    						}
    
    						if (line.contains("508659")) {
    							move508659(result[i]);
    							break;
    						}
    					}
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    
    			}
    		} else if (result == null) {
    			System.out.println("Invalid filename or folder");
    		} else {
    			System.out.println("No files matching the criteria");
    		}
    
    	}
    
    	private static void move508659(File result) {
    		// TODO Auto-generated method stub
    
    		if (result.renameTo(new File(
    				"C:\\Users\\akellap\\RND\\Test\\Target\\SUN_ID_508659\\"
    						+ result.getName()))) {
    			System.out.println("File:" + result + " moved successfully");
    		} else {
    			System.out.println("File " + result.getName() + " not moved");
    		}
    	}
    
    	private static void move645559(File result) {
    		// TODO Auto-generated method stub
    		if (result.renameTo(new File(
    				"C:\\Users\\akellap\\RND\\Test\\Target\\SUN_ID_645559\\"
    						+ result.getName()))) {
    			System.out.println("File:" + result + " moved successfully");
    		} else {
    			System.out.println("File " + result.getName() + " not moved");
    		}
    	}
    }
    Last edited by phaniakella; 11-29-2016 at 12:24 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Move files to different folders based on the value in the first line

    but files are not getting moved
    What does the program do when executed?

    How are you trying to debug the program to see what it is doing?
    Last edited by Norm; 11-29-2016 at 12:54 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Move files to different folders based on the value in the first line

    Might I suggest you write a very simple program to move a single file to another location to see if it works. The
    ability to move files using the rename command is platform dependent and may not work in some situations (e.g.
    across file systems).

    Check out the Files class for a platform independent move option.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    phaniakella is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: Move files to different folders based on the value in the first line

    1. Programme checks for .dat files in Source folder.
    2. Check for a specific texte (645559 or 508659) in first line of each file.
    3. If 645559 exists, move the files to folder SUN_ID_645559
    4. If 508659 exists, move the files to folder SUN_ID_508659

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Move files to different folders based on the value in the first line

    Do as jim suggests.
    Take a single file, which you know has one of those values in the first line, and run it through the code.
    Add lots of debug statements i there (println()) so you can track what's going on.

    Note, in your loop where you read the file, if there is no number in the first line then it will continue to read the file.
    That doesn't match with what your requirements say.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Move files to different folders based on the value in the first line

    It would be helpful to the program's user if a message were displayed as each file is moved that showed the full source path, the target path and the results.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: 03-18-2014, 10:43 AM
  2. Unzip files and folders
    By Asolpdrag in forum New To Java
    Replies: 16
    Last Post: 06-29-2012, 06:38 PM
  3. Help please, Playing with folders and files.
    By paulio2 in forum New To Java
    Replies: 38
    Last Post: 11-17-2011, 02:16 PM
  4. Access files in folders and sub-folders
    By kakinyim in forum CLDC and MIDP
    Replies: 0
    Last Post: 05-18-2011, 09:29 PM
  5. how to display computer folders and files in GUI
    By nashbender in forum AWT / Swing
    Replies: 12
    Last Post: 04-22-2011, 04:28 AM

Posting Permissions

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