File move archieve folder

mail@pastecode.io avatar
unknown
csharp
2 years ago
808 B
2
Indexable
Never
 private void MoveCsvFiletoArchieved(string fileName, string filePath)
        {
            if (File.Exists($"{filePath}{fileName}"))
            {
                string archievedFolderPath = $"{filePath}Archieved/";

                if (!Directory.Exists(archievedFolderPath))
                    Directory.CreateDirectory(archievedFolderPath);

                var file = new DirectoryInfo(filePath).GetFiles()?.FirstOrDefault(x => x.Name.Equals(fileName));
                if (file != null && file.Length > 0)
                {
                    if (DateTime.Compare(file.LastWriteTime, DateTime.Now) < 0)
                    {
                        file.MoveTo($"{archievedFolderPath}{file.Name.AppendTimeStamp()}");
                    }
                }
            }
        }