Skip to content

Commit 106e8de

Browse files
Add olderFiles param to GetFiles methods
1 parent 0850fbc commit 106e8de

3 files changed

Lines changed: 14 additions & 12 deletions

File tree

FileSyncLibNet/AccessProviders/FileIoAccessProvider.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public FileIoAccessProvider(ILogger logger, string stateFilename)
1717
this.logger = logger;
1818
if (!string.IsNullOrEmpty(stateFilename))
1919
remoteState = new RemoteState(stateFilename);
20-
20+
2121
}
2222
public void UpdateAccessPath(string accessPath)
2323
{
@@ -31,7 +31,7 @@ public void CreateDirectory(string path)
3131

3232
public FileInfo2 GetFileInfo(string path)
3333
{
34-
if(null!=remoteState)
34+
if (null != remoteState)
3535
return remoteState.GetFileInfo(Path.Combine(AccessPath, path));
3636

3737
var fi = new FileInfo(Path.Combine(AccessPath, path));
@@ -43,7 +43,7 @@ public FileInfo2 GetFileInfo(string path)
4343
};
4444
}
4545

46-
public List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, string path = null, bool recursive = false, List<string> subfolders = null)
46+
public List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, string path = null, bool recursive = false, List<string> subfolders = null, bool olderFiles = false)
4747
{
4848
DirectoryInfo _di = new DirectoryInfo(AccessPath);
4949
List<FileInfo2> ret_val = new List<FileInfo2>();
@@ -57,10 +57,12 @@ public List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, s
5757
searchPattern: pattern,
5858
searchOption: recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
5959

60-
61-
ret_val.AddRange(_fi.Select(x => new FileInfo2(x.FullName.Substring(AccessPath.Length + 1), x.Exists) { Length = x.Length, LastWriteTime = x.LastWriteTime }).ToList());
60+
ret_val.AddRange(_fi.Where(x => (olderFiles ? x.LastWriteTime <= minimumLastWriteTime : x.LastWriteTime >= minimumLastWriteTime))
61+
.Select(x => new FileInfo2(x.FullName.Substring(AccessPath.Length + 1), x.Exists) { Length = x.Length, LastWriteTime = x.LastWriteTime })
62+
.ToList());
6263
}
63-
catch (Exception exc) {
64+
catch (Exception exc)
65+
{
6466
logger?.LogError(exc, "exception in GetFiles for path {A}, dir {B}", path, dir);
6567
}
6668
}

FileSyncLibNet/AccessProviders/IAccessProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ internal interface IAccessProvider
99
{
1010
void CreateDirectory(string path);
1111
void UpdateAccessPath(string path);
12-
List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, string path = null, bool recursive = false, List<string> subfolders = null);
12+
List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, string path = null, bool recursive = false, List<string> subfolders = null, bool olderFiles=false);
1313
FileInfo2 GetFileInfo(string path);
1414
void Delete(FileInfo2 fileInfo);
1515
Stream GetStream(FileInfo2 file);

FileSyncLibNet/AccessProviders/ScpAccessProvider.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal class ScpAccessProvider : IAccessProvider
1717
SftpClient ftpClient;
1818
private readonly RemoteState remoteState;
1919
public string AccessPath { get; private set; }
20-
20+
2121
private readonly ILogger logger;
2222
public ScpAccessProvider(NetworkCredential credentials, ILogger logger, string stateFilename)
2323
{
@@ -137,7 +137,7 @@ public bool MatchesPattern(string fileName, string pattern)
137137
return Regex.IsMatch(fileName, regexPattern, RegexOptions.IgnoreCase);
138138
}
139139

140-
public List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, string path = null, bool recursive = false, List<string> subfolders = null)
140+
public List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, string path = null, bool recursive = false, List<string> subfolders = null, bool olderFiles = false)
141141
{
142142
//add try catch for non existent folders
143143
EnsureConnected();
@@ -156,7 +156,7 @@ public List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, s
156156
try
157157
{
158158
var files = ftpClient.ListDirectory(basePath);
159-
var sepChar="/";
159+
var sepChar = "/";
160160
if (recursive)
161161
{
162162
foreach (var folder in files.Where(x => x.IsDirectory && !(x.Name == ".") && !(x.Name == "..")))
@@ -165,8 +165,8 @@ public List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, s
165165
ret_val.AddRange(GetFiles(minimumLastWriteTime, pattern, subPath, recursive));
166166
}
167167
}
168-
ret_val.AddRange(files.Where(x => MatchesPattern(x.Name, pattern)).Where(x => x.LastWriteTime >= minimumLastWriteTime && !x.IsDirectory).Select(x =>
169-
new FileInfo2($"{(AccessPath.Length + 1<basePath.Length?(basePath.Substring(AccessPath.Length+1))+sepChar:string.Empty)}{x.Name}", exists: true) { LastWriteTime = x.LastWriteTime, Length = x.Length }).ToList());
168+
ret_val.AddRange(files.Where(x => MatchesPattern(x.Name, pattern)).Where(x => (olderFiles ? x.LastWriteTime <= minimumLastWriteTime : x.LastWriteTime >= minimumLastWriteTime) && !x.IsDirectory).Select(x =>
169+
new FileInfo2($"{(AccessPath.Length + 1 < basePath.Length ? (basePath.Substring(AccessPath.Length + 1)) + sepChar : string.Empty)}{x.Name}", exists: true) { LastWriteTime = x.LastWriteTime, Length = x.Length }).ToList());
170170
}
171171
catch (Exception exc)
172172
{

0 commit comments

Comments
 (0)