Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Java code to to get files in a given directory from a server and do operations

  1. #1
    Junior Member macquil7's Avatar
    Join Date
    May 2009
    Location
    A boy next door
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java code to to get files in a given directory from a server and do operations

    Hi,

    I am administering a couple of servers and I would to create an IT Audit Management System. The goal basically is to get snap shots of the paths and drives I wanted to monitor. The app will extract the file information in the paths and drives I wanted and will compare from previous extract for discrepancies (new, deleted, updated files).

    Is this possible in Java? What would be my best approach? Thank you.

    Regards,
    macquil7


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Server Audit

    Hello macquil7 and welcome to the Java Programming Forums

    Yes this is possible. We have some code snippets in our tips & tutorials forum that may help you:

    http://www.javaprogrammingforums.com...directory.html

    http://www.javaprogrammingforums.com...tory-path.html

    http://www.javaprogrammingforums.com...date-time.html

    It would be easy enough to get a list of all files in a directory then compare that list against the same directory at another given date/time.

    Is this what you are looking to do or are you thinking more advanced than this?

    I can help you get started if you wish...
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member macquil7's Avatar
    Join Date
    May 2009
    Location
    A boy next door
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Server Audit

    Hi, thank you very much. Yeah, my goal for now is to get the files in a given directory from a server and then compare from previous day. Any discrepancy, an email will be sent to me. As I progress, I also wanted to be able to extract the programs or applications installed in a server to be able to monitor any changes. Capturing the changes in the registry will also be good.

    If you don't mind, i will really appreciate your assistance to get me started. Thank you very much.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Server Audit

    Hello macquil7,

    Sure, I can help you get started. I have written this code for you..

    This application will list all the files in a given directory and write the results to a dated log file. For example, auditlog-07-05-2009.txt

    The next step would be to write an application to compare the results of 2 log files.

    Let me know if you need help.

    import java.io.*;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
     
    public class ServerAudit {
     
        public static String files;
        public static String newLine = System.getProperty("line.separator");
     
         public static final String DATE_FORMAT_NOW = "dd-MM-yyyy HH:mm:ss";
         public static String dateTime;
         public static String date;
     
     
        /**
         * JavaProgrammingForums.com
         */
        public static void main(String[] args) {
     
            ServerAudit sa = new ServerAudit();
     
            System.out.println(sa.DateTime());
            System.out.println("Files in Directory:");
            System.out.println("-------------------");
     
            date = DateTime().substring(0, 10);
            sa.ListFiles();
     
        }
     
        public void ListFiles() {
     
            try {
     
                // For writing file list to log file
                Writer output = null;
                File file = new File("auditlog-" + date + ".txt");
                output = new BufferedWriter(new FileWriter(file));
     
                // Directory path here
                // . = application root
                String path = ".";
     
                File folder = new File(path);
                File[] listOfFiles = folder.listFiles();
     
                output.write(DateTime());
                output.write(newLine);
     
                for (int i = 0; i < listOfFiles.length; i++) {
     
                    if (listOfFiles[i].isFile()) {
                        files = listOfFiles[i].getName();
     
                        if (!files.equals("auditlog-" + date + ".txt")) {
     
                            System.out.println(files);
                            output.write(files);
                            output.write(newLine);
                        }
     
                    }
                }
     
                output.close();
                System.out.println("");
                System.out.println("Audit Log file written");
     
            } catch (Exception e) {
                System.out.println(e);
            }
     
        }
     
         public static String DateTime() 
         {
             Calendar cal = Calendar.getInstance();
             SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
             return sdf.format(cal.getTime());
         }
     
     
    }
    Console output:

    07-05-2009 10:03:54
    Files in Directory:
    -------------------
    .classpath
    .project

    Audit Log file written
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Server Audit

    Hello macquil7,

    Has this helped at all?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  6. #6
    Junior Member macquil7's Avatar
    Join Date
    May 2009
    Location
    A boy next door
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Server Audit

    hi JavaPF,

    I'm just about to try this today. I'll update the thread as soon as I got something. Thank you very much.

    Regards,
    macquil7

  7. #7
    Junior Member macquil7's Avatar
    Join Date
    May 2009
    Location
    A boy next door
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Server Audit

    tried it but it only writes down date and timestamp in the generated text file.

    i.e. 13-05-2009 11:19:04

    I updated the value of string path with the below

    String path = "c:\\Program Files";

  8. #8
    Junior Member macquil7's Avatar
    Join Date
    May 2009
    Location
    A boy next door
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Server Audit

    Tried String path = "."; and it read the files in the application root however, it doesn't include the folders and recursively reading them too.. one for me to figure out i think.

  9. #9
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Server Audit

    Quote Originally Posted by macquil7 View Post
    tried it but it only writes down date and timestamp in the generated text file.

    i.e. 13-05-2009 11:19:04

    I updated the value of spring path with the below

    String path = "c:\\Program Files";
    When you run this application, the date and time stamp is written into the log file with the other information. When the log file is generated & saved the date is also included in the log files name. For example auditlog-07-05-2009.txt. I have just tested this again and that is definitely what happens.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  10. #10
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Server Audit

    String path = "c:\\Program Files"; should list all the files in that directory. Is this not the case?

    So you also want this to list folders as well as files and also check each folder in the given directory?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  11. #11
    Junior Member macquil7's Avatar
    Join Date
    May 2009
    Location
    A boy next door
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Server Audit

    hi JavaPF,

    yeah.. it does work.. i overlooked. It appears like it only reads the files and doesn't reads the folders in it and recursively reading the files in each folders.

    Thanks.

  12. #12
    Junior Member macquil7's Avatar
    Join Date
    May 2009
    Location
    A boy next door
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Server Audit

    Quote Originally Posted by JavaPF View Post
    So you also want this to list folders as well as files and also check each folder in the given directory?
    Yeah. I'm trying to figure out how to do this as well. Thanks.

  13. #13
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Server Audit

    I have made a small edit here to include the folders as well:

    import java.io.*;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
     
    public class ServerAudit {
     
        public static String files;
        public static String newLine = System.getProperty("line.separator");
     
         public static final String DATE_FORMAT_NOW = "dd-MM-yyyy HH:mm:ss";
         public static String dateTime;
         public static String date;
     
     
        /**
         * JavaProgrammingForums.com
         */
        public static void main(String[] args) {
     
            ServerAudit sa = new ServerAudit();
     
            System.out.println(sa.DateTime());
            System.out.println("Files in Directory:");
            System.out.println("-------------------");
     
            date = DateTime().substring(0, 10);
            sa.ListFiles();
     
        }
     
        public void ListFiles() {
     
            try {
     
                // For writing file list to log file
                Writer output = null;
                File file = new File("auditlog-" + date + ".txt");
                output = new BufferedWriter(new FileWriter(file));
     
                // Directory path here
                // . = application root
                String path = ".";
     
                File folder = new File(path);
                File[] listOfFiles = folder.listFiles();
     
                output.write(DateTime());
                output.write(newLine);
     
                for (int i = 0; i < listOfFiles.length; i++) {
     
                        files = listOfFiles[i].getName();
     
                        if (!files.contains("auditlog-")) {
     
                            System.out.println(files);
                            output.write(files);
                            output.write(newLine);
                        }
     
                    }
     
     
                output.close();
                System.out.println("");
                System.out.println("Audit Log file written");
     
            } catch (Exception e) {
                System.out.println(e);
            }
     
        }
     
         public static String DateTime() 
         {
             Calendar cal = Calendar.getInstance();
             SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
             return sdf.format(cal.getTime());
         }
     
    }
    If you wish to go into each of those folders and grab a list of all the files it starts to get a lot more complicated.
    I haven't got time to look at this for you now but I will see if I can expand on this for you tomorrow.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  14. #14
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Server Audit

    Hello macquil7, did you get any further with this?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  15. #15
    Junior Member macquil7's Avatar
    Join Date
    May 2009
    Location
    A boy next door
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Server Audit

    hi JavaPF, i wasn't able to expand the code this past few days as I was on holiday... i'll try it within this week. thanks.