Hi,
A few days ago I asked how to sort files in a folder according to name and date. I need to upload the files into the database according to name and date.
I received a reply that pointed me in the right direction.
http://forums.asp.net/p/1145406/1863023.aspx#1863023
In line 215 it says that i need to sort the dates in the order that i want. However, i tried and was unable to.
First employees, then venues, seminars, cvs,SemReg,CandidteAttds,Offers.
But i also have to pay attention to the date in the file name so in the example i will first upload:
081706_ETS_Employees.xls
081507_ETS_Employees.xls
081507_ETS_Venues.xls
081507_ETS_Seminars.xls
082207_ETS_SemReg.xls
082207_ETS_CandidteAttds.xls
082207_ETS_Offers.xls
081508_ETS_Employees.xls
The files in the folder are in the format:
081507_ETS_Employees.xls
081507_ETS_Venues.xls
081507_ETS_Seminars.xls
081508_ETS_Employees.xls
081706_ETS_Employees.xls
082207_ETS_SemReg.xls
082207_ETS_CandidteAttds.xls
082207_ETS_Offers.xls
How can i sort the files correctly?
Thanks
1 private static ArrayList alFiles = new ArrayList();
2 public static string FTPconnect = ConfigurationSettings.AppSettings["FTP"].ToString();
3 public static string FTPMoveFiles = ConfigurationSettings.AppSettings["FTPMoveFiles"].ToString();
4
5 [STAThread]
6
7
8 static void Main(string[] args)
9 {
10 Application.EnableVisualStyles();
11 Application.SetCompatibleTextRenderingDefault(fals e);
12 // Application.Run(new Form1());
13 string[] myArray = new string[7];
14
15 string PartFileName = string.Empty;
16 string FilePath = string.Empty;
17 string _strSheetName = string.Empty;
18 ArrayList alLogSuccess = new ArrayList();
19 ArrayList alLogErrors = new ArrayList();
20 string cv = string.Empty;
21 string attd = string.Empty;
22 string reg = string.Empty;
23 string offers = string.Empty;
24 string Employees = string.Empty;
25 string Seminars = string.Empty;
26 string Venues = string.Empty;
27
28 excelReader exr = new excelReader();
29
30 string Location = FTPconnect; //@"C:\excelTest\";
31
32 string partFname = string.Empty;
33 System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(Location);
34 int intFiles = dir.GetFiles("*.*").GetUpperBound(0) + 1;
35 // string[] myArray = new string[intFiles];
36 string inpBodyWords = string.Empty;
37 string BodyWords = string.Empty;
38 string fromMail = string.Empty;
39 string textCV = string.Empty;
40 string AttachHtmFile = string.Empty;
41 //Initialize the name part of file. Venues is first, Seminar is second, Employees is third.
42 string[] arrayName = new string[] { "Employees","Venues", "Seminars","CVs","SemReg","CandidteAttds","Off ers" };
43
44 //First get all the files.
45 ArrayList listDate = FilesArray();
46 //Sort the date part of files.
47 listDate = FilesSort(listDate);
48
49
50 string fullpath = string.Empty;
51
52
53 if (intFiles == 0)
54 {
55
56 Console.WriteLine("no files in directory");
57 DB.noFiles("No files in directory");
58 }
59 else
60 {
61 for (int i = 0; i < listDate.Count; i++)
62 {
63 for (int j = 0; j < 7; j++)
64 {
65 //Get complete name of file
66 string strFile = listDate[i].ToString() + "_ETS_" + arrayName[j] + ".xls";
67 fullpath = FTPconnect + strFile;
68
69
70 //
71 //TODO: Import this file into database
72
73 if (File.Exists(fullpath.ToString()))
74 {
75 //File.Create(@"c:/files.txt");
76 File.AppendAllText(@"c:/files.txt",fullpath.ToString());
77 string UsedLocation = FTPMoveFiles;//@"C:\excelTest\Old\";
78 FilePath = fullpath.ToString();
79 // CallReader(FilePath, _strSheetName, fullpath.ToString());
80 if (File.Exists(UsedLocation + fullpath.ToString()))
81 {
82 Console.Write("File exists in location");
83 }
84 else
85 {
86 File.Move(FilePath, UsedLocation + strFile.ToString());
87 // myArray[i] = null;
88 }
89 }
90 else
91 {
92 // File.Move(FilePath, UsedLocation + myArray[i].ToString());
93 Console.Write("File does not exist in location");
94 if (fullpath.Contains ("Employees"))
95 {
96
97 DB.noFiles("No employees file for the " + " " + listDate[i].ToString() + " in directory");
98 }
99
100 if (fullpath.Contains("Venues"))
101 {
102
103 DB.noFiles("No Venues file for the " + " " + listDate[i].ToString() + " in directory");
104 }
105
106 if (fullpath.Contains("Seminars"))
107 {
108
109 DB.noFiles("No Seminars file for the " + " " + listDate[i].ToString() + " directory");
110 }
111
112 if (fullpath.Contains ("CVs"))
113 {
114
115 DB.noFiles("No CVs file for the " + " " + listDate[i].ToString() + " directory");
116 }
117 if (fullpath.Contains("SemReg"))
118 {
119
120 DB.noFiles("No SemReg file for the " + " " + listDate[i].ToString() + " in directory");
121 }
122 if (fullpath.Contains("CandidteAttds"))
123 {
124
125 DB.noFiles("No CandidateAttended file for the " + " " + listDate[i].ToString() + " in directory");
126 }
127
128 if (fullpath.Contains("Offers"))
129 {
130
131 DB.noFiles("No Offers file for the " + " " + listDate[i].ToString() + " in directory");
132 }
133 //end of check if files exist
134
135 }
136
137 // }
138 //
139 }
140
141 }
142 }//end else intfiles
143
144 }
145
146 /// <summary>
147 /// call db functions
148 /// </summary>
149 /// <returns>The ArrayList includes the name of Excel</returns>
150 private static void CallReader(string FilePath, string _strSheetName, string partFname)
151 {
152 //System.IO.FileInfo f;
153 excelReader exr = new excelReader();
154 DataTable _dt;
155 exr.ReadExcel(FilePath, out _strSheetName);
156 _dt = exr.GetTable(FilePath, _strSheetName, partFname);
157
158 }
159 /// <summary>
160 /// Get all the files
161 /// </summary>
162 /// <returns>The ArrayList includes the name of Excel</returns>
163 private static ArrayList FilesArray()
164 {
165
166 ArrayList list = new ArrayList();
167 string Location = FTPconnect;
168 System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(Location);
169 FileInfo[] fileInfoArray = dirInfo.GetFiles("*.xls");
170
171 foreach (System.IO.FileInfo f in dirInfo.GetFiles("*.xls"))
172 {
173
174 list.Add((f.Name));
175 }
176
177 return list;
178
179 }
180
181 /// <summary>
182 /// Sort the file name according to the requirement.
183 /// </summary>
184 /// <param name="list">The ArrayList includes the name of Excel</param>
185 /// <returns>The Date port of Arraylist be sorted</returns>
186 public static ArrayList FilesSort(ArrayList list)
187 {
188 ArrayList listDate = new ArrayList();
189
190 //for (int i = 0; i < list.Count; i++)
191 foreach (string str in list)
192 {
193 //Get Date part
194 string strDate = str.ToString().Substring(0, 6);
195
196 //You should check the date to see if the date has been in the listDate.
197 // If the date has been in the listDate, you should not add it to listDate.
198 //
199 //TODO: Check date
200 //
201
202 if (listDate.Contains(strDate) )
203 {
204 Console.Write("date in list already");
205 }
206 else
207 {
208 listDate.Add(strDate);
209
210 }
211
212 }
213
214 //
215 //TODO: Sort the Date by your requirement.Please //make sure there is the unique date in the listDate.
216 //
217
218 return listDate;
219 }
220 }