// Java class to use instead of FileSystemObject On ASP // Author: Walter D'Avila Neto // Dependencies // package com.util.filesys; import java.io.File; import java.util.Vector; // --------------------------------------------------------------------------------------- // File System Class // Obs: It should work very likely a dos linux " Command Prompt Commands " // Commands: ls, mkdir, rmdir etc.. // Other Interfaces: // getFolders , getFiles // for further information see on Method Declaration // --------------------------------------------------------------------------------------- public class FileSystemViewer { // ------------------------------------------------------------------------------------ // Properies public File currentDir; // this is our Current Directory // ------------------------------------------------------------------------------------- // Construcotrs !! public FileSystemViewer (){ // Oks Let us make some conventions here... // if no string is passad to construcotor // We assume that "C:/" is valid at least. currentDir = new File("C:"); } public FileSystemViewer (String aDirectory) { File testFile = new File (aDirectory); // ok.. so let's test this string to see if it's a valid dir if ( testFile.exists() && testFile.isDirectory()) // So it's is Valid and it is a Folder this.currentDir = new File(aDirectory); // For god's sakes I want a pointer !!! else this.currentDir = new File("C:"); // Just As Default Contructor testFile = null ; // Let's put some order here !!! } // ------------------------------------------------------------------------------------- // // Also ..just as chDir or cd .. // public boolean chdir ( String aDir) { File tmpDir = new File(currentDir.getPath() + aDir); // Case realtive path if ( tmpDir.exists() && tmpDir.isDirectory() ) { // Check if This directory Exists and it is here ! this.currentDir = tmpDir ; // ok... return true; } else { // Case Absolute path tmpDir = new File ( aDir ) ; if ( tmpDir.exists() && tmpDir.isDirectory() ) { // Is it a Absolute PAth Value ?? this.currentDir = tmpDir ; // ok return true; } } return false; // not Valid Path } // ------------------------------------------------------------------------------------- // Get Current Folders // ------------------------------------------------------------------------------------- public File getFolder() { return currentDir; } // ------------------------------------------------------------------------------------- // get A List of Files on Current Directory // ------------------------------------------------------------------------------------- public File[] getFiles() { Vector arrayFiles = new Vector(); // Array of Files File tmpFile = new File(currentDir.getPath()); // get CurrentDir String[] allFiles = tmpFile.list(); // Get a list of All "files" or itens on this Directory // here we will interate over all Files on current Dir // checkig in each file is it's a File or not. for ( int iCounter = 0 ; iCounter < allFiles.length ; iCounter++ ){ tmpFile = new File(currentDir.getPath() + "\\" + allFiles[iCounter]); if ( tmpFile.isFile() ) // Is this a File (or Non Folder..) ? arrayFiles.addElement( tmpFile ); // } File[] returnFileList = new File[arrayFiles.size()]; // Create New File List to retun the Values! arrayFiles.copyInto(returnFileList); // Copy Values on our Vector To return List return returnFileList; // return the list } // --------------------------------------------------------------- // Get All Folder from Current Folder (this) // --------------------------------------------------------------- public File[] getSubFolders (){ Vector arrayFolders = new Vector(); // Array of Folders File tmpFile = new File(currentDir.getPath()); // get CurrentDir String[] allFiles = tmpFile.list(); // Get a list of All "files" or itens on this Directory // here we will interate over all Files on current Dir // checkig in each file is it's a Folder or not. for ( int iCounter = 0 ; iCounter < allFiles.length ; iCounter++ ){ tmpFile = new File(currentDir.getPath() + "\\" + allFiles[iCounter]); if ( tmpFile.isDirectory()) { // Is this a Folder ? arrayFolders.addElement( tmpFile ); } } File[] returnFileList = new File[arrayFolders.size()]; // Create New String List to retun the Values! arrayFolders.copyInto(returnFileList); // Copy Values on our Vector To return List return returnFileList; // return the list } }