Skip to content
Permalink
Browse files

first

  • Loading branch information...
Jeremy Heminger Jeremy Heminger
Jeremy Heminger authored and Jeremy Heminger committed Aug 15, 2019
1 parent f899176 commit 1e78f6fec07b4a9d90453f145a0d1c96763b8c72
Showing with 238 additions and 0 deletions.
  1. +1 −0 LoopIT/LoopIT.csproj
  2. +26 −0 LoopIT/Program.cs
  3. +211 −0 LoopIT/StackLoopDirs.cs
@@ -45,6 +45,7 @@
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StackLoopDirs.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
@@ -10,6 +10,32 @@ class Program
{
static void Main(string[] args)
{
string strArgTwo = "dir";
string strArgThree = "";

if (args.Length == 0)
{
Console.WriteLine("Program requires at least one argument");
Console.WriteLine("[1] - path to folder to loop");
Console.WriteLine("[2] - target directory or file [dir,file] optional - default is dir");
Console.WriteLine("[3] - directories to skip - can be a single value or list delimitted by commas");
}
else
{
if (args.Length >= 2)
strArgTwo = args[1];
if (args.Length == 3)
strArgThree = args[2];

StackLoopDirs sld = new StackLoopDirs();

if (strArgTwo == "dir")
sld.TraverseTreeDirs(args[0], strArgThree);

if (strArgTwo == "file")
sld.TraverseTreeFiles(args[0], strArgThree);

}
}
}
}
@@ -0,0 +1,211 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LoopIT
{
class StackLoopDirs
{
public void TraverseTreeDirs(string root, string skip)
{
// Data structure to hold names of subfolders to be
// examined for files.
Stack<string> dirs = new Stack<string>(20);

if (!System.IO.Directory.Exists(root))
{
throw new ArgumentException();
}
dirs.Push(root);

while (dirs.Count > 0)
{
string currentDir = dirs.Pop();

// try to protect the operation from hittin Windows file length max
if (this.CheckLength(currentDir, 190))
continue;

string[] subDirs;
try
{
subDirs = System.IO.Directory.GetDirectories(currentDir);
}
// An UnauthorizedAccessException exception will be thrown if we do not have
// discovery permission on a folder or file. It may or may not be acceptable
// to ignore the exception and continue enumerating the remaining files and
// folders. It is also possible (but unlikely) that a DirectoryNotFound exception
// will be raised. This will happen if currentDir has been deleted by
// another application or thread after our call to Directory.Exists. The
// choice of which exceptions to catch depends entirely on the specific task
// you are intending to perform and also on how much you know with certainty
// about the systems on which this code will run.
catch (UnauthorizedAccessException e)
{
Console.WriteLine(e.Message);
continue;
}
catch (System.IO.DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
continue;
}

// Push the subdirectories onto the stack for traversal.
// This could also be done before handing the files.
foreach (string str in subDirs)
{
// try to protect the operation from hittin Windows file length max
if (this.CheckLength(str, 250))
continue;

// skip dirs we don't need
if (this.CheckSkipDir(str, skip))
continue;

dirs.Push(str);
}
}
}
public void TraverseTreeFiles(string root, string skip)
{
// Data structure to hold names of subfolders to be
// examined for files.
Stack<string> dirs = new Stack<string>(20);

if (!System.IO.Directory.Exists(root))
{
throw new ArgumentException();
}
dirs.Push(root);

while (dirs.Count > 0)
{
string currentDir = dirs.Pop();

// try to protect the operation from hittin Windows file length max
if (this.CheckLength(currentDir, 190))
continue;

if (this.CheckSkipDir(currentDir, skip))
continue;

string[] subDirs;
try
{
subDirs = System.IO.Directory.GetDirectories(currentDir);
}
// An UnauthorizedAccessException exception will be thrown if we do not have
// discovery permission on a folder or file. It may or may not be acceptable
// to ignore the exception and continue enumerating the remaining files and
// folders. It is also possible (but unlikely) that a DirectoryNotFound exception
// will be raised. This will happen if currentDir has been deleted by
// another application or thread after our call to Directory.Exists. The
// choice of which exceptions to catch depends entirely on the specific task
// you are intending to perform and also on how much you know with certainty
// about the systems on which this code will run.
catch (UnauthorizedAccessException e)
{
Console.WriteLine(e.Message);
continue;
}
catch (System.IO.DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
continue;
}

string[] files = null;
try
{
files = System.IO.Directory.GetFiles(currentDir);
}

catch (UnauthorizedAccessException e)
{

Console.WriteLine(e.Message);
continue;
}

catch (System.IO.DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
continue;
}
// Perform the required action on each file here.
// Modify this block to perform your required task.
foreach (string file in files)
{
// try to protect the operation from hittin Windows file length max
if (this.CheckLength(file,250))
continue;

try
{
// Perform whatever action is required in your scenario.
System.IO.FileInfo fi = new System.IO.FileInfo(file);
Console.WriteLine("{0}: {1}, {2}", fi.Name, fi.Length, fi.CreationTime);
}
catch (System.IO.FileNotFoundException e)
{
// If file was deleted by a separate application
// or thread since the call to TraverseTree()
// then just continue.
Console.WriteLine(e.Message);
continue;
}
}

// Push the subdirectories onto the stack for traversal.
// This could also be done before handing the files.
foreach (string str in subDirs)
dirs.Push(str);
}
}
// try to protect against hitting Windows file length max
private Boolean CheckLength(string path, ushort strlen)
{
if(path.Length >= strlen)
{
return true;
}
return false;
}
private Boolean CheckSkipDir(string dir,string skip)
{
DirectoryInfo di = new DirectoryInfo(dir);
try
{
// Determine whether the directory exists.
if (di.Exists)
{
if (skip.IndexOf(",") > -1)
{
string[] skips = skip.Split(',');
foreach (string s in skips)
{
if (di.Name == s)
return true;
}
}
else
{
if (di.Name == skip)
return true;
}
Console.WriteLine("{0}: {1}", di.FullName, di.Name);
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
return false;
}
}

}

0 comments on commit 1e78f6f

Please sign in to comment.
You can’t perform that action at this time.