// 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 FileSystem { // ------------------------------------------------------------------------------------ // Properies public File currentDir; // this is our Current Directory // ------------------------------------------------------------------------------------- // Construcotrs !! public FileSystem (){ // 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 FileSystem (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 !!! } // ------------------------------------------------------------------------------------- // // This method should work just as mkdir in cmd // Obs: Remember that you should know where you are!! (just as cmd..) public boolean mkdir ( String newDirectory ) { File tmpDir = new File(newDirectory); tmpDir.mkdirs(); return tmpDir.exists(); } // // 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 } public File getFolder() { return currentDir; } public String[] 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.getPath() ); // } String[] returnStringList = new String[arrayFiles.size()]; // Create New String List to retun the Values! arrayFiles.copyInto(returnStringList); // Copy Values on our Vector To return List return returnStringList; // return the list } public String[] 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.getPath()); } } String[] returnStringList = new String[arrayFolders.size()]; // Create New String List to retun the Values! arrayFolders.copyInto(returnStringList); // Copy Values on our Vector To return List return returnStringList; // return the list } // public static void main ( String[] args ) { // System.out.println ( "Walter Test"); // } }