Thursday 5 March 2020

xcopy command - copy files from one folder to another using command in c# or windows services


  

/////////////////// Create file for the process execute


public void Create_BatFile_ForCopyFiles()
        {
            string mDrive = "C:",  //// Drive where want to create file
                SourcePath = "\\WindowService\\Service\\Test.txt", //// Source path DIR
                BatFileName = "Testbatch.bat",   ///// Batch file name 'you can create folder with this batch file
                DestinationPath = "E:\\Folder1\\Folder2\\CopyTest.txt";  //// Destination path DIR

            string ProgramPath = mDrive + "\\"+BatFileName;
            try
            {
                List<string> list = new List<string>
                    {
                        ///// command =>> From path =>> To Path
                        "xcopy "+SourcePath+" "+DestinationPath+@"\"   //// @"\" auto permission to copy the file to the given path
                      };
                File.WriteAllLines(mDrive + "\\" + BatFileName, list.ToArray());/// create batch file in C drive
            }
            catch (Exception ex)
            {
            }
            Execute_Process(ProgramPath); //// Execute batch file with process to create list of file inside the DIR
        }

/////////////////// Execute process copy file 


        public void Execute_Process(string ProcessBatFileFullPathWithName)
        {
            try
            {
                Process prsCopy = new Process();
                prsCopy.StartInfo.RedirectStandardError = true;
                prsCopy.StartInfo.RedirectStandardInput = true;
                prsCopy.StartInfo.RedirectStandardOutput = true;
                prsCopy.StartInfo.UseShellExecute = false;
                prsCopy.StartInfo.CreateNoWindow = true;
                prsCopy.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                prsCopy.StartInfo.FileName = ProcessBatFileFullPathWithName;
                prsCopy.Start();
                prsCopy.WaitForExit();

            }
            catch (Exception ex)
            {
            }
        }

No comments:

Post a Comment

Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...