Skip to content

Commit 111ffe5

Browse files
committed
When a new directory appears, grab all of its files immediately
1 parent 2398095 commit 111ffe5

1 file changed

Lines changed: 34 additions & 18 deletions

File tree

Program.cs

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,45 +48,61 @@ private static void Main(string[] args)
4848

4949
private static void SomethingHappened(object sender, FileSystemEventArgs e)
5050
{
51-
//the FileWatcher won't give us a new one until
52-
//we return. Since timing is the whole point here,
53-
//we spawn a thread to try and grab that file and return quickly.
54-
var t = new Thread(() => CampOnFile(e));
51+
// if a new directory was created or renamed, camp on all of its files
52+
if (Directory.Exists(e.FullPath))
53+
{
54+
try
55+
{
56+
foreach (var f in Directory.GetFiles(e.FullPath))
57+
{
58+
var x = new Thread(() => CampOnFile(f, e.ChangeType));
59+
x.Start();
60+
}
61+
}
62+
catch (Exception err)
63+
{
64+
Print(ConsoleColor.Red, " Error trying to camp on all files in: " + e.FullPath);
65+
Print(ConsoleColor.Red, " " + err.Message);
66+
}
67+
return;
68+
}
69+
70+
//the FileWatcher won't give us a new one until
71+
//we return. Since timing is the whole point here,
72+
//we spawn a thread to try and grab that file and return quickly.
73+
var t = new Thread(() => CampOnFile(e.FullPath, e.ChangeType));
5574
t.Start();
5675
}
5776

58-
private static void CampOnFile(FileSystemEventArgs e)
77+
private static void CampOnFile(string fullPath, WatcherChangeTypes changeType)
5978
{
60-
if(Directory.Exists(e.FullPath))
61-
return; //it's not a file
62-
63-
var filename = Path.GetFileName(e.FullPath);
79+
var filename = Path.GetFileName(fullPath);
6480

6581
var extension = Path.GetExtension(filename);
6682
if (s_extensionsToIgnore.Contains(extension.ToLowerInvariant()))
6783
return;
6884

69-
if (s_filesInProcess.Contains(e.FullPath))
85+
if (s_filesInProcess.Contains(fullPath))
7086
{
7187
//Print(ConsoleColor.Gray, " Already processing: " + filename);
7288
return;
7389
}
7490
else
7591
{
76-
s_filesInProcess.Add(e.FullPath);
92+
s_filesInProcess.Add(fullPath);
7793
}
7894

7995
var startTime = DateTime.Now.AddMilliseconds(kGiveUpMilliseconds);
8096
var reportedWaiting = false;
81-
var relativePath = e.FullPath.Replace(s_root, "") + " ";
82-
switch (e.ChangeType)
97+
var relativePath = fullPath.Replace(s_root, "") + " ";
98+
switch (changeType)
8399
{
84100
case WatcherChangeTypes.Created:
85101
Print(ConsoleColor.DarkMagenta, "Creation: " + relativePath);
86102
break;
87103
case WatcherChangeTypes.Deleted:
88104
Print(ConsoleColor.DarkRed, "Deletion: " + relativePath);
89-
s_filesInProcess.Remove(e.FullPath);
105+
s_filesInProcess.Remove(fullPath);
90106
return;
91107
case WatcherChangeTypes.Changed:
92108
Print(ConsoleColor.Cyan, "Modified: " + relativePath);
@@ -101,20 +117,20 @@ private static void CampOnFile(FileSystemEventArgs e)
101117
{
102118
try
103119
{
104-
using(File.Open(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.None))
120+
using(File.Open(fullPath, FileMode.Open, FileAccess.Read, FileShare.None))
105121
{
106122

107123
Print(ConsoleColor.Yellow, " Locking: " + filename);
108124
Thread.Sleep(kLockMilliseconds);
109125
}
110126
Print(ConsoleColor.Green, " Released: " + filename);
111-
s_filesInProcess.Remove(e.FullPath);
127+
s_filesInProcess.Remove(fullPath);
112128
return;
113129
}
114130
catch(FileNotFoundException)
115131
{
116132
Print(ConsoleColor.DarkGreen, " File gone: " + filename);
117-
s_filesInProcess.Remove(e.FullPath);
133+
s_filesInProcess.Remove(fullPath);
118134
return;
119135
}
120136
catch(Exception error)
@@ -123,7 +139,7 @@ private static void CampOnFile(FileSystemEventArgs e)
123139
{
124140
Print(ConsoleColor.Red, " Giving up waiting for: " + filename);
125141
Print(ConsoleColor.Red, error.Message);
126-
s_filesInProcess.Remove(e.FullPath);
142+
s_filesInProcess.Remove(fullPath);
127143
return;
128144
}
129145
if(!reportedWaiting)

0 commit comments

Comments
 (0)