0

This question might have been asked before, however I am not able to pass three parameters in single loop which I need for my method. I have like thousands of files in that directory (xml,jpg,tiff) (mixed).

This is something what I'm trying to get.

protected void btn_Click(object sender, EventArgs e)
        {
            var path = @"d:\xmlfiles";
            foreach (var file in Directory.GetFiles(path))
            {
                ProcessFile(file,param2,param3);
            }

        }

    static void ProcessFile(string file_xml, string file_jpg, string file_tiff)
        {
            // do processing here...
            //Adding data to sql
        }

I tried Path.GetExtension, but it only gives extension. I have to pass file name and the logic is in the method Processfile().

I saw many questions asked before which only returns single file. Any other way for a workaround?

Any helpe would be appreciated. Thanks.

6
  • You must add an example directory listing (2-3 files is enough) with the expected output. Commented Feb 11, 2019 at 13:08
  • 2
    I don't understand what these parameters should be. Are you just trying to process files with the extensions xml, gif and jpeg? Commented Feb 11, 2019 at 13:08
  • You should clarify what you expect to be in common among these different files? For example, do you have: - file1.xml - file1.jpg - file1.tiff or not necessarily? Commented Feb 11, 2019 at 13:09
  • @Steve yes. Eg. 1.xml, 1.jpg, 1.tiff Commented Feb 11, 2019 at 13:26
  • @Fabio M. Sorry I forgot to add the expectation. Commented Feb 11, 2019 at 13:31

1 Answer 1

2

So you have a directory containing files like this:

  • Foo.xml, Foo.jpg, Foo.tiff
  • Bar.xml, Bar.jpg, Bar.tiff
  • Baz.xml, Baz.jpg, Baz.tiff

And you want to process equally named files together. Then why not pick and enumerate one extension, and reconstruct the accompanying file names:

foreach (var xmlFile in Directory.GetFiles(path, "*.xml"))
{
    var extensionLess = Path.GetFilenameWithoutExtension(xmlFile);

    var jpgFile = Path.Combine(path, extensionLess + ".jpg");
    var tiffFile = Path.Combine(path, extensionLess + ".tiff");

    ProcessFile(xmlFile, jpgFile, tiffFile);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Should do the same treatment to the xml file so it doesn't have the full path. I am guessing he wants the same format for the 3 files.
@CodeCaster Thank you for big help!! and I didn't know of "GetFilenameWithoutExtension" I only tried GetFiles() which I knew from other questions. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.