Title: finding files in a directory with a specific file mask
Hi Mark !
 
Here is a testprogram that lists all files in a current folder (and its subfolders) based on a certain mask.
I don't know what you want to do with the ini. -files, but perhaps you can use this structure to get started ?
 
Hope it helps...
/S
 
/*
 * Created by IntelliJ IDEA.
 * User: stefan
 * Date: 2002-june-05
 * Time: 05:10:16
 * To change template for new class use
 * Code Style | Class Templates options (Tools | IDE Options).
 */
import java.io.File;
import java.io.FileFilter;
 
public class ListFiles {
 
    /**
     * Constructor
     *
     * @param folderPath    Root-folder where to start the search
     * @param fileMask      Part that must be in file that you are looking for
     *                                (in your case ".ini")
     */
    public ListFiles(String folderPath, String fileMask) {
        findFiles(new File(folderPath), fileMask);
    }
   
    /**
     * Find (recursively) all files with a specific
     * mask and do something with each file...
     *
     * @param folder    Root-folder where to start the search
     * @param fileMask  Part that must be in file that you are looking for
     *                  (in your case ".ini")
     */
    private void findFiles(File folder, final String fileMask) {
        // since you are running Windows you probably don't care about case sensitivity...
        final boolean ignoreCase = true;
 
        // read all files in actual folder that meets the specified criteria
        File[] files = folder.listFiles(new FileFilter() {
            public boolean accept(File file) {
                String fileName = file.getName();
                int index = fileName.length() - fileMask.length();
 
                return file.isFile() && fileMask.regionMatches(ignoreCase, 0, fileName, index, fileMask.length());
            }
        });
 
        // do something with the files that you found (call your method for reading the
        // content of the file and construct your xml-tags...); just printing them out here...
        for (int i = 0; files != null && i < files.length; i++) {
            if (files[i] != null) {
                //...
                System.out.println(files[i]);
                //...
            }
        }
 
        // for all subfolder to the actual folder...
        File[] folders = folder.listFiles(new FileFilter() {
            public boolean accept(File dir) {
                return dir.isDirectory();
            }
        });
 
        int currentFolderIndex = 0;
 
        for (int i = 0; folders != null && i < folders.length; i++) {
            // move to next folder (recursively) and do the
            // above processing on the files found there
            findFiles(folders[currentFolderIndex++], fileMask);
        }
    }
 
    public static void main(String[] args) {
        String folderPath = "c:\\temp";
        String mask = ".txt";
 
        new ListFiles(folderPath, mask);
    }
}
-----Ursprungligt meddelande-----
Fr�n: Mark Meyer [mailto:[EMAIL PROTECTED]]
Skickat: den 4 juni 2002 23:31
Till: JDJList
�mne: [jdjlist] finding files in a directory with a specific file mask


hi everyone..

i am making the conversion from delphi to java.

i am writing my first java utility app that will

1) loop through a given directory (on a windows box) looking for files with a specific file mask

2) then open up the file and interrogate the information inside (the files are actually .ini files)

3) and then write out the information in an XML format

my first task at hand is item 1)

i have classes/components written in Delphi to do recursive file searching - but i don't have a clue as to how to do this in java.  any help, sources of information or guidance would be hugely appreciated.

any help or suggestions on items 2) and or 3) would also be appreciated.

thx
mark

To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm
To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm

Reply via email to