locked
accept multiple file formats in my dirinfo.GetFiles method RRS feed

  • Question

  • User-1660589204 posted

    I have a code which currently accepts .trm file formats, reads them and processes, I got a task to update the code to accept

    .wav, mp3, mp4, m4a file formats as well. I am using :

    FileInfo[] fileInfos = dirinfo.GetFiles("*.trm");

    to read only the trm files. I now need to include other formats as wav, mp3,mp4 and m4a as well. My full code is:

    public async Task PostValuesIntoDB(string strAccessToken)
            {
                try
                {
                    string strGetTodayDate = DateTime.Now.ToString("yyyyMMdd");
                    DirectoryInfo info = new DirectoryInfo(path + "\\" + strGetTodayDate + "\\");
                    if (info.Exists)
                    {
                        DirectoryInfo[] dirFolders = info.GetDirectories();
                        if (dirFolders != null || dirFolders.Length > 0)
                        {
                            foreach (DirectoryInfo dirinfo in dirFolders)
                            {
                                FileInfo[] fileInfos = dirinfo.GetFiles("*.trm");
                                if (fileInfos != null || fileInfos.Length > 0)
                                {
                                    filesLegnth = fileInfos.Length;
                                    foreach (FileInfo fileInfo in fileInfos)
                                    {
                                        RsbfileDetails detail = new RsbfileDetails();
                                        detail.Filename = fileInfo.Name;
                                        detail.FileUrl = fileInfo.FullName;
                                        detail.FileType = fileInfo.Extension;
                                        Guid obj = Guid.NewGuid();
                                        detail.CreatedDate = fileInfo.CreationTime;
                                        detail.FileReferenceId = obj.ToString();
                                        await FileUpload(strAccessToken, detail);
                                        Task.Factory.StartNew(() => GetFileTranscodeJobID(accessToken, detail));
                                        Task.Factory.StartNew(() => GetJobID(accessToken, detail));
                                        var res = await _rSBRepository.Add(detail);
                                    }
    
                                }
                                else
                                {
                                    Console.WriteLine("No trm file in destination folder");
                                }
                            }
                            Console.ReadLine();
    
                        }
                    }
    
                }
                catch (Exception ex)
                {
    
                }
            }

    GetFiles need to accept multiple input formats.

     

    Thursday, December 10, 2020 7:15 AM

All replies

  • User753101303 posted

    Hi,

    AFAIK not out of the box but you could loop on patterns for example using Linq:

    string[] patterns= {"*.trm", "*.wav", "*.mp3","*.mp4","*.m4a"};
    FileInfo[] fileInfos = filters.SelectMany(pattern => dirinfo.GetFiles(pattern)).ToArray();
    Thursday, December 10, 2020 10:57 AM
  • User303363814 posted

    You can remove the nested loops and also look for a list of extensions like this

    public async Task PostValuesIntoDB(string strAccessToken)
    {
       var today = DateTime.Now.ToString("yyyyMMdd");
       var rootDir = Path.Combine(path, today);
       var desired = new string[] {".trm", ".wav", ".mp3", etc};
       foreach (var file in Directory
                              .EnumerateFiles(rootDir, "*", SearchOptions.AllDirectories)
                              .Where(d => desired.Contains(Path.GetExtension(d)))
       {
          var fi = new FileInfo(file);
          var detail = new RsbfileDetails();
          detail.Filename = fi.Name;
          detail.FileUrl = fi.FullName;
          detail.FileType = fi.Extension;
          // etc
    } }

    Friday, December 11, 2020 2:55 AM