WD1 Source External Drive File Recorder Source letter and 2 brackets (G:\\) DirectoryFileRecorder (Eclipse 2023 03 version) ============================================= === DirectoryLocator.java public class DirectoryLocator { private String sourceDrivePath; //Example I:\\ used to provide the drive to read all directories and files private String targetDrivePath; //Example F:\\ used to provide the drive to save each source drive data under the sourceDriveName.txt folder public void setSourceDrivePath(String p) { this.sourceDrivePath = p; } public String getSourceDrivePath() { return sourceDrivePath; } public void setTargetDrivePath(String p) { this.targetDrivePath = p; } public String getTargetDrivePath() { return targetDrivePath; } ============================================= === DirectoryPrinter.java // Recursive Java program to print all files // in a folder(and sub-folders) import java.io.File; import java.io.IOException; public class DirectoryPrinter { //DirectoryLocator dirLocator; static void RecursivePrint(File[] arr, int level, DirectoryLocator dirLocator) { FileManager fileManager = new FileManager(); // for-each loop for main directory files for (File f : arr) { // tabs for internal levels String appendTab = ""; for (int i = 0; i < level; i++) appendTab = appendTab + ("\t"); String fname = f.getName().toLowerCase(); if (f.isFile()) try { fileManager.appendFile(dirLocator, appendTab + f.getName()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //System.out.println(f.getName()); else if (f.isDirectory() && !fname.startsWith("$") && !fname.startsWith("system") ) { System.out.println(appendTab + "[" + f.getName() + "]"); try { fileManager.appendFile(dirLocator, appendTab + "[" + f.getName() + "]"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // recursion for sub-directories RecursivePrint(f.listFiles(), level + 1,dirLocator); } } } // Driver Method public void main(DirectoryLocator pdirLocator) { FileManager fileManager = new FileManager(); File dp = new File(pdirLocator.getSourceDrivePath()); File fileslist[] = dp.listFiles(); for(File file: fileslist) { try { fileManager.appendFile(pdirLocator, file.getName()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // Calling recursive method RecursivePrint(fileslist, 0, pdirLocator); } } ============================================= === FileManager.java import java.io.PrintWriter; // Step 1 import java.io.IOException; import java.io.FileWriter; public class FileManager { public void setupFile(DirectoryLocator dirLocator) throws IOException { // Open the file. //PrintWriter out = new PrintWriter("F:\\oceans.txt"); // Step 2 PrintWriter out = new PrintWriter(dirLocator.getTargetDrivePath()); // Step 2 System.out.println("Test test targetDrivePath: " + dirLocator.getTargetDrivePath()); // Close the file. out.close(); // Step 4 } public void appendFile(DirectoryLocator dirLocator, String record) throws IOException { // Open the file in append mode. //FileWriter fw = new FileWriter("F:\\oceans.txt"); FileWriter fw = new FileWriter(dirLocator.getTargetDrivePath(),true); PrintWriter out = new PrintWriter(fw); out.println(record); //out.println("Atlantic"); // Step 3 //out.println("Pacific"); //out.println("Indian"); //out.println("Arctic"); //out.println("Southern"); // Close the file. out.close(); } } ============================================= === SWTRecorderSetup.java import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.Font; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.Color; import java.awt.event.ActionListener; import java.io.IOException; import java.awt.event.ActionEvent; public class SWTRecorderSetup { private JFrame frmExternalDriveFile; private JTextField textSourceDrive; private JTextField textTargetDrive; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { SWTRecorderSetup window = new SWTRecorderSetup(); window.frmExternalDriveFile.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public SWTRecorderSetup() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frmExternalDriveFile = new JFrame(); frmExternalDriveFile.setTitle("External Drive File Recorder"); frmExternalDriveFile.setBounds(100, 100, 596, 300); frmExternalDriveFile.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frmExternalDriveFile.getContentPane().setLayout(null); JLabel lblNewLabel_1 = new JLabel("Source drive letter and 2 backslashes ( G:\\\\ ):"); lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 13)); lblNewLabel_1.setBounds(10, 49, 324, 23); frmExternalDriveFile.getContentPane().add(lblNewLabel_1); textSourceDrive = new JTextField(); textSourceDrive.setText("G:\\\\"); textSourceDrive.setFont(new Font("Arial Black", Font.BOLD, 12)); textSourceDrive.setBounds(484, 48, 86, 23); frmExternalDriveFile.getContentPane().add(textSourceDrive); textSourceDrive.setColumns(10); JLabel lblNewLabel_2 = new JLabel("Target drive letter and name of Source as a text file"); lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD, 13)); lblNewLabel_2.setBounds(0, 107, 400, 23); frmExternalDriveFile.getContentPane().add(lblNewLabel_2); textTargetDrive = new JTextField(); textTargetDrive.setText("F:\\\\MiniVault.txt"); textTargetDrive.setFont(new Font("Arial Black", Font.BOLD, 12)); textTargetDrive.setBounds(410, 142, 160, 20); frmExternalDriveFile.getContentPane().add(textTargetDrive); textTargetDrive.setColumns(10); JLabel lblNotifications = new JLabel(".."); lblNotifications.setForeground(new Color(255, 0, 0)); lblNotifications.setFont(new Font("Tahoma", Font.BOLD, 14)); lblNotifications.setBounds(10, 172, 560, 31); frmExternalDriveFile.getContentPane().add(lblNotifications); JButton btnExecuteReadWrite = new JButton("Execute"); btnExecuteReadWrite.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // button clicked DirectoryLocator dirLocator = new DirectoryLocator(); dirLocator.setSourceDrivePath(textSourceDrive.getText()); dirLocator.setTargetDrivePath(textTargetDrive.getText()); FileManager fileManager = new FileManager(); try { fileManager.setupFile(dirLocator); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } DirectoryPrinter dirPrinter = new DirectoryPrinter(); dirPrinter.main(dirLocator); lblNotifications.setText("COMPLETED"); } }); btnExecuteReadWrite.setForeground(new Color(255, 0, 0)); btnExecuteReadWrite.setFont(new Font("Tahoma", Font.BOLD, 11)); btnExecuteReadWrite.setBounds(286, 227, 89, 23); frmExternalDriveFile.getContentPane().add(btnExecuteReadWrite); JLabel lblNewLabel = new JLabel("Example: F:\\\\WD1.txt or F:\\\\MiniVault.txt"); lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 13)); lblNewLabel.setBounds(10, 141, 324, 23); frmExternalDriveFile.getContentPane().add(lblNewLabel); } } =================================================================================== === END