public static void Demo_Dir_Walk_Simple(string path) { Console.WriteLine("\n--------------- Chapter 1.5 Dir_Walk_Simple ---------------"); Dir_Walk_Simple(path, Print_Dir); Console.WriteLine(); // dir-walk-simple called with lambdas - Higher Order Perl p. 19 Console.WriteLine("\n--------------- Chapter 1.5 Dir_Walk_Simple w/ lambda ---------------"); // Same as Print_Dir, but using an expression lambda Dir_Walk_Simple(path, (x) => Console.WriteLine(x)); Console.WriteLine(); Console.WriteLine("\n--------------- Chapter 1.5 Dir_Walk_Simple w/ sizes ---------------"); Dir_Walk_Simple(path, (x) => Console.WriteLine(DirOrFileSizeFormat, PerlFileOps.Size(x), x)); Console.WriteLine(); Console.WriteLine("\n--------------- Chapter 1.5 Dir_Walk_Simple that displays Broken Links/Shortcuts ---------------"); Dir_Walk_Simple(path, (x) => { if (PerlFileOps.Link(x, out string target) && !PerlFileOps.Exists(target)) // -l && -e { Console.WriteLine("'{0}' => '{1}'", x, target); } }); Console.WriteLine(); // dir-walk-simple used with an accumulator in the lambda - Higher Order Perl p. 20 Console.WriteLine("\n--------------- Chapter 1.5 Dir_Walk_Simple w/ total size ---------------"); long total = 0; Dir_Walk_Simple(path, (x) => total += PerlFileOps.Size(x)); Console.WriteLine("Total size of '{0}' is {1:N0}", path, total); Console.WriteLine(); }
public object Dir_Walk_Accumulator(string top) { if (PerlFileOps.IsDir(top)) // -d { FileSystemInfo[] filesAndDirs; try { filesAndDirs = (new DirectoryInfo(top)).GetFileSystemInfos(); } catch (Exception e) { Console.WriteLine("Couldn't open directory '{0}': {1} - skipping.", top, e.Message); return(null); } List <object> results = new List <object>(); foreach (FileSystemInfo file in filesAndDirs) { object r = Dir_Walk_Accumulator(file.FullName); if (r != null) { results.Add(r); } } return(Directory(top, results)); } else { return(File(top)); } }
// dir-walk-simple - Higher Order Perl pp. 17-20 public static void Dir_Walk_Simple(string top, ItemFunc code) { code(top); if (PerlFileOps.IsDir(top)) // -d { // There is not an equivalent to Perl's opendir/readir. The line below tries to read the files and directories // in directory "top" and will throw an exception if the directory with this name doesn't exist or has some // kind of access error FileSystemInfo[] filesAndDirs; try { filesAndDirs = (new DirectoryInfo(top)).GetFileSystemInfos(); } catch (Exception e) { Console.WriteLine("Couldn't open directory {0}: {1} - skipping.", top, e.Message); return; } foreach (FileSystemInfo file in filesAndDirs) { // System.IO doesn't return aliases like "." or ".." for any GetXXX calls // so we don't need code to exclude them Dir_Walk_Simple(file.FullName, code); } } }
// The Perl implementation of total_size starting on page 12 has a bug that does not occur when implementing // this in the straight-forward way in C# // total-size - Higher Order Perl pp. 8-15 public static long Total_Size(string top) { long total = PerlFileOps.Size(top); // -s if (PerlFileOps.IsFile(top)) { return(total); // -f } // There is not an equivalent to Perl's opendir/readir. The line below tries to read the files and directories // in directory "top" and will throw an exception if the directory with this name doesn't exist or has some // kind of access error FileSystemInfo[] files; try { files = (new DirectoryInfo(top)).GetFileSystemInfos(); } catch (Exception e) { Console.WriteLine("Couldn't open directory {0}: {1}; skipping.", top, e.Message); return(total); } foreach (FileSystemInfo file in files) { // System.IO doesn't return aliases like "." or ".." for any GetXXX calls // so we don't need code to exclude them total += Total_Size(file.FullName); } // Don't need to "closedir" the directory, it is freed when out of scope return(total); }
public override void FileOrDirectory(string path) { // Need to check that this is a file in case we have any wacky directory names like 'dirdir.jpg' if ((PerlFileOps.IsFile(path)) && (extensions.Contains(Path.GetExtension(path).ToLower()))) { Console.WriteLine(path); } }
// dangles example public static object Dangles(string file) { if (PerlFileOps.Link(file, out string target) && !PerlFileOps.Exists(target)) // -l && -e { Console.WriteLine("'{0}' => '{1}'", file, target); } return(null); }
public override void FileOrDirectory(string path) { string target; if (PerlFileOps.Link(path, out target) && !PerlFileOps.Exists(target)) // -l && -e { Console.WriteLine("'{0}' => '{1}'", path, target); } }
// dir-walk-sizehash - Higher Order Perl pp. 23-24 public static object File(string file) { List <object> al = new List <object> { Path.GetFileName(file), // We don't need a sub short{} equivalent since we have Path.GetFileName() PerlFileOps.Size(file) }; return(al); }
public override object File(string path) { List <object> l = new List <object> { Path.GetFileName(path), // We don't need a sub short{} equivalent since we have Path.GetFileName() PerlFileOps.Size(path) }; return(l); }
public override object File(string path) { if (PerlFileOps.Exists(path)) { return(PerlFileOps.Size(path)); } else { Console.WriteLine("'{0}' - no such file or directory exists.", path); return(0); } }
// dir-walk-cb - Higher Order Perl pp. 21-22 public static object Dir_Walk_CB(string top, FileFunc fileFunc, DirFunc dirFunc) { if (PerlFileOps.IsDir(top)) // -d { // There is not an equivalent to Perl's opendir/readir. The line below tries to read the files and directories // in directory "top" and will throw an exception if the directory with this name doesn't exist or has some // kind of access error FileSystemInfo[] filesAndDirs; try { filesAndDirs = (new DirectoryInfo(top)).GetFileSystemInfos(); } catch (Exception e) { Console.WriteLine("Couldn't open directory {0}: {1} - skipping.", top, e.Message); return(null); } // Using List<object> vs. ArrayList, references: // https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist?view=netframework-4.8#remarks // https://github.com/dotnet/platform-compat/blob/master/docs/DE0006.md List <object> results = new List <object>(); foreach (FileSystemInfo file in filesAndDirs) { // System.IO doesn't return aliases like "." or ".." for any GetXXX calls // so we don't need code to exclude them object r = Dir_Walk_CB(file.FullName, fileFunc, dirFunc); if (r != null) { results.Add(r); } } return(dirFunc(top, results)); } else { return(fileFunc(top)); } }
public void Dir_Walk(string top) { FileOrDirectory(top); if (PerlFileOps.IsDir(top)) // -d { FileSystemInfo[] filesAndDirs; try { filesAndDirs = (new DirectoryInfo(top)).GetFileSystemInfos(); } catch (Exception e) { Console.WriteLine("Couldn't open directory '{0}': {1} - skipping.", top, e.Message); return; } foreach (FileSystemInfo fileOrDir in filesAndDirs) { Dir_Walk(fileOrDir.FullName); } } }
// dir-walk-cb-def - Higher Order Perl p. 24 public static object Dir_Walk_CB_Def(string top, FileFunc fileFunc, DirFunc dirFunc) { if (PerlFileOps.IsDir(top)) // -d { // There is not an equivalent to Perl's opendir/readir. The line below tries to read the files and directories // in directory "top" and will throw an exception if the directory with this name doesn't exist or has some // kind of access error FileSystemInfo[] filesAndDirs; try { filesAndDirs = (new DirectoryInfo(top)).GetFileSystemInfos(); } catch (Exception e) { Console.WriteLine("Couldn't open directory {0}: {1} - skipping.", top, e.Message); return(null); } List <object> results = new List <object>(); foreach (FileSystemInfo file in filesAndDirs) { // System.IO doesn't return aliases like "." or ".." for any GetXXX calls // so we don't need code to exclude them object r = Dir_Walk_CB_Def(file.FullName, fileFunc, dirFunc); if (r != null) { results.Add(r); } } return((dirFunc != null) ? dirFunc(top, results) : null); } else { return((fileFunc != null) ? fileFunc(top) : null); } }
public override void FileOrDirectory(string path) { Console.WriteLine(DirOrFileSizeFormat, PerlFileOps.Size(path), path); }
public static object File_Size(string path) { return(PerlFileOps.Size(path)); }
public override object File(string path) { return(PerlFileOps.Size(path)); }