Delete Directory if Emtpy or Only contains .txt


i using syntax move .xlsx or .xls file 1 master directory.  how can delete directory if file left .txt file or directory empty?  note not want delete c:\files\ directory sub-directories.  so example if move spreadsheet directory c:\files\rodger\ , after move directory is 

1) empty

2) has .txt in it

i want delete directory.

also, new c# syntax, proper way move files 1 directory another?  if happens notice may over-looking i'd love tips on improvement gurus!

string path = @"c:\files\"; string newpath = @"c:\master\files\"; var spreadsheet = directory.enumeratefiles(path, "*.xlsx", searchoption.alldirectories); parallel.foreach(spreadsheet, ssheet => { 	string sssssheet = path.getfilename(ssheet); 	if (file.exists(newpath + "\\" + sssssheet)) { } 	else { file.move(ssheet, newpath + "\\" + sssssheet); } });

1 additional thought if file c:\files\abcd\abcd.xlsx exists in c:\master\files\abcd.xlsx -- still want delete directory c:\files\abcd\.

using system; using system.collections.generic; using system.io; using system.linq; using system.text; using system.threading.tasks;  namespace consoleapplication2 { 	class program 	{ 		static void main(string[] args) 		{ 			string path = @"c:\files\"; 			string newpath = @"c:\master\files\"; 			// usage 			cleanup(path, newpath); 		}  		public static void cleanup(string dir, string target, bool deletetarget = false) 		{ 			if (!directory.exists(dir)) 			{ 				return; 			} 			if(!directory.exists(target)) 			{ 				directory.createdirectory(target); 			} 			foreach(string d in directory.getdirectories(dir)) 			{ 				cleanup(dir, d, true); 				if(deletetarget && directory.getfiles(d).where(p=>!p.endswith(".txt")).count() == 0) 				{ 					try 					{ 						// true recursive delete, note called after copying 						directory.delete(d, true); 					} 					catch(exception ex) 					{  						// add routine case suits needs 						// exceptions can access denied, directory gone, directory opened, etc. 					} 				} 			} 			foreach(string f in directory.getfiles(dir)) 			{ 				if(f.endswith(".xslx")) 				{ 					string newpath = path.combine(target, path.getfilename(f)); 					while(file.exists(newpath)) 					{ 						// add routine case suits needs (new name file) 					} 					try 					{ 						file.move(f, newpath); 					} 					catch(exception ex) 					{ 						// add routine case suits needs 						// exceptions can access denied, file gone, file opened, etc. 						// means prpably won't able delete folder 					} 				} 			} 		} 	} }

untested!!!

however, if might work, can suggest, use sort of object collect information on

{ fileoriginal=#oldpath#, filenew=#newpath#, filewasmoved {get { return filenew == null} }, moveexception=#occuredexception# }

{ list<fileactionresult>allfilteredfilesindir=[#allfilteredfilesindir#], hasotherfiles=#nonignorablefilesfound#, _dir=#directorypath#, _filter=#filestomove#, deleted=#wasdeleted#, deleteexception=#occuredexception# }

so can start afterprocessing handle exceptions taht may have occured (for example when file opened , not moved, can present user in afterprocessing , try move again)

cheers






Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

BizTalk Server 2013 Azure VM Log Shipping and HA for hosts

How to Share webservice object to all user