c# - What is the best way to copy a list of files with these variable filenames using Path.Combine -
i have source directories c:\source\yyyy-mm-dd yyyy-mm-dd represents range of dates, , destination directory e:\destination. need multiple filenames in each date directory in range of dates , copy (filenamea*.*, filenameb*.*, filenamec*.*). need create date directories range of dates , copy associated files found respective directories.
i'm using 2 datetimepickers range of dates. have foreach logic working fine. i'm struggling best way use path.combine create destination folders , copy each of files. think i'm doing unnecessary work in existing code, , keep confusing myself:
private async void processfiles() { // create list of topics var topics = topicsbox.checkeditems.cast<string>().tolist(); // create list of source directories based on date range var directories = new list<string>(); var folders = new list<string>(); (datetime date = datetimepicker1.value.date; date.date <= datetimepicker2.value.date; date = date.adddays(1)) { var datedir = _tracepath + @"\" + date.tostring("yyyy-mm-dd") + @"\"; directories.add(datedir); folders.add(@"\" + date.tostring("yyyy-mm-dd") + @"\"); } // create list of source files copy , destination foreach (var path in directories) { var path1 = path; try { foreach (var files2 in folders) { var destpath = textbox1.text + @"\" + textbox4.text + files2; foreach (var files in topics) { if (!directory.exists(path1)) { toolstripstatuslabel1.forecolor = color.red; toolstripstatuslabel1.text = ("could not find \"" + path1 + "\" check start , end date."); } else { foreach ( string sourcepath in directory.enumeratefiles(path1, files + "*.*", searchoption.alldirectories)) { var filename = path.getfilename(sourcepath); // each filename in source directory var fulldestpath = path.combine(destpath, filename); // create destination folder name directory.createdirectory(sourcepath.replace(sourcepath, destpath)); // create destination folder // copy files temp folder asynchronously using (filestream sourcestream = file.open(sourcepath, filemode.open)) { using (filestream destinationstream = file.create(fulldestpath)) { xferbtn.enabled = false; toolstripstatuslabel1.forecolor = color.green; toolstripstatuslabel1.text = "copying " + sourcepath; await sourcestream.copytoasync(destinationstream); toolstripstatuslabel1.text = "copying complete!"; } } } } } } } catch (exception e) { } } }
to start, may easier break things apart separate methods keep track of tasks. find easier when doing multiple tasks in 1 method. created simple test copy files source destination. won't match logic, may lead in right direction. when enumerate on files, ienumerable object work with. can use iterate on file paths. also, have directoryinfo object can use check directory creation before copying. did not follow logic source/destination stream, instead used system.file.io.copy method show how path.combine works. easy way combine path directory , file without concatenation. string.format("{0}", somthingtoappend).
private void btnprocess_click(object sender, eventargs e) { // create list of source directories based on date range list<string> sourcedirectories = new list<string>(); list<string> destdirectories = new list<string>(); sourcedirectories.add("c:\\users\\eric\\desktop\\dir1"); sourcedirectories.add("c:\\users\\eric\\desktop\\dir2"); destdirectories.add("c:\\users\\eric\\desktop\\dest1"); destdirectories.add("c:\\users\\eric\\desktop\\dest2"); // create list of source files copy , destination foreach (var path in sourcedirectories) { if (!directory.exists(path)) { //error message here if path not exist or continue next path continue; } try { ienumerable<string> paths = directory.enumeratefiles(path, "*.*", searchoption.alldirectories); directoryinfo destpathinfo = directory.createdirectory(destdirectories[0]); // create destination folder if (destpathinfo.exists) { foreach (var p in paths) { string destination = system.io.path.combine(destdirectories[0], system.io.path.getfilename(p)); system.io.file.copy(p, destination); } destdirectories.removeat(0); } } catch (exception ex) { } } }
Comments
Post a Comment