Advertisement
kyrathasoft

com_wms_dirs.cs

Jun 9th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.25 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4.  
  5. namespace com.wms.dirs{
  6.    
  7.     /*
  8.         + https://gist.github.com/kyrathasoft/aca4a458ce58edb05f4476a8186cae33
  9.     */
  10.    
  11.     public class ClassDirs{
  12.            
  13.         public static int CountDirectoriesRecursively(string targetDirectory)
  14.         {
  15.             int iDirs = 0; //if targetDirectory exists, returns total # of all subdirectories, even nested ones
  16.            
  17.             if(Directory.Exists(targetDirectory)){
  18.                 iDirs = Directory.GetDirectories(targetDirectory, "*", SearchOption.AllDirectories).Length;
  19.             }
  20.            
  21.             return iDirs;
  22.         }          
  23.                
  24.         public static bool CopyTo(string targetDirectory, string destinationDirectory)
  25.         {
  26.             bool blnSuccess = false;
  27.            
  28.             try{
  29.                 if(Directory.Exists(targetDirectory)){
  30.                     foreach (string dirPath in Directory.GetDirectories(targetDirectory, "*",
  31.                         SearchOption.AllDirectories)){
  32.                         Directory.CreateDirectory(dirPath.Replace(targetDirectory, destinationDirectory)); 
  33.                     }
  34.                     //Copy all the files & Replaces any files with the same name
  35.                     foreach (string newPath in Directory.GetFiles(targetDirectory, "*.*",
  36.                         SearchOption.AllDirectories)){
  37.                         File.Copy(newPath, newPath.Replace(targetDirectory, destinationDirectory), true);
  38.                         blnSuccess = true;
  39.                     }                      
  40.                 }              
  41.             }catch(Exception exCopyTo){
  42.                 Console.WriteLine("Error in ioLibrary: " + exCopyTo.Message);
  43.             }
  44.            
  45.             return blnSuccess;
  46.            
  47.         }
  48.        
  49.         public static int DirectoryDepth(string someValidPath)
  50.         {
  51.             int depth = -1;        
  52.            
  53.             if(!IsValidPath(someValidPath)){
  54.                 return depth;
  55.             }else{
  56.                 Console.Write(" Calculating directory depth... ");
  57.                 char[] splitter = new char[]{'\\'};
  58.                 string[] tokens = someValidPath.Split(splitter, StringSplitOptions.RemoveEmptyEntries);
  59.                 Console.Write("{0} deep\n", tokens.Length);
  60.                
  61.                 for(int i=0; i < tokens.Length; i++){
  62.                     if(i % 2 == 0 && (i != 0)){
  63.                         Console.WriteLine("\tDepth {0}: {1}; ", (i + 1), tokens[i]);
  64.                     }else{
  65.                         Console.Write("\tDepth {0}: {1}; ", (i + 1), tokens[i]);
  66.                     }
  67.                 }
  68.                 Console.WriteLine("");
  69.                
  70.             }
  71.                        
  72.             return depth;
  73.         }      
  74.  
  75.         public static int GetDirectoryFileCount(string path)
  76.         {
  77.             int fileCount = 0;
  78.            
  79.             if(Directory.Exists(path))
  80.             {
  81.                 fileCount = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Length;
  82.             }
  83.            
  84.             return fileCount;
  85.         }      
  86.        
  87.         public static List<string> GetListOfSubdirectoryPaths(string path){
  88.             List<string> listing = new List<string>();
  89.             DirectoryInfo di = new DirectoryInfo(path);
  90.             DirectoryInfo[] directories = di.GetDirectories("*.*", SearchOption.AllDirectories);
  91.             foreach(DirectoryInfo dinfo in directories){
  92.                 listing.Add(dinfo.FullName);
  93.             }
  94.             return listing;
  95.         }
  96.        
  97.         public static string GetPathofCodeBase(){
  98.             string result = string.Empty;
  99.             string sPath = (System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
  100.             sPath = sPath.Substring(8, sPath.Length - 8);
  101.             sPath = Path.GetDirectoryName(sPath);
  102.             result = sPath;
  103.             return result;
  104.         }
  105.        
  106.         //kept for backward compatibility: for instance, cproggen.cs needs it...
  107.         public static string GetPathPortionOfFullPath(string fullpath)
  108.         {
  109.             string result = string.Empty;
  110.             if(IsValidPath(fullpath)){                
  111.                 string sFilename = Path.GetFileName(fullpath);
  112.                 int index = fullpath.IndexOf(sFilename);
  113.                 result = fullpath.Substring(0, index);
  114.                 }else{
  115.                     result = "A valid path wasn't passed into GetPathPortionOfFullPath()...";
  116.                 }
  117.             return result;
  118.         }      
  119.        
  120.         public static string GetPathPortionOfFullPathToFile(string fullpathToFile)
  121.         {
  122.             string result = string.Empty;
  123.             if(File.Exists(fullpathToFile)){
  124.                 string sFilename = Path.GetFileName(fullpathToFile);
  125.                 int index = fullpathToFile.IndexOf(sFilename);
  126.                 result = fullpathToFile.Substring(0, index);
  127.             }else{
  128.                 //leave empty string assigned to result
  129.             }
  130.             return result;
  131.         }
  132.        
  133.         public static bool IsValidPath(string path, bool allowRelativePaths = false)
  134.         {
  135.             bool isValid = true;
  136.  
  137.             try
  138.             {
  139.                 string fullPath = Path.GetFullPath(path);
  140.  
  141.                 if (allowRelativePaths)
  142.                 {
  143.                     isValid = Path.IsPathRooted(path);
  144.                 }
  145.                 else
  146.                 {
  147.                     string root = Path.GetPathRoot(path);
  148.                     isValid = string.IsNullOrEmpty(root.Trim(new char[] { '\\', '/' })) == false;
  149.                 }
  150.             }
  151.             catch(Exception ex)
  152.             {
  153.                 isValid = false;
  154.                 Console.WriteLine("\n " + ex.Message + "\n\nMessage originated in dirs.dll method IsValidPath()...");
  155.             }
  156.  
  157.             return isValid;
  158.         }      
  159.  
  160.         public static bool SpecifiedDirectoryContainsTheseFiles(string targetDirectory, List<string> _fileList)
  161.         {
  162.             bool result = false;
  163.             int sought_count = _fileList.Count;
  164.             if(Directory.Exists(targetDirectory)){
  165.                 string[] files = Directory.GetFiles(targetDirectory);
  166.                 int iCount = 0;
  167.                 foreach(string p in _fileList){
  168.                     for(int i=0; i < files.Length; i++){
  169.                         string sName = Path.GetFileName(files[i]);
  170.                         if(sName == p){ iCount++; }
  171.                     }
  172.                 }
  173.                 if(iCount == sought_count){ result = true; }
  174.             }
  175.            
  176.             return result;
  177.         }          
  178.            
  179.     }      
  180.    
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement