Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions crop/Crop/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ class Config



public static void WalkDirectoryTree(string root)
public static void WalkDirectoryTree(string root, int maxDepth)
{
Stack<string> dirs = new Stack<string>();
var currentDepth = 0;

Console.WriteLine("[*] Walking directory tree for: " + root);
Console.WriteLine("[*] Walking directory tree for: {0} until a depth of {1}", root, maxDepth > 0 ? maxDepth.ToString() : "infinity");
if (!System.IO.Directory.Exists(root))
{
Console.WriteLine("[!] Error, folder does not exist");
return;
}
dirs.Push(root);
folders.Add(root);

while (dirs.Count > 0)
while (dirs.Count > 0 && (maxDepth < 0 || currentDepth < maxDepth))
{

string currentDir = dirs.Pop();
string[] subDirs;
try
Expand All @@ -67,6 +67,9 @@ public static void WalkDirectoryTree(string root)
continue;
}

// Track depth
currentDepth += subDirs.Length == 0 ? -1 : 1;

// Push the subdirectories onto the stack for traversal.
// This could also be done before handing the files.
foreach (string str in subDirs)
Expand Down
18 changes: 14 additions & 4 deletions crop/Crop/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,27 @@ static void ShowHelp()
Console.WriteLine("crop.exe \\\\fileserver\\Common\\ crop.url \\\\workstation@8888\\harvest");
Console.WriteLine("\nOptional arguments:");
Console.WriteLine("--recurse : write the file to every sub folder of the specified path");
Console.WriteLine("--depth=X : number of sub folders deep of the specified path to write the file");
Console.WriteLine("--clean : remove the file from every sub folder of the specified path");
return;
}
static void ParseArgs(string[] args)
{
var recurse = false;
var clean = false;
var maxDepth = -1;
if (args.Contains("--recurse"))
recurse = true;
else if (args.Contains("--clean"))
clean = true;

if (Array.Exists(args, x => x.Contains("--depth=")))
{
if (!Int32.TryParse(args.Where(s => s.Contains("--depth=")).First().Split('=')[1], out maxDepth)) {
ShowHelp();
return;
}
}

Config.targetLocation = args[0].Trim();
Config.targetFilename = args[1].Trim();
Expand All @@ -59,7 +69,7 @@ static void ParseArgs(string[] args)
{
if (recurse)
{
Config.WalkDirectoryTree(Config.targetLocation);
Config.WalkDirectoryTree(Config.targetLocation, maxDepth);
foreach (var folder in Config.folders)
{
var f = folder;
Expand All @@ -72,7 +82,7 @@ static void ParseArgs(string[] args)
}
else if (clean)
{
Config.WalkDirectoryTree(Config.targetLocation);
Config.WalkDirectoryTree(Config.targetLocation, maxDepth);
foreach (var folder in Config.folders)
{
var f = folder;
Expand Down Expand Up @@ -106,7 +116,7 @@ static void ParseArgs(string[] args)

if (recurse)
{
Config.WalkDirectoryTree(Config.targetLocation);
Config.WalkDirectoryTree(Config.targetLocation, maxDepth);
foreach (var folder in Config.folders)
{
var f = folder;
Expand All @@ -119,7 +129,7 @@ static void ParseArgs(string[] args)
}
else if (clean)
{
Config.WalkDirectoryTree(Config.targetLocation);
Config.WalkDirectoryTree(Config.targetLocation, maxDepth);
foreach (var folder in Config.folders)
{
var f = folder;
Expand Down