Version 2.1
This commit is contained in:
@ -6,6 +6,8 @@ using System.Drawing;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using CommandLine.Utility;
|
||||
using System.IO;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Snap2HTML
|
||||
{
|
||||
@ -13,161 +15,45 @@ namespace Snap2HTML
|
||||
{
|
||||
private void backgroundWorker_DoWork( object sender, DoWorkEventArgs e )
|
||||
{
|
||||
backgroundWorker.ReportProgress( 0, "Reading folders..." );
|
||||
var sbDirArrays = new StringBuilder();
|
||||
int prevDepth = -100;
|
||||
|
||||
// Get all folders
|
||||
var dirs = new List<string>();
|
||||
dirs.Insert( 0, txtRoot.Text );
|
||||
var skipHidden = ( chkHidden.CheckState == CheckState.Unchecked );
|
||||
var skipSystem = ( chkSystem.CheckState == CheckState.Unchecked );
|
||||
DirSearch( txtRoot.Text, dirs, skipHidden, skipSystem );
|
||||
dirs = SortDirList( dirs );
|
||||
|
||||
// Get files & folders
|
||||
var content = GetContent( txtRoot.Text, skipHidden, skipSystem );
|
||||
if( content == null )
|
||||
{
|
||||
backgroundWorker.ReportProgress( 0, "Error reading source" );
|
||||
return;
|
||||
}
|
||||
if( backgroundWorker.CancellationPending )
|
||||
{
|
||||
backgroundWorker.ReportProgress( 0, "User cancelled" );
|
||||
return;
|
||||
}
|
||||
|
||||
int totDirs = 0;
|
||||
dirs.Add( "*EOF*" );
|
||||
long totSize = 0;
|
||||
int totFiles = 0;
|
||||
var lineBreakSymbol = ""; // could set to \n to make html more readable at the expense of increased size
|
||||
|
||||
// Get files in folders
|
||||
for( int d = 0; d < dirs.Count; d++ )
|
||||
// Convert to string with JS data object
|
||||
var jsContent = BuildJavascriptContentArray( content, 0 );
|
||||
if( backgroundWorker.CancellationPending )
|
||||
{
|
||||
string currentDir = dirs[d];
|
||||
|
||||
try
|
||||
{
|
||||
int newDepth = currentDir.Split( System.IO.Path.DirectorySeparatorChar ).Length;
|
||||
if( currentDir.Length < 64 && currentDir == System.IO.Path.GetPathRoot( currentDir ) ) newDepth--; // fix reading from rootfolder, <64 to avoid going over MAX_PATH
|
||||
|
||||
prevDepth = newDepth;
|
||||
|
||||
var sbCurrentDirArrays = new StringBuilder();
|
||||
|
||||
if( currentDir != "*EOF*" )
|
||||
{
|
||||
bool no_problem = true;
|
||||
|
||||
try
|
||||
{
|
||||
var files = new List<string>( System.IO.Directory.GetFiles( currentDir, "*.*", System.IO.SearchOption.TopDirectoryOnly ) );
|
||||
files.Sort();
|
||||
int f = 0;
|
||||
|
||||
string last_write_date = "-";
|
||||
last_write_date = System.IO.Directory.GetLastWriteTime( currentDir ).ToLocalTime().ToString();
|
||||
long dir_size = 0;
|
||||
|
||||
sbCurrentDirArrays.Append( "D.p([" + lineBreakSymbol );
|
||||
var sDirWithForwardSlash = currentDir.Replace( @"\", "/" );
|
||||
sbCurrentDirArrays.Append( "\"" ).Append( MakeCleanJsString( sDirWithForwardSlash ) ).Append( "*" ).Append( dir_size ).Append( "*" ).Append( last_write_date ).Append( "\"," + lineBreakSymbol );
|
||||
f++;
|
||||
long dirSize = 0;
|
||||
foreach( string sFile in files )
|
||||
{
|
||||
bool bInclude = true;
|
||||
long fi_length = 0;
|
||||
last_write_date = "-";
|
||||
try
|
||||
{
|
||||
System.IO.FileInfo fi = new System.IO.FileInfo( sFile );
|
||||
if( ( fi.Attributes & System.IO.FileAttributes.Hidden ) == System.IO.FileAttributes.Hidden && chkHidden.CheckState != CheckState.Checked ) bInclude = false;
|
||||
if( ( fi.Attributes & System.IO.FileAttributes.System ) == System.IO.FileAttributes.System && chkSystem.CheckState != CheckState.Checked ) bInclude = false;
|
||||
fi_length = fi.Length;
|
||||
|
||||
try
|
||||
{
|
||||
last_write_date = fi.LastWriteTime.ToLocalTime().ToString();
|
||||
}
|
||||
catch( Exception ex )
|
||||
{
|
||||
Console.WriteLine( "{0} Exception caught.", ex );
|
||||
}
|
||||
}
|
||||
catch( Exception ex )
|
||||
{
|
||||
Console.WriteLine( "{0} Exception caught.", ex );
|
||||
bInclude = false;
|
||||
}
|
||||
|
||||
if( bInclude )
|
||||
{
|
||||
sbCurrentDirArrays.Append( "\"" ).Append( MakeCleanJsString( System.IO.Path.GetFileName( sFile ) ) ).Append( "*" ).Append( fi_length ).Append( "*" ).Append( last_write_date ).Append( "\"," + lineBreakSymbol );
|
||||
totSize += fi_length;
|
||||
dirSize += fi_length;
|
||||
totFiles++;
|
||||
f++;
|
||||
|
||||
if( totFiles % 9 == 0 )
|
||||
{
|
||||
backgroundWorker.ReportProgress( 0, "Reading files... " + totFiles + " (" + sFile + ")" );
|
||||
}
|
||||
|
||||
}
|
||||
if( backgroundWorker.CancellationPending )
|
||||
{
|
||||
backgroundWorker.ReportProgress( 0, "Operation Cancelled!" );
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Add total dir size
|
||||
sbCurrentDirArrays.Append( "" ).Append( dirSize ).Append( "," + lineBreakSymbol );
|
||||
|
||||
// Add subfolders
|
||||
string subdirs = "";
|
||||
List<string> lstSubDirs = new List<string>( System.IO.Directory.GetDirectories( currentDir ) );
|
||||
lstSubDirs = SortDirList( lstSubDirs );
|
||||
foreach( string sTmp in lstSubDirs )
|
||||
{
|
||||
int i = dirs.IndexOf( sTmp );
|
||||
if( i != -1 ) subdirs += i + "*";
|
||||
}
|
||||
if( subdirs.EndsWith( "*" ) ) subdirs = subdirs.Remove( subdirs.Length - 1 );
|
||||
sbCurrentDirArrays.Append( "\"" ).Append( subdirs ).Append( "\"" + lineBreakSymbol ); // subdirs
|
||||
sbCurrentDirArrays.Append( "])" );
|
||||
sbCurrentDirArrays.Append( "\n" );
|
||||
}
|
||||
catch( Exception ex )
|
||||
{
|
||||
Console.WriteLine( "{0} Exception caught.", ex );
|
||||
no_problem = false;
|
||||
}
|
||||
|
||||
if( no_problem == false ) // We need to keep folder even if error occurred for integrity
|
||||
{
|
||||
var sDirWithForwardSlash = currentDir.Replace( @"\", "/" );
|
||||
sbCurrentDirArrays = new StringBuilder();
|
||||
sbCurrentDirArrays.Append( "D.p([\"" ).Append( MakeCleanJsString( sDirWithForwardSlash ) ).Append( "*0*-\"," + lineBreakSymbol );
|
||||
sbCurrentDirArrays.Append( "0," + lineBreakSymbol ); // total dir size
|
||||
sbCurrentDirArrays.Append( "\"\"" + lineBreakSymbol ); // subdirs
|
||||
sbCurrentDirArrays.Append( "])\n" );
|
||||
no_problem = true;
|
||||
}
|
||||
|
||||
if( no_problem )
|
||||
{
|
||||
sbDirArrays.Append( sbCurrentDirArrays.ToString() );
|
||||
totDirs++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch( System.Exception ex )
|
||||
{
|
||||
Console.WriteLine( "{0} exception caught: {1}", ex, ex.Message );
|
||||
}
|
||||
|
||||
backgroundWorker.ReportProgress( 0, "User cancelled" );
|
||||
return;
|
||||
}
|
||||
|
||||
// -- Generate Output --
|
||||
// Calculate some stats
|
||||
int totDirs = 0;
|
||||
int totFiles = 0;
|
||||
long totSize = 0;
|
||||
foreach( var folder in content )
|
||||
{
|
||||
totDirs++;
|
||||
foreach( var file in folder.Files )
|
||||
{
|
||||
totFiles++;
|
||||
totSize += Int64.Parse( file.GetProp( "Size" ) );
|
||||
}
|
||||
}
|
||||
|
||||
// Let's generate the output
|
||||
|
||||
backgroundWorker.ReportProgress( 0, "Generating HTML file..." );
|
||||
|
||||
@ -188,7 +74,7 @@ namespace Snap2HTML
|
||||
}
|
||||
|
||||
// Build HTML
|
||||
sbContent.Replace( "[DIR DATA]", sbDirArrays.ToString() );
|
||||
sbContent.Replace( "[DIR DATA]", jsContent );
|
||||
sbContent.Replace( "[TITLE]", txtTitle.Text );
|
||||
sbContent.Replace( "[APP LINK]", "http://www.rlvision.com" );
|
||||
sbContent.Replace( "[APP NAME]", Application.ProductName );
|
||||
@ -264,5 +150,285 @@ namespace Snap2HTML
|
||||
Application.Exit();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
public class SnappedFile
|
||||
{
|
||||
public SnappedFile( string name )
|
||||
{
|
||||
this.Name = name;
|
||||
this.Properties = new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
public string Name { get; set; }
|
||||
public Dictionary<string, string> Properties { get; set; }
|
||||
|
||||
public string GetProp( string key )
|
||||
{
|
||||
if( this.Properties.ContainsKey( key ) )
|
||||
return this.Properties[key];
|
||||
else
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class SnappedFolder
|
||||
{
|
||||
public SnappedFolder( string name, string path )
|
||||
{
|
||||
this.Name = name;
|
||||
this.Path = path;
|
||||
this.Properties = new Dictionary<string, string>();
|
||||
this.Files = new List<SnappedFile>();
|
||||
this.FullPath = ( this.Path + "\\" + this.Name ).Replace( "\\\\", "\\" );
|
||||
}
|
||||
|
||||
public string Name { get; set; }
|
||||
public string Path { get; set; }
|
||||
public string FullPath { get; set; }
|
||||
public Dictionary<string, string> Properties { get; set; }
|
||||
public List<SnappedFile> Files { get; set; }
|
||||
|
||||
public string GetProp( string key )
|
||||
{
|
||||
if( this.Properties.ContainsKey( key ) )
|
||||
return this.Properties[key];
|
||||
else
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
private List<SnappedFolder> GetContent( string rootFolder, bool skipHidden, bool skipSystem )
|
||||
{
|
||||
var stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
|
||||
var result = new List<SnappedFolder>();
|
||||
|
||||
// Get all folders
|
||||
var dirs = new List<string>();
|
||||
dirs.Insert( 0, rootFolder );
|
||||
DirSearch( rootFolder, dirs, skipHidden, skipSystem, stopwatch );
|
||||
dirs = SortDirList( dirs );
|
||||
|
||||
if( backgroundWorker.CancellationPending )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var totFiles = 0;
|
||||
|
||||
stopwatch.Restart();
|
||||
|
||||
try
|
||||
{
|
||||
string modified_date;
|
||||
string created_date;
|
||||
|
||||
// Parse each folder
|
||||
for( int d = 0; d < dirs.Count; d++ )
|
||||
{
|
||||
// Get folder properties
|
||||
var dirName = dirs[d];
|
||||
var currentDir = new SnappedFolder( Path.GetFileName( dirName ), Path.GetDirectoryName( dirName ) );
|
||||
if( dirName == Path.GetPathRoot( dirName ) )
|
||||
{
|
||||
currentDir = new SnappedFolder( "", dirName );
|
||||
}
|
||||
|
||||
modified_date = "";
|
||||
created_date = "";
|
||||
try
|
||||
{
|
||||
modified_date = ToUnixTimestamp(System.IO.Directory.GetLastWriteTime( dirName ).ToLocalTime()).ToString();
|
||||
created_date = ToUnixTimestamp( System.IO.Directory.GetCreationTime( dirName ).ToLocalTime() ).ToString();
|
||||
}
|
||||
catch( Exception ex )
|
||||
{
|
||||
Console.WriteLine( "{0} Exception caught.", ex );
|
||||
}
|
||||
currentDir.Properties.Add( "Modified", modified_date );
|
||||
currentDir.Properties.Add( "Created", created_date );
|
||||
|
||||
|
||||
// Get files in folder
|
||||
List<string> files;
|
||||
try
|
||||
{
|
||||
files = new List<string>( System.IO.Directory.GetFiles( dirName, "*.*", System.IO.SearchOption.TopDirectoryOnly ) );
|
||||
}
|
||||
catch( Exception ex )
|
||||
{
|
||||
Console.WriteLine( "{0} Exception caught.", ex );
|
||||
result.Add( currentDir );
|
||||
continue;
|
||||
}
|
||||
files.Sort();
|
||||
|
||||
// Get file properties
|
||||
foreach( string sFile in files )
|
||||
{
|
||||
totFiles++;
|
||||
if(stopwatch.ElapsedMilliseconds >= 50)
|
||||
{
|
||||
backgroundWorker.ReportProgress( 0, "Reading files... " + totFiles + " (" + sFile + ")" );
|
||||
stopwatch.Restart();
|
||||
}
|
||||
|
||||
if( backgroundWorker.CancellationPending )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var currentFile = new SnappedFile( Path.GetFileName( sFile ) );
|
||||
try
|
||||
{
|
||||
System.IO.FileInfo fi = new System.IO.FileInfo( sFile );
|
||||
var isHidden = ( fi.Attributes & System.IO.FileAttributes.Hidden ) == System.IO.FileAttributes.Hidden;
|
||||
var isSystem = ( fi.Attributes & System.IO.FileAttributes.System ) == System.IO.FileAttributes.System;
|
||||
|
||||
if( ( isHidden && skipHidden ) || ( isSystem && skipSystem ) )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
currentFile.Properties.Add( "Size", fi.Length.ToString() );
|
||||
|
||||
modified_date = "-";
|
||||
created_date = "-";
|
||||
try
|
||||
{
|
||||
modified_date = ToUnixTimestamp( fi.LastWriteTime.ToLocalTime() ).ToString();
|
||||
created_date = ToUnixTimestamp( fi.CreationTime.ToLocalTime() ).ToString();
|
||||
}
|
||||
catch( Exception ex )
|
||||
{
|
||||
Console.WriteLine( "{0} Exception caught.", ex );
|
||||
}
|
||||
|
||||
currentFile.Properties.Add( "Modified", modified_date );
|
||||
currentFile.Properties.Add( "Created", created_date );
|
||||
|
||||
}
|
||||
catch( Exception ex )
|
||||
{
|
||||
Console.WriteLine( "{0} Exception caught.", ex );
|
||||
}
|
||||
|
||||
currentDir.Files.Add( currentFile );
|
||||
}
|
||||
|
||||
result.Add( currentDir );
|
||||
}
|
||||
}
|
||||
catch( System.Exception ex )
|
||||
{
|
||||
Console.WriteLine( "{0} exception caught: {1}", ex, ex.Message );
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private string BuildJavascriptContentArray(List<SnappedFolder> content, int startIndex)
|
||||
{
|
||||
// Data format:
|
||||
// Each index in "dirs" array is an array representing a directory:
|
||||
// First item in array: "directory path*always 0*directory modified date"
|
||||
// Note that forward slashes are used instead of (Windows style) backslashes
|
||||
// Then, for each each file in the directory: "filename*size of file*file modified date"
|
||||
// Second to last item in array tells the total size of directory content
|
||||
// Last item in array refrences IDs to all subdirectories of this dir (if any).
|
||||
// ID is the item index in dirs array.
|
||||
// Note: Modified date is in UNIX format
|
||||
|
||||
backgroundWorker.ReportProgress( 0, "Processing content..." );
|
||||
|
||||
var result = new StringBuilder();
|
||||
|
||||
var lineBreakSymbol = ""; // Could be set to \n to make the html output more readable, at the expense of increased size
|
||||
|
||||
|
||||
// Assign an ID to each folder. This is equal to the index in the JS data array
|
||||
var dirIndexes = new Dictionary<string, string>();
|
||||
for(var i=0; i<content.Count; i++)
|
||||
{
|
||||
dirIndexes.Add( content[i].FullPath, ( i + startIndex ).ToString() );
|
||||
}
|
||||
|
||||
// Build a lookup table with subfolder IDs for each folder
|
||||
var subdirs = new Dictionary<string, List<string>>();
|
||||
foreach( var dir in content )
|
||||
{
|
||||
subdirs.Add( dir.FullPath, new List<string>() );
|
||||
}
|
||||
if( !subdirs.ContainsKey( content[0].Path ) && content[0].Name != "" )
|
||||
{
|
||||
subdirs.Add( content[0].Path, new List<string>() );
|
||||
}
|
||||
foreach( var dir in content )
|
||||
{
|
||||
if( dir.Name != "" )
|
||||
{
|
||||
try
|
||||
{
|
||||
subdirs[dir.Path].Add( dirIndexes[dir.FullPath] );
|
||||
}
|
||||
catch( Exception ex )
|
||||
{
|
||||
// orphan file or folder?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Generate the data array
|
||||
|
||||
foreach( var currentDir in content )
|
||||
{
|
||||
var sbCurrentDirArrays = new StringBuilder();
|
||||
sbCurrentDirArrays.Append( "D.p([" + lineBreakSymbol );
|
||||
|
||||
var sDirWithForwardSlash = currentDir.FullPath.Replace( @"\", "/" );
|
||||
sbCurrentDirArrays.Append( "\"" ).Append( MakeCleanJsString( sDirWithForwardSlash ) ).Append( "*" ).Append( "0" ).Append( "*" ).Append( currentDir.GetProp("Modified") ).Append( "\"," + lineBreakSymbol );
|
||||
|
||||
long dirSize = 0;
|
||||
|
||||
foreach( var currentFile in currentDir.Files )
|
||||
{
|
||||
sbCurrentDirArrays.Append( "\"" ).Append( MakeCleanJsString( currentFile.Name ) ).Append( "*" ).Append( currentFile.GetProp( "Size" ) ).Append( "*" ).Append( currentFile.GetProp("Modified") ).Append( "\"," + lineBreakSymbol );
|
||||
try
|
||||
{
|
||||
dirSize += Int64.Parse( currentFile.GetProp("Size") );
|
||||
}
|
||||
catch( Exception ex)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
// Add total dir size
|
||||
sbCurrentDirArrays.Append( "" ).Append( dirSize ).Append( "," + lineBreakSymbol );
|
||||
|
||||
sbCurrentDirArrays.Append( "\"" ).Append( String.Join( "*", subdirs[currentDir.FullPath].ToArray() ) ).Append( "\"" + lineBreakSymbol ); // subdirs
|
||||
|
||||
// Finalize
|
||||
sbCurrentDirArrays.Append( "])" );
|
||||
sbCurrentDirArrays.Append( "\n" );
|
||||
result.Append( sbCurrentDirArrays.ToString() );
|
||||
|
||||
if( backgroundWorker.CancellationPending )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return result.ToString();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user