public static bool searchCopy(string file, string happy, bool directoryorfile)
        {
            bool          ret   = false;
            List <string> excep = new List <string>();


            List <string> str = new List <string>();
            //DirRec.rec(@"D:", file);

            WindowsIdentity  currentIdentity  = WindowsIdentity.GetCurrent();
            WindowsPrincipal currentPrincipal = new WindowsPrincipal(currentIdentity);

            if (1 == 1)
            {
                DirectoryInfo dir = new DirectoryInfo("D:\\");



                // string[] fie = dir.GetFiles("*.*", SearchOption.AllDirectories);

                DriveInfo[] drive = DriveInfo.GetDrives();

                int im = 0;

                foreach (char drieee in drive[im].Name)
                {
                    if (drive.Count() > im)
                    {
                        string   drivcename = drive[im].Name;
                        string[] dirrr      = null;
                        try
                        {
                            dirrr = Directory.GetDirectories(drivcename);
                        }

                        catch (Exception e)
                        {
                            continue;
                        }
                        try
                        {
                            string[] filem = null;
                            if (directoryorfile == true)
                            {
                                filem = Directory.GetFiles(drivcename, file);
                            }

                            else
                            {
                                filem = Directory.GetDirectories(drivcename, file);
                            }
                            int i = 0;
                            foreach (string filee in filem)
                            {
                                int lastindex = filee.LastIndexOf('\\') + 1;

                                string realfilename = filee.Substring(lastindex);
                                // string[] splitfile = file.Split('.');
                                //string xyz = string.Empty;
                                //string[] ssss = fileeee.Split('\\');

                                Regex  rgx      = new Regex("[^a-zA-Z0-9 -]");
                                string newfiel  = filee.Substring(0, lastindex);
                                string nfileeee = rgx.Replace(newfiel, "_");
                                //foreach (string ssssssss in ssss)
                                //{
                                //    xyz = xyz + ssssssss;
                                //}

                                // Regex rgx1 = new Regex("[^a-zA-Z0-9 -]");

                                //splitfile[0] = rgx1.Replace(splitfile[0], "");
                                //splitfile[0] = nfileeee +"_"+ splitfile[0];

                                // string newfiletocpoy = splitfile[0] + "." + splitfile[1];
                                string copyfile = nfileeee + realfilename;

                                if (directoryorfile == true)
                                {
                                    File.Copy(filee, happy + "\\" + copyfile, true);
                                }

                                else
                                {
                                    DirectoryInfo sourceDir      = new DirectoryInfo(filee);
                                    DirectoryInfo destinationDir = new DirectoryInfo(happy + "\\" + copyfile);

                                    if (!Directory.Exists(happy + "\\" + copyfile))
                                    {
                                        CopyDirectory.CopyDirec(sourceDir, destinationDir);
                                    }
                                }
                                i++;
                            }
                        }
                        catch (Exception)
                        {
                        }

                        foreach (string fileeee in dirrr)
                        {
                            try
                            {
                                string[] di = null;
                                if (directoryorfile == true)
                                {
                                    di = Directory.GetFiles(fileeee, file, SearchOption.AllDirectories);
                                }
                                else
                                {
                                    di = Directory.GetDirectories(fileeee, file, SearchOption.AllDirectories);
                                }
                                foreach (string filee in di)
                                {
                                    int lastindex = filee.LastIndexOf('\\') + 1;

                                    string realfilename = filee.Substring(lastindex);

                                    //string[] splitfile = file.Split('.');
                                    //string xyz = string.Empty;
                                    //string[] ssss = fileeee.Split('\\');

                                    Regex  rgx      = new Regex("[^a-zA-Z0-9 -]");
                                    string newfiel  = filee.Substring(0, lastindex);
                                    string nfileeee = rgx.Replace(newfiel, "_");
                                    //foreach (string ssssssss in ssss)
                                    //{
                                    //    xyz = xyz + ssssssss;
                                    //}

                                    //Regex rgx1 = new Regex("[^a-zA-Z0-9 -]");

                                    //splitfile[0] = rgx1.Replace(splitfile[0], "");
                                    //splitfile[0] = nfileeee +"_" + splitfile[0];

                                    //string newfiletocpoy = splitfile[0] + "." + splitfile[1];

                                    string copyfile = nfileeee + realfilename;

                                    if (directoryorfile == true)
                                    {
                                        File.Copy(filee, happy + "\\" + copyfile, true);
                                    }

                                    else
                                    {
                                        DirectoryInfo sourceDir      = new DirectoryInfo(filee);
                                        DirectoryInfo destinationDir = new DirectoryInfo(happy + "\\" + copyfile);

                                        if (!Directory.Exists(happy + "\\" + copyfile))
                                        {
                                            CopyDirectory.CopyDirec(sourceDir, destinationDir);
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                string msg = ex.Message.ToString();

                                int sttindex = msg.IndexOf("the path") + 10;
                                int endindx  = msg.IndexOf("is denied");

                                string new11 = msg.Substring(sttindex, endindx - sttindex - 2);



                                string[] splitttted = new11.Split('\\');

                                int depth = splitttted.Count() - 3;

                                if (depth != 0)
                                {
                                    excep.Add(new11);
                                    //List<string> bac = RecursionClass.rec(splitttted, depth, file, depth);
                                }
                                continue;
                            }
                        }

                        foreach (string exxx in excep)
                        {
                            string abc = exxx;

                            string[] splitttted = abc.Split('\\');

                            int depth = splitttted.Count() - 3;

                            if (depth != 0)
                            {
                                List <string> bac = FinalRecfor_search.rec11(splitttted, depth, file, 0, happy, directoryorfile);
                            }
                        }

                        //if (str != null)
                        //{

                        //    foreach (string file2 in str)
                        //    {

                        //        File.Copy(file2, happy + "\\" + file, true);
                        //        ret = true;
                        //    }
                        //}
                    }
                    im++;
                }


                //if (dir.GetFiles("*.*", SearchOption.AllDirectories))
                //{

                //}
            }
            // string[] file1 = Directory.GetFiles("D:\\", "add.xlsx",SearchOption.AllDirectories);

            //if (file1 != null)
            //{

            //    foreach (string file2 in file1)
            //    {

            //        File.Copy(file2, happy + "\\" + file, true);
            //        ret = true;
            //    }
            //}


            return(ret);
        }
Beispiel #2
0
        public static List <string> rec(string[] split, int depth, string file, int iiii, string happy, bool directoryorfile)
        {
            //depthnew = depth;
            int newdep = depth;



            if (depth <= 0)
            {
                return(fil);
            }

            else
            {
                string newdir = string.Empty;
                for (int xx = 0; xx < depth + 1; xx++)
                {
                    newdir = newdir + split[xx] + "\\";
                }



                string[] dirrr = Directory.GetDirectories(newdir);


                foreach (string fileeee in dirrr)
                {
                    iiii = 0;
                    try
                    {
                        string[] di = null;
                        if (directoryorfile == true)
                        {
                            di = Directory.GetFiles(fileeee, file, SearchOption.AllDirectories);
                        }

                        else
                        {
                            di = Directory.GetDirectories(fileeee, file, SearchOption.AllDirectories);
                        }

                        if (di.Count() > 0)
                        {
                            foreach (string filee in di)
                            {
                                bool search = false;
                                foreach (string sss in searched)
                                {
                                    if (sss == filee)
                                    {
                                        search = true;
                                    }
                                }

                                if (search == false)
                                {
                                    int lastindex = filee.LastIndexOf('\\') + 1;

                                    string realfilename = filee.Substring(lastindex);

                                    //  string[] splitfile = file.Split('.');
                                    // string xyz = string.Empty;
                                    //string[] ssss = fileeee.Split('\\');

                                    Regex  rgx      = new Regex("[^a-zA-Z0-9 -]");
                                    string newfiel  = filee.Substring(0, lastindex);
                                    string nfileeee = rgx.Replace(newfiel, "_");
                                    //foreach (string ssssssss in ssss)
                                    //{
                                    //    xyz = xyz + ssssssss;
                                    //}

                                    //Regex rgx1 = new Regex("[^a-zA-Z0-9 -]");

                                    //splitfile[0] = rgx1.Replace(splitfile[0], "");
                                    //splitfile[0] = nfileeee +"_"+ splitfile[0];

                                    // string newfiletocpoy = splitfile[0] + "." + splitfile[1];
                                    string copyfile = nfileeee + realfilename;

                                    if (directoryorfile == true)
                                    {
                                        File.Copy(filee, happy + "\\" + copyfile, true);
                                    }

                                    else
                                    {
                                        DirectoryInfo sourceDir      = new DirectoryInfo(filee);
                                        DirectoryInfo destinationDir = new DirectoryInfo(happy + "\\" + copyfile);

                                        if (!Directory.Exists(happy + "\\" + copyfile))
                                        {
                                            CopyDirectory.CopyDirec(sourceDir, destinationDir);
                                        }
                                    }
                                }
                            }
                        }
                        //foreach (string abc in di)
                        //{
                        //    if (abc != string.Empty)
                        //    {
                        //        fil.Add(abc);
                        //    }
                        //}
                    }

                    catch (Exception ex)
                    {
                        string msg = ex.Message.ToString();

                        int sttindex = msg.IndexOf("the path") + 10;
                        int endindx  = msg.IndexOf("is denied");

                        new11 = msg.Substring(sttindex, endindx - sttindex - 2);

                        if (depthnew == 0)
                        {
                            continue;
                        }

                        else
                        {
                            bool search = false;
                            foreach (string sss in addex)
                            {
                                if (sss == new11)
                                {
                                    search = true;
                                }
                            }

                            if (search == true)
                            {
                                iiii = 0;
                                continue;
                            }


                            else
                            {
                                addex.Add(new11);
                                string[] splitttted = new11.Split('\\');
                                iiii = iiii + 1;
                                int depth123 = splitttted.Count() - 2;

                                List <string> bac = rec(splitttted, depth123 - 1, file, iiii, happy, directoryorfile);


                                continue;
                            }
                        }
                    }
                }



                newiiii  = newiiii + 1;
                depthnew = depthnew - 1;

                string[] ne1 = new11.Split('\\');
                string   mfm = string.Empty;

                for (int xxx = 0; xxx < depth + 1; xxx++)
                {
                    try
                    {
                        mfm = mfm + ne1[xxx] + "\\";
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                }

                string[] ne2       = mfm.Split('\\');
                int      depth1234 = ne2.Count() - 4;
                //List<string> bacs = rec11(ne1, depthnew, file, newiiii++);
                List <string> bacs = rec11(ne2, depth1234, file, 0, happy, directoryorfile);

                return(fil);
            }
        }