/* * [FS.java] * * Summary: Gets info about all instances of a file or wildcard in the current tree. * * Copyright: (c) 2016-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2016-11-10 initial release * 1.1 2016-12-09 make patterns case-insensitive */ package com.mindprod.fs; import com.mindprod.common18.EIO; import com.mindprod.common18.ST; import com.mindprod.filter.OnlyDirectoriesFilter; import com.mindprod.filter.WildcardFilter; import java.io.File; import java.io.IOException; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import static java.lang.System.*; /** * Gets info about all instances of a file or wildcard in the current tree. * * @author Roedy Green, Canadian Mind Products * @version 1.1 2016-12-09 make patterns case-insensitive * @since 2016-11-10 */ public class FS { /** * Modified Zulu format mask 2008-06-22T07:57+46.438 */ private static final SimpleDateFormat SDF = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm'+'ss'.'SSS " ); /** * undisplayed copyright notice */ @SuppressWarnings( { "UnusedDeclaration" } ) private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 2016-2017 Roedy Green, Canadian Mind Products, http://mindprod.com"; /** * date this version was released. */ @SuppressWarnings( { "UnusedDeclaration" } ) private static final String RELEASE_DATE = "2016-12-09"; private static final int FIRST_COPYRIGHT_YEAR = 2016; /** * how to use the command line */ private static final String USAGE = "\nFS requires unqualified file name, or wildcard in quotes."; /** * embedded version string. */ @SuppressWarnings( { "UnusedDeclaration" } ) private static final String VERSION_STRING = "1.1"; private static final OnlyDirectoriesFilter DIRS_ONLY = new OnlyDirectoriesFilter(); private static final DecimalFormat SIZE_FORMAT = new DecimalFormat( "#,##0 " ); /** * Scan file for all occurrences of the file. * * @param dir to start the recursive search * * @throws IOException if trouble reading directory tree. */ private static void scanDirForFile( File dir, String filename ) throws IOException { final File hit = new File( dir, filename ); if ( hit.exists() ) { // output stats on this file. // format length final long length = hit.length(); final String lengthDisplay = ST.leftPad( SIZE_FORMAT.format( length ), 13, false ); // format date final long date = hit.lastModified(); final String dateDisplay = SDF.format( date ); out.println( lengthDisplay + dateDisplay + EIO.getCanOrAbsPath( hit ) ); } // continue scanning nested dirs final String[] dirs = dir.list( DIRS_ONLY ); if ( dirs != null ) { for ( String subdir : dirs ) { scanDirForFile( new File( dir, subdir ), filename ); } } } // end scanFile /** * Scan file for all occurrences of the wildcard. * * @param dir to start the recursive search * * @throws IOException if trouble reading directory tree. */ private static void scanDirForWildcard( File dir, WildcardFilter wildcardFilter ) throws IOException { final String[] fileHits = dir.list( wildcardFilter ); if ( fileHits != null ) { for ( String i : fileHits ) { // output stats on this file. // format length final File hit = new File( dir, i ); final long length = hit.length(); final String lengthDisplay = ST.leftPad( SIZE_FORMAT.format( length ), 13, false ); // format date final long date = hit.lastModified(); final String dateDisplay = SDF.format( date ); out.println( lengthDisplay + dateDisplay + EIO.getCanOrAbsPath( hit ) ); } } // continue scanning nested dirs final String[] dirs = dir.list( DIRS_ONLY ); if ( dirs != null ) { for ( String subdir : dirs ) { scanDirForWildcard( new File( dir, subdir ), wildcardFilter ); } } } /** * compacts HTML files. * * @param args regex, output file, then names of files to process, dirs, files, -s, *.*, no wildcards. */ public static void main( String[] args ) { if ( args.length != 1 ) { err.println( USAGE ); for ( String arg : args ) { err.print( "[" + arg + "] " ); } err.println(); System.exit( 2 ); } // will throw an detailed exception if this is not a valid string. final String filename = args[ 0 ]; try { if ( filename.indexOf( '*' ) >= 0 || filename.indexOf( '?' ) >= 0 ) { scanDirForWildcard( new File( "." ), new WildcardFilter( filename ) ); } else { scanDirForFile( new File( "." ), filename ); } } catch ( IOException e ) { err.println( "io error scanning for " + filename ); } } // end main }