public static void RenameFileExtension(string iPath, string src, string dest)
        {
            if (!Directory.Exists(iPath))
            {
                return;
            }

            // rename subDir
            string[] dirPaths = Directory.GetDirectories(iPath);
            for (int i = 0; i < dirPaths.Length; i++)
            {
                RenameFileExtension(dirPaths[i], src, dest);
            }

            // rename files
            string[] filePaths = Directory.GetFiles(iPath);
            for (int i = 0; i < filePaths.Length; ++i)
            {
                string extension = Path.GetExtension(filePaths[i]);
                if (extension == src)
                {
                    string dir      = Path.GetDirectoryName(filePaths[i]);
                    string name     = Path.GetFileNameWithoutExtension(filePaths[i]);
                    string destFile = dir + name + dest;

                    MoveWithReplace(filePaths[i], destFile);
                }
            }
        }
Пример #2
0
        /*public static void GetFilesForImportFolder(string folderLocation, ref List<string> fileList)
         *      {
         *              if (Directory.Exists(folderLocation))
         *              {
         *                      // get root level files
         *                      fileList.AddRange(Directory.GetFiles(folderLocation, "*.*", SearchOption.TopDirectoryOnly));
         *
         *                      // search sub folders
         *                      foreach (string dirName in Directory.GetDirectories(folderLocation))
         *                      {
         *                              try
         *                              {
         *                                      if (dirName.ToUpper().Contains("RECYCLE.BIN")) continue;
         *
         *                                      fileList.AddRange(Directory.GetFiles(dirName, "*.*", SearchOption.AllDirectories));
         *                              }
         *                              catch (Exception ex)
         *                              {
         *                                      logger.Warn("Error accessing: {0} - {1}", dirName, ex.Message);
         *                              }
         *                      }
         *              }
         *      }*/

        public static void GetFilesForImportFolder(string sDir, ref List <string> fileList)
        {
            try
            {
                // get root level files
                fileList.AddRange(Directory.GetFiles(sDir, "*.*", SearchOption.TopDirectoryOnly));

                // search sub folders
                foreach (string d in Directory.GetDirectories(sDir))
                {
                    DirectoryInfo di       = new DirectoryInfo(d);
                    bool          isSystem = (di.Attributes & FileAttributes.System) == FileAttributes.System;
                    if (isSystem)
                    {
                        continue;
                    }

                    //fileList.AddRange(Directory.GetFiles(d, "*.*", SearchOption.TopDirectoryOnly));

                    GetFilesForImportFolder(d, ref fileList);
                }
            }
            catch (System.Exception excpt)
            {
                Console.WriteLine(excpt.Message);
            }
        }
Пример #3
0
        public IResult <IEnumerable <string> > GetAllDirectories(string path)
        {
            try
            {
                var directories = Directory.GetDirectories(path);

                return(new SuccessResult <IEnumerable <string> >(directories));
            }
            catch (Exception e)
            {
                return(new FailureResult <IEnumerable <string> >(e));
            }
        }
Пример #4
0
        public override DirectoryInfo[] GetDirectories()
        {
            if (Drive == null)
            {
                return(new DirectoryInfo[0]);
            }
            String path = Name;

            if (!Name.EndsWith("" + Path.DirectorySeparatorChar))
            {
                path += Path.DirectorySeparatorChar;
            }
            return(Directory.GetDirectories(path).Select(a => new DirectoryInfo(a)).ToArray());
        }
Пример #5
0
        public void ReadCourse(string coursePath, string dbPath)
        {
            try
            {
                if (Directory.Exists(coursePath) && this.InitDb(dbPath))
                {
                    bgwGetCourse.ReportProgress(1, new Log()
                    {
                        Text = "Getting course data . . .", TextColor = Color.Green, NewLine = true
                    });

                    List <string> folderList = Directory.GetDirectories(coursePath, "*", SearchOption.TopDirectoryOnly).ToList();
                    logger.Debug($"FolderList1: {folderList.Count}");
                    folderList = folderList.Where(r => Directory.GetDirectories(r, "*", SearchOption.TopDirectoryOnly).Length > 0).ToList();
                    logger.Debug($"FolderList2: {folderList.Count}");
                    listCourse = folderList.Select(r => new CourseItem()
                    {
                        CoursePath = r, Course = this.GetCourseFromDb(r)
                    }).Where(r => r.Course != null).OrderBy(r => r.Course.Title).ToList();
                    logger.Debug($"listCourse1: {listCourse.Count}");
                    listCourse = listCourse.Where(c => c.Course.IsDownloaded).ToList();
                    logger.Debug($"listCourse2: {listCourse.Count}");
                    foreach (CourseItem item in listCourse)
                    {
                        Image img = File.Exists(item.CoursePath + @"\image.jpg") ? Image.FromFile(item.CoursePath + @"\image.jpg") : new Bitmap(100, 100);

                        ListViewItem listItem = new ListViewItem()
                        {
                            ImageKey = item.Course.Name, Name = item.Course.Name, Text = item.Course.Title
                        };

                        bgwGetCourse.ReportProgress(1, new { Item = listItem, Image = img });
                    }

                    bgwGetCourse.ReportProgress(1, new Log()
                    {
                        Text = $"Complete! Total: {listCourse.Count} courses", TextColor = Color.Green, NewLine = true
                    });

                    bgwGetCourse.ReportProgress(100);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                logger.Error(ex);
            }
        }
Пример #6
0
 static void MarkFolderWritable(string folder)
 {
     // ReSharper disable once ObjectCreationAsStatement
     if (!Directory.Exists(folder))
     {
         return;
     }
     Directory.GetDirectories(folder, "*", SearchOption.AllDirectories).ForEach(f => new DirectoryInfo(f)
     {
         Attributes = FileAttributes.Normal
     });
     Directory.GetFiles(folder, "*", SearchOption.AllDirectories).ForEach(f => new FileInfo(f)
     {
         Attributes = FileAttributes.Normal
     });
 }
Пример #7
0
        public void TestGetDirectoriesWithSearchWithNoResults()
        {
            var randomFileName       = Path.GetRandomFileName();
            var tempLongPathFilename = Path.Combine(uncDirectory, randomFileName);

            Directory.CreateDirectory(tempLongPathFilename);
            try
            {
                var dirs = Directory.GetDirectories(uncDirectory, "gibberish").ToArray();
                Assert.AreEqual(0, dirs.Length);
                Assert.IsFalse(dirs.Contains(tempLongPathFilename));
            }
            finally
            {
                Directory.Delete(tempLongPathFilename);
            }
        }
Пример #8
0
        public void TestGetRecursiveDirectoriesWithSubsetSearch()
        {
            var tempLongPathFilename = Path.Combine(uncDirectory, "TestGetRecursiveDirectoriesWithSubsetSearch");

            Directory.CreateDirectory(tempLongPathFilename);
            var tempLongPathFilename2 = Path.Combine(tempLongPathFilename, "ATestGetRecursiveDirectoriesWithSubsetSearch");

            Directory.CreateDirectory(tempLongPathFilename2);
            try
            {
                Assert.AreEqual(1, Directory.GetDirectories(uncDirectory, "A*", System.IO.SearchOption.AllDirectories).Count());
            }
            finally
            {
                Directory.Delete(tempLongPathFilename2);
                Directory.Delete(tempLongPathFilename);
            }
        }
Пример #9
0
        public void TestGetDirectoriesWithAnySearch()
        {
            var tempLongPathFilename = Path.Combine(uncDirectory, "TestGetDirectoriesWithAnySearch");

            Directory.CreateDirectory(tempLongPathFilename);
            var tempLongPathFilename2 = Path.Combine(uncDirectory, "ATestGetDirectoriesWithAnySearch");

            Directory.CreateDirectory(tempLongPathFilename2);
            try
            {
                Assert.AreEqual(2, Directory.GetDirectories(uncDirectory, "*").Count());
            }
            finally
            {
                Directory.Delete(tempLongPathFilename);
                Directory.Delete(tempLongPathFilename2);
            }
        }
        // clean all files and directories in iPath, then delete iPath dir...
        public static void DeleteDirectory(string iPath, string[] ignoreFiles = null)
        {
            if (iPath == null)
            {
                return;
            }
            if (Application.platform == RuntimePlatform.WindowsEditor)
            {
                iPath = iPath.Replace('/', '\\');
            }

            if (!Directory.Exists(iPath))
            {
                return;
            }

            // delete subDir
            string[] dirPaths = Directory.GetDirectories(iPath);
            for (int i = 0; i < dirPaths.Length; i++)
            {
                DeleteDirectory(dirPaths[i], ignoreFiles);
            }

            // delete files
            string[] filePaths = Directory.GetFiles(iPath);
            for (int i = 0; i < filePaths.Length; ++i)
            {
                string extension = Path.GetExtension(filePaths[i]);
                if (ignoreFiles != null && Array.IndexOf(ignoreFiles, extension) >= 0)
                {
                    continue;
                }
                if (filePaths[i] != null)
                {
                    File.Delete(filePaths[i]);
                }
            }

            // delete iPath dir...
            if (Directory.GetDirectories(iPath).Length == 0 && Directory.GetFiles(iPath).Length == 0)
            {
                Directory.Delete(iPath);
            }
        }
Пример #11
0
        private void InitNodes()
        {
            foreach (string cat in categories)
            {
                foreach (string sub in subjectNames)
                {
                    DoNodesForFolder(cat, sub);
                }
            }
            string textbookPath = path + "Textbooks\\";

            string[] dirs = Directory.GetDirectories(textbookPath);
            foreach (string s in dirs)
            {
                string sub = s.Substring(textbookPath.Length);
                DoNodesForFolder("Textbooks", sub);
            }
            files.Sort();
            //webBrowser1.
        }
Пример #12
0
 public void TestGetRecursiveDirectoriesWithSearch()
 {
     Assert.AreEqual(0, Directory.GetDirectories(uncDirectory, "*", SearchOption.AllDirectories).Count());
 }
Пример #13
0
 public void TestGetDirectories()
 {
     Assert.AreEqual(0, Directory.GetDirectories(uncDirectory).Count());
 }
Пример #14
0
        public void MoveFileIfRequired()
        {
            try
            {
                logger.Trace("Attempting to move file: {0}", this.FullServerPath);

                // check if this file is in the drop folder
                // otherwise we don't need to move it
                if (this.ImportFolder.IsDropSource == 0)
                {
                    logger.Trace("Not moving file as it is NOT in the drop folder: {0}", this.FullServerPath);
                    return;
                }

                if (!File.Exists(this.FullServerPath))
                {
                    logger.Error("Could not find the file to move: {0}", this.FullServerPath);
                    return;
                }

                // find the default destination
                ImportFolder           destFolder = null;
                ImportFolderRepository repFolders = new ImportFolderRepository();
                foreach (ImportFolder fldr in repFolders.GetAll())
                {
                    if (fldr.IsDropDestination == 1)
                    {
                        destFolder = fldr;
                        break;
                    }
                }

                if (destFolder == null)
                {
                    return;
                }

                if (!System.IO.Directory.Exists(destFolder.ImportFolderLocation))
                {
                    return;
                }

                // keep the original drop folder for later (take a copy, not a reference)
                ImportFolder dropFolder = this.ImportFolder;

                // we can only move the file if it has an anime associated with it
                List <CrossRef_File_Episode> xrefs = this.EpisodeCrossRefs;
                if (xrefs.Count == 0)
                {
                    return;
                }
                CrossRef_File_Episode xref = xrefs[0];

                // find the series associated with this episode
                AnimeSeriesRepository repSeries = new AnimeSeriesRepository();
                AnimeSeries           series    = repSeries.GetByAnimeID(xref.AnimeID);
                if (series == null)
                {
                    return;
                }

                // find where the other files are stored for this series
                // if there are no other files except for this one, it means we need to create a new location
                bool   foundLocation = false;
                string newFullPath   = "";

                // sort the episodes by air date, so that we will move the file to the location of the latest episode
                List <AnimeEpisode> allEps = series.GetAnimeEpisodes().OrderByDescending(a => a.AniDB_EpisodeID).ToList();

                AniDB_AnimeRepository           repAnime      = new AniDB_AnimeRepository();
                CrossRef_File_EpisodeRepository repFileEpXref = new CrossRef_File_EpisodeRepository();

                foreach (AnimeEpisode ep in allEps)
                {
                    // check if this episode belongs to more than one anime
                    // if it does we will ignore it
                    List <CrossRef_File_Episode> fileEpXrefs = repFileEpXref.GetByEpisodeID(ep.AniDB_EpisodeID);
                    int? animeID   = null;
                    bool crossOver = false;
                    foreach (CrossRef_File_Episode fileEpXref in fileEpXrefs)
                    {
                        if (!animeID.HasValue)
                        {
                            animeID = fileEpXref.AnimeID;
                        }
                        else
                        {
                            if (animeID.Value != fileEpXref.AnimeID)
                            {
                                crossOver = true;
                            }
                        }
                    }
                    if (crossOver)
                    {
                        continue;
                    }

                    foreach (VideoLocal vid in ep.GetVideoLocals())
                    {
                        if (vid.VideoLocalID != this.VideoLocalID)
                        {
                            // make sure this folder is not the drop source
                            if (vid.ImportFolder.IsDropSource == 1)
                            {
                                continue;
                            }

                            string thisFileName = vid.FullServerPath;
                            string folderName   = Path.GetDirectoryName(thisFileName);

                            if (Directory.Exists(folderName))
                            {
                                newFullPath   = folderName;
                                foundLocation = true;
                                break;
                            }
                        }
                    }
                    if (foundLocation)
                    {
                        break;
                    }
                }

                if (!foundLocation)
                {
                    // we need to create a new folder
                    string newFolderName = Utils.RemoveInvalidFolderNameCharacters(series.GetAnime().PreferredTitle);
                    newFullPath = Path.Combine(destFolder.ImportFolderLocation, newFolderName);
                    if (!Directory.Exists(newFullPath))
                    {
                        Directory.CreateDirectory(newFullPath);
                    }
                }

                int    newFolderID       = 0;
                string newPartialPath    = "";
                string newFullServerPath = Path.Combine(newFullPath, Path.GetFileName(this.FullServerPath));

                DataAccessHelper.GetShareAndPath(newFullServerPath, repFolders.GetAll(), ref newFolderID,
                                                 ref newPartialPath);
                logger.Info("Moving file from {0} to {1}", this.FullServerPath, newFullServerPath);

                if (File.Exists(newFullServerPath))
                {
                    logger.Trace(
                        "Not moving file as it already exists at the new location, deleting source file instead: {0} --- {1}",
                        this.FullServerPath, newFullServerPath);

                    // if the file already exists, we can just delete the source file instead
                    // this is safer than deleting and moving
                    File.Delete(this.FullServerPath);

                    this.ImportFolderID = newFolderID;
                    this.FilePath       = newPartialPath;
                    VideoLocalRepository repVids = new VideoLocalRepository();
                    repVids.Save(this, true);
                }
                else
                {
                    string   originalFileName = this.FullServerPath;
                    FileInfo fi = new FileInfo(originalFileName);

                    // now move the file
                    File.Move(this.FullServerPath, newFullServerPath);

                    this.ImportFolderID = newFolderID;
                    this.FilePath       = newPartialPath;
                    VideoLocalRepository repVids = new VideoLocalRepository();
                    repVids.Save(this, true);

                    try
                    {
                        // move any subtitle files
                        foreach (string subtitleFile in Utils.GetPossibleSubtitleFiles(originalFileName))
                        {
                            if (File.Exists(subtitleFile))
                            {
                                FileInfo fiSub      = new FileInfo(subtitleFile);
                                string   newSubPath = Path.Combine(Path.GetDirectoryName(newFullServerPath), fiSub.Name);
                                if (File.Exists(newSubPath))
                                {
                                    // if the file already exists, we can just delete the source file instead
                                    // this is safer than deleting and moving
                                    File.Delete(newSubPath);
                                }
                                else
                                {
                                    File.Move(subtitleFile, newSubPath);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.ErrorException(ex.ToString(), ex);
                    }

                    // check for any empty folders in drop folder
                    // only for the drop folder
                    if (dropFolder.IsDropSource == 1)
                    {
                        foreach (
                            string folderName in
                            Directory.GetDirectories(dropFolder.ImportFolderLocation, "*",
                                                     SearchOption.AllDirectories))
                        {
                            if (Directory.Exists(folderName))
                            {
                                if (Directory.GetFiles(folderName, "*", SearchOption.AllDirectories).Length == 0)
                                {
                                    try
                                    {
                                        Directory.Delete(folderName, true);
                                    }
                                    catch (Exception ex)
                                    {
                                        logger.ErrorException(ex.ToString(), ex);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string msg = string.Format("Could not move file: {0} -- {1}", this.FullServerPath, ex.ToString());
                logger.ErrorException(msg, ex);
            }
        }
Пример #15
0
 public static string[] GetDirectories(string path)
 {
     return(Directory.GetDirectories(path));
 }
Пример #16
0
 public static string[] GetDirectories(string path, string searchPattern, SearchOption searchOption)
 {
     return(Directory.GetDirectories(path, searchPattern, searchOption));
 }