Question

I currently have a log file(see bellow) that I need to iterate through and pull out a list of files that were scanned. Then by using that list of files copy the scanned files into a different directory.

So in this case, it would go through and pull c:\tools\baregrep.exe c:\tools\baretail.exe etc etc

and then move them to a folder, say c:\folder\SafeFolder with the same file structure

I wish I had a sample of what the output was on a failed scan, but this will get me a good head start and I can probably figure the rest out

Symantec Image of Log File

Thanks in advanced, I really appreciate any help that you can lend me.

Was it helpful?

Solution

This should do the trick. Now, just adapt for your solution!

public static void find(String logPath, String safeFolder) throws FileNotFoundException, IOException {
    ArrayList<File> files = new ArrayList<File>();

    BufferedReader br = new BufferedReader(new FileReader(logPath));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = br.readLine()) != null) {
        Pattern pattern = Pattern.compile("'[a-zA-Z]:\\\\.+?'");
        Matcher matcher = pattern.matcher(line);
        if (matcher.matches()) {
        }
        if (matcher.find()) {
            files.add(new File(matcher.group()));
            System.out.println("Got a new file! " + files.get(files.size() - 1));
        }
    }

    for (File f : files) {
        // Make sure we get a file indeed
        if (f.exists()) {
            if (!f.renameTo(new File(safeFolder, f.getName()))) {
                System.err.println("Unable to move file! " + f);
            }
        } else {
            System.out.println("I got a wrong file! " + f);
        }
    }
}

OTHER TIPS

This question is tagged as Java, and as much as I love Java, this problem is something that would be easier and quicker to solve in a language such as Perl (so if you only want the end result and do not need to run in a particular environment then you may wish to use a scripting language instead).

Not a working implementation, but code along the lines of the below is all it would take in perl: (Syntax untested and likely broken as is, only serves as a guideline.. been awhile since I wrote any perl).

use File::Copy;
my $outdir = "c:/out/";
while(<>)
{
  my ($path) = /Processing File\s+\'([^\']+)\'/;
  my ($file) = $path =~ /(.*\\)+([^\\]+)/;
  if (($file) && (-e $path))
  {
    copy($path,$outdir . $file);
  }
}

Its straight forward.

  1. Read the Log file line by line using NEW_LINE as your deliminator. If this is a small file, feel free to load it & process it via String.split("\n") or StringTokenizer

  2. As you loop each line, you need to do a simple test to detect if that string contains 'Processing File '.

  3. If it does, using Regular Expression (harder) or simple parsing to capture the file names. It should be within the ['], so detect the first occurrence of ['], and detect the second, and get the string in between.

  4. If your string is valid (you may test using java.io.File) or existing, you could copy the file name to another file. I would not advise you against copying it in java for memory restrictions for starters.

  5. Instead, copy the string of files to form a batch file to copy them at once using the OS Script like Windows BAT or Bash Script, eg cp 'filename_from' 'copy_to_dir'

Let me know of you need a working example

regards

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top