CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2010
    Posts
    31

    Comparing files in a directory.

    I have some code which search for mp3 files and extracts that information for each file in each folder in a directory.

    Code:
    private void ShowFiles_Click(object sender, EventArgs e)
    {
    
    foreach (string foldername in Directory.GetDirectories(DirectoryName))
                {
                    MP3Header mp3hdr = new MP3Header();
                    string Mp3Filename;
    
                   
    
                    foreach (string filename in Directory.GetFiles(foldername))
                    {
                        string extension = Path.GetExtension(filename);
                        FileInfo f = new FileInfo(filename);
    
    
                        if (extension == FileTypeExtension)
                        {
                            this.listBox1.Items.Add(filename);
                        }
    
    
                        if (f.Length <= thisFileLength)
                        {
                            this.listBox2.Items.Add(filename);
    
                        }
    
                        // Form1 tag = new Form1();
    
                        bool boolIsMP3 = mp3hdr.ReadMP3Information(filename);
    
                        this.listBoxFileName.Items.Add(mp3hdr.strFileName);
                        this.listBox3.Items.Add(mp3hdr.lngFileSize.ToString());
                        this.listBoxBitRate.Items.Add(mp3hdr.intBitRate.ToString());
                        this.listBoxFrequency.Items.Add(mp3hdr.intFrequency.ToString());
                        this.listBoxstrMode.Items.Add(mp3hdr.strMode);
                        this.listBoxstrLength.Items.Add(mp3hdr.strLengthFormatted);
    
                        Mp3Filename = filename;
    
                    }

    What would be the best way to extend this code so that it can compare files in one folder to a file in another folder, so for a duplicate finder?

  2. #2
    Join Date
    Mar 2010
    Location
    Mexico
    Posts
    9

    Re: Comparing files in a directory.

    Maybe you can create a List of PONOs and put all that info for each object in both directorys and when you find something that it's duplicated in the first List, you can add that object in other list.

    References:
    * http://ayende.com/Blog/archive/2007/...t-classes.aspx

  3. #3
    Join Date
    Jan 2010
    Posts
    31

    Re: Comparing files in a directory.

    I tried adding a list called 'Filelist'.

    Code:
    List<string> Filelist = new List<string>();
    Then trying to call a method 'CompareFile' in the foreach loop.

    Code:
    foreach (string foldername in Directory.GetDirectories(DirectoryName))
                {
                    MP3Header mp3hdr = new MP3Header();
                    string Mp3Filename;
    
    
    
                    foreach (string filename in Directory.GetFiles(foldername))
                    {
                        string extension = Path.GetExtension(filename);
                        FileInfo f = new FileInfo(filename);
    
    
                        if (extension == FileTypeExtension)
                        {
                            this.listBox1.Items.Add(filename);
                        }
    
    
                        if (f.Length <= thisFileLength)
                        {
                            this.listBox2.Items.Add(filename);
    
                        }
    
                        // Form1 tag = new Form1();
    
                        bool boolIsMP3 = mp3hdr.ReadMP3Information(filename);
    
                        this.listBoxFileName.Items.Add(mp3hdr.strFileName);
                        this.listBox3.Items.Add(mp3hdr.lngFileSize.ToString());
                        this.listBoxBitRate.Items.Add(mp3hdr.intBitRate.ToString());
                        this.listBoxFrequency.Items.Add(mp3hdr.intFrequency.ToString());
                        this.listBoxstrMode.Items.Add(mp3hdr.strMode);
                        this.listBoxstrLength.Items.Add(mp3hdr.strLengthFormatted);
    
                        Mp3Filename = filename;
    
                        Filelist.Add(mp3hdr.strFileName);
    
                        //this.listBox4.Items.Add(Filelist);
                        public void CompareFile(filename);
    
                    }
                }
    Then the method
    Code:
    public void String CompareFile(string mp3Filename)
                    {
                    foreach (string file in Filelist)
                    {
                        if(string file= mp3Filename)
                        {
                        this.listBox4.Items.Add(file);
                        }
                    }
                    }
    Then it's creating the error 'Expected class, delegate'enum'interface, or struct'.

    Any suggestions?

  4. #4
    Join Date
    Jan 2010
    Posts
    31

    Re: Comparing files in a directory.

    Does this method...

    Code:
    public void String CompareFile(string mp3Filename)
                    {
                    foreach (string file in Filelist)
                    {
                        if(string file= mp3Filename)
                        {
                        this.listBox4.Items.Add(file);
                        }
                    }
                    }
    need to be in the for loop or is it written incorrectly?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured