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();

                    folderList = folderList.Where(r => Directory.GetDirectories(r, "*", SearchOption.TopDirectoryOnly).Length > 0).ToList();

                    listCourse = folderList.Select(r => new CourseItem()
                    {
                        CoursePath = r, Course = this.GetCourseFromDb(r)
                    }).Where(r => r.Course != null).OrderBy(r => r.Course.Title).ToList();

                    listCourse = listCourse.Where(r => Directory.GetFiles(r.CoursePath, "*.psv", SearchOption.AllDirectories).Length == r.Course.NumOfVideo).ToList();

                    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!", TextColor = Color.Green, NewLine = true
                    });

                    bgwGetCourse.ReportProgress(100);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void DoNodesForFolder(string cat, string sub)
        {
            string currPath = path + cat + "\\" + sub + "\\";

            string[] fl = Directory.GetFiles(currPath, "*.*", SearchOption.AllDirectories)
                          .Where(s => s.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase)).ToArray();
            foreach (string f in fl)
            {
                string nm = f.Substring(path.Length + 2 + cat.Length + sub.Length);
                nm = nm.Substring(0, nm.Length - 4);
                string   nameForFile  = f.Substring(0, f.Length - 4);
                string   txtFile      = nameForFile + ".txt";
                string   fullTextFile = nameForFile + " Full Text.txt";
                string[] contents;
                if (File.Exists(txtFile))
                {
                    contents = File.ReadLines(txtFile).ToArray();
                }
                else
                {
                    File.WriteAllText(txtFile, nm);
                    contents    = new string[1];
                    contents[0] = nm;
                }
                string ft = "";
                if (File.Exists(fullTextFile))
                {
                    string[] ftLines = File.ReadLines(fullTextFile).ToArray();
                    foreach (string s in ftLines)
                    {
                        ft += s + " ";
                    }
                    ft = ft.Trim();
                }
                List <string> goodTags = new List <string>();
                for (int i = 1; i < contents.Length; i++)
                {
                    if (contents[i] != "")
                    {
                        goodTags.Add(contents[i]);
                    }
                }
                files.Add(new DataFile(contents[0], goodTags.ToArray(), cat, sub, nm, ft));
            }
        }
        // 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);
            }
        }
Beispiel #4
0
        public void TestGetFilesRecursiveWithSearchWithNoResults()
        {
            var tempLongPathFilename = Path.Combine(uncDirectory, Path.GetRandomFileName());

            Directory.CreateDirectory(tempLongPathFilename);
            try
            {
                Assert.IsTrue(Directory.Exists(tempLongPathFilename));
                var randomFileName = Util.CreateNewEmptyFile(tempLongPathFilename);

                var files = Directory.GetFiles(uncDirectory, "gibberish", SearchOption.AllDirectories).ToArray();
                Assert.AreEqual(0, files.Length);
                Assert.IsFalse(files.Contains(uncFilePath));
                Assert.IsFalse(files.Contains(randomFileName));
            }
            finally
            {
                const bool recursive = true;
                Directory.Delete(tempLongPathFilename, recursive);
            }
        }
Beispiel #5
0
        public void TestGetRecursiveFilesWithSubsetSearch()
        {
            var tempLongPathFilename = Path.Combine(uncDirectory, Path.GetRandomFileName());

            Directory.CreateDirectory(tempLongPathFilename);
            try
            {
                Assert.IsTrue(Directory.Exists(tempLongPathFilename));
                var randomFileName = Util.CreateNewEmptyFile(tempLongPathFilename);

                var searchPattern = Path.GetFileName(randomFileName).Substring(0, 3) + "*" + Path.GetExtension(randomFileName);

                var files = Directory.GetFiles(uncDirectory, searchPattern, SearchOption.AllDirectories).ToArray();
                Assert.AreEqual(1, files.Length);
                Assert.IsFalse(files.Contains(uncFilePath));
                Assert.IsTrue(files.Contains(randomFileName));
            }
            finally
            {
                const bool recursive = true;
                Directory.Delete(tempLongPathFilename, recursive);
            }
        }
Beispiel #6
0
 public void TestGetFiles()
 {
     Assert.AreNotEqual(0, Directory.GetFiles(uncDirectory).Count());
     Assert.AreEqual(1, Directory.GetFiles(uncDirectory).Count());
     Assert.IsTrue(Directory.GetFiles(uncDirectory).Contains(uncFilePath));
 }
Beispiel #7
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);
            }
        }
Beispiel #8
0
 public static string[] GetFiles(string path, string searchPattern, SearchOption options)
 {
     return(Directory.GetFiles(path, searchPattern, options));
 }
Beispiel #9
0
 public static string[] GetFiles(string path, string searchPattern)
 {
     return(Directory.GetFiles(path, searchPattern));
 }
Beispiel #10
0
 public static string[] GetFiles(string path)
 {
     return(Directory.GetFiles(path));
 }