listfs.java
/*
* Copyright (c) 2001-2005 Servertec. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* THIS NOTICE MUST NOT BE ALTERED NOR REMOVED.
*
* CopyrightVersion 1.0
*/
import java.util.Date;
import stec.sfc.Win32.FileEnumerator;
import stec.sfc.Win32.File;
import stec.sfc.Win32.FileAttribute;
import stec.sfc.Win32.FileSystem;
public class listfs
{
private static final String PAD = " ";
public static void main(String[] args) throws Exception
{
String path;
String basedir;
if(args.length == 0)
{
basedir = FileSystem.getCurrentDirectory();
path = basedir + "\\*.*";
}
else
{
path = args[0];
if(path.endsWith("\\"))
{
basedir = path.substring(0, path.length());
path = basedir + "*.*";
}
else
{
File file = new File(path);
if((file.getAttributes() & FileAttribute.DIRECTORY) == FileAttribute.DIRECTORY)
{
if(path.length() == 2 && path.endsWith(":"))
{
basedir = FileSystem.getCurrentDirectory();
path = basedir + "\\*.*";
}
else
{
basedir = file.getLongPathName();
path += "\\*.*";
}
}
else
{
int offset = path.lastIndexOf('\\');
if(offset == -1)
{
basedir = FileSystem.getCurrentDirectory();
}
else
{
basedir = path.substring(0, offset);
}
}
}
}
if(basedir.equals("\\"))
{
basedir = "";
}
else if(basedir.endsWith("\\"))
{
basedir = basedir.substring(0, basedir.length());
}
System.out.println("List " + path);
System.out.println();
FileEnumerator e = new FileEnumerator(path);
try
{
int file_count = 0;
int directory_count = 0;
long length;
long total_length = 0;
String slength;
String attributes;
int pad_length;
File file;
String filename;
while(e.hasMoreFiles())
{
filename = e.getName();
try
{
file = new File(basedir + "\\" + filename);
System.out.print(new Date(file.getLastModifiedTimeEx()).toString());
attributes = getAttributes(file);
System.out.print(" " + attributes + PAD.substring(0, 5 - attributes.length()));
if(filename.equals(".") || filename.equals("..") || file.getAttributes() == FileAttribute.DIRECTORY)
{
directory_count++;
System.out.print(PAD.substring(0, 18));
}
else
{
file_count++;
length = file.length();
slength = String.valueOf(length);
System.out.print(PAD.substring(0, 18 - slength.length()) + slength);
total_length += length;
}
System.out.println(" " + filename);
}
catch(Exception ex)
{
System.out.println(filename + " is not accessible.");
}
e.getNextFile();
}
System.out.println();
System.out.println(file_count + " File(s) " + total_length + " bytes");
System.out.println(directory_count + " Dir(s)");
}
finally
{
e.close();
e = null;
}
}
private static String getAttributes(File file) throws Exception
{
int attributes = file.getAttributes();
StringBuffer sb = new StringBuffer();
if((attributes & FileAttribute.ARCHIVE) == FileAttribute.ARCHIVE)
{
sb.append("A");
}
if((attributes & FileAttribute.COMPRESSED) == FileAttribute.COMPRESSED)
{
sb.append("C");
}
if((attributes & FileAttribute.DIRECTORY) == FileAttribute.DIRECTORY)
{
sb.append("D");
}
if((attributes & FileAttribute.ENCRYPTED) == FileAttribute.ENCRYPTED)
{
sb.append("E");
}
if((attributes & FileAttribute.HIDDEN) == FileAttribute.HIDDEN)
{
sb.append("H");
}
if((attributes & FileAttribute.NORMAL) == FileAttribute.NORMAL)
{
sb.append("N");
}
if((attributes & FileAttribute.OFFLINE) == FileAttribute.OFFLINE)
{
sb.append("O");
}
if((attributes & FileAttribute.READONLY) == FileAttribute.READONLY)
{
sb.append("R");
}
if((attributes & FileAttribute.REPARSE_POINT) == FileAttribute.REPARSE_POINT)
{
sb.append("P");
}
if((attributes & FileAttribute.SPARSE_FILE) == FileAttribute.SPARSE_FILE)
{
sb.append("F");
}
if((attributes & FileAttribute.SYSTEM) == FileAttribute.SYSTEM)
{
sb.append("S");
}
if((attributes & FileAttribute.TEMPORARY) == FileAttribute.TEMPORARY)
{
sb.append("T");
}
return sb.toString();
}
}