java - clean up demos

Post Reply
stealth
Posts: 140
Joined: Fri Dec 09, 2011 6:02 pm

java - clean up demos

Post by stealth »

This is the same as the perl script I made. Difference is this written in java so more people can run it without having to install other software.

This only cleans up AP demos that follow the ETrun name standard.

Instructions:
-Download zip
-Extract
-Run

Source:

Code: Select all

package democleaner;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.*;
import javax.swing.*;

public class CleanerUi extends JFrame {

    JFileChooser directoryMenu;
    JButton browseButton = new JButton("Browse"), cleanButton = new JButton("Clean");
    JTextField path = new JTextField();
    JScrollPane scrollLog;
    JTextArea log;
    Container c;

    public CleanerUi() {

        super("demo Cleaner");
        setBounds(100, 100, 350, 410);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        c = getContentPane();
        c.setLayout(new FlowLayout());

        directoryMenu = new JFileChooser();
        directoryMenu.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        directoryMenu.setDialogTitle("Select demos directory");
        directoryMenu.setAcceptAllFileFilterUsed(false);

        browseButton.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent e1) {
                        launchDemoBrowser();
                    }
                });

        cleanButton.setEnabled(false);
        cleanButton.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent e1) {
                        cleanDemos();
                    }
                });

        path.setEditable(false);
        path.setPreferredSize(new Dimension(200, 20));

        log = new JTextArea();
        log.setEditable(false);
        scrollLog = new JScrollPane(log);
        scrollLog.setPreferredSize(new Dimension(300, 300));
        log.append("Select your demos directory.\n");

        c.add(browseButton);
        c.add(cleanButton);
        c.add(path);
        c.add(scrollLog);

        setVisible(true);
    }

    private void launchDemoBrowser() {
        if (directoryMenu.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            path.setText(directoryMenu.getSelectedFile().toString());
            cleanButton.setEnabled(true);
            log.append("Demo dir: " + directoryMenu.getSelectedFile().toString() + "\n");
        } else {
            log.append("Select demos directory...\n");
        }
    }

    private void cleanDemos() {
        File folder = directoryMenu.getSelectedFile();
        ArrayList<File> files = new ArrayList<File>();

        for (File f : folder.listFiles()) {
            if (f.getName().matches("^.*\\[AP\\]_[0-9]*-[0-9]*-[0-9]*.[dD][mM]_84$")) {
                files.add(f);
            }
        }

        if (!files.isEmpty()) {
            log.append("Cleaning...\n");
            Collections.sort(files);

            String name1 = "";
            String name2 = "";
            for (File f : files) {
                String[] line = f.getName().split("_");
                name1 = "";
                for (int i = 0; i < line.length - 2; i++) {
                    name1 += line[i] + "_";
                }
                name1 = name1.substring(0, name1.length() - 1);
                if (name1.equals(name2)) {
                    log.append("Deleting: ");
                    f.delete();
                } else {
                    log.append("Keeping: ");
                }
                log.append(f.getName() + "\n");
                name2 = name1;
            }
        } else {
            log.append("No demos found.\n");
        }
        log.append("Done.\n");
    }

    public static void main(String[] args) {
        new CleanerUi();
    }
}
screenshot:
democleaner1.jpg
democleaner1.jpg (65.42 KiB) Viewed 11399 times
Attachments
ETrunAPDemoCleaner.zip
(3.38 KiB) Downloaded 1298 times
nico
Site Admin
Posts: 168
Joined: Mon Nov 07, 2011 11:40 pm

Re: java - clean up demos

Post by nico »

Thanks for this Stealth, very useful :)
--
>Nico*
Contact: pm me
Timothy
Posts: 41
Joined: Tue Nov 22, 2011 10:39 pm

Re: java - clean up demos

Post by Timothy »

Very useful indeed, thanks.
Post Reply