c# - XCopy commands to archive files with original date stamp -
i trying write .net console app use xcopy copy on files newer x days, while maintaining original date created timestamp. have command:
/// <summary> /// performs copy , verification using xcopy /// </summary> /// <returns>true if success, false otherwise</returns> internal bool copyandverify() { string date = @"d:" + time.tostring("mm/dd/yyyy"); date = date.replace('/', '-'); date = date.insert(0, "/"); process exeprocess = new process(); processstartinfo startinfo = new processstartinfo(); startinfo.createnowindow = false; startinfo.useshellexecute = false; startinfo.redirectstandarderror = true; startinfo.redirectstandardinput = true; startinfo.redirectstandardoutput = true; startinfo.filename = "xcopy.exe"; startinfo.windowstyle = processwindowstyle.hidden; startinfo.arguments = "\"" + source + "\"" + " " + "\"" + dest + "\"" + @" /v " + date + " /i /s /r /h /y /z"; try { using (exeprocess = process.start(startinfo)) { exeprocess.waitforexit(); } } catch (exception) { return false; } return true; }
the code performs copy , verification, when test find folders/subfolders date modified , date created time of copy. doing wrong?
here's simple code run after xcopy set subdir destination folder dates , times same source. hope helpful.
using system; using system.collections.generic; using system.linq; using system.text; using system.io; namespace copy { class copydirtimestamps { public static bool copytimestamps( string sourcedirname, string destdirname, bool copysubdirs) { try { copyfordir(sourcedirname, destdirname, copysubdirs, false); return true; } catch (exception) { return false; } } private static void copyfordir( string sourcedirname, string destdirname, bool copysubdirs, bool issubdir) { directoryinfo dir = new directoryinfo(sourcedirname); directoryinfo[] dirs = dir.getdirectories(); // if source directory not exist, throw exception. if (!dir.exists) { throw new directorynotfoundexception( "source directory not exist or not found: " + sourcedirname); } if (!directory.exists(destdirname)) return; directoryinfo destdir = new directoryinfo(destdirname); // if copysubdirs true, copy subdirectories. if (copysubdirs) { foreach (directoryinfo subdir in dirs) { // create subdirectory. string temppath = path.combine(destdirname, subdir.name); // copy subdirectories. copyfordir(subdir.fullname, temppath, copysubdirs, true); } } if (issubdir) { destdir.creationtime = dir.creationtime; destdir.lastaccesstime = dir.lastaccesstime; destdir.lastwritetime = dir.lastwritetime; } } } }
Comments
Post a Comment