This repository has been archived by the owner on Sep 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
113 lines (97 loc) · 3.81 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace DuplicateTool
{
class Program
{
static void Main(string[] args)
{
// To check the length of
// Command line arguments
if (args.Length > 0)
{
string passendFile = args[0];
FileAttributes attr = File.GetAttributes(@passendFile);
//Regex Pattern for date
string pattern = @"([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))";
//Get Date
string currentYear = DateTime.Now.Year.ToString();
string currentMonth = DateTime.Now.Month.ToString().PadLeft(2, '0');
string currentDay = DateTime.Now.Day.ToString().PadLeft(2, '0');
string date = currentYear + "-" + currentMonth + "-" + currentDay;
//File
if (!attr.HasFlag(FileAttributes.Directory))
{
string pathStripped = Path.GetDirectoryName(passendFile);
string fileName = Path.GetFileName(passendFile);
string newFilePath;
if (Regex.IsMatch(fileName, pattern))
{
newFilePath = pathStripped + "\\" + date + fileName.Remove(0, 10);
}
else
{
newFilePath = pathStripped + "\\" + date + "_" + fileName;
}
if (!File.Exists(@newFilePath))
{
File.Copy(passendFile, newFilePath);
Console.WriteLine("File Duplicated with todays date.");
}
else
{
Console.WriteLine("File already exists!");
}
}
//Folder
else
{
string folderAbove = System.IO.Directory.GetParent(passendFile).ToString();
string folderName = passendFile.Replace(folderAbove + "\\", "");
string newFolderPath;
if (Regex.IsMatch(folderName, pattern))
{
newFolderPath = folderAbove + "\\" + date + folderName.Remove(0, 10);
}
else
{
newFolderPath = folderAbove + "\\" + date + "_" + folderName;
}
if (!Directory.Exists(@newFolderPath))
{
CopyFolder(passendFile, newFolderPath);
Console.WriteLine("Folder Duplicated with todays date.");
}
else
{
Console.WriteLine("Folder already exists!");
}
}
}
else
{
Console.WriteLine("No file found.");
}
}
static public void CopyFolder(string sourceFolder, string destFolder)
{
if (!Directory.Exists(destFolder))
Directory.CreateDirectory(destFolder);
string[] files = Directory.GetFiles(sourceFolder);
foreach (string file in files)
{
string name = Path.GetFileName(file);
string dest = Path.Combine(destFolder, name);
File.Copy(file, dest);
}
string[] folders = Directory.GetDirectories(sourceFolder);
foreach (string folder in folders)
{
string name = Path.GetFileName(folder);
string dest = Path.Combine(destFolder, name);
CopyFolder(folder, dest);
}
}
}
}