-4

I'm attempting to loop through a specific directory until I find a file containing a certain substring. The file in question is an XML file used for parameter communication between Python and an Autocad C# Plugin.

Here's what I've completed so far.

  1. Create an XML file containing the required parameters in Python.

  2. Save that file as xrefTester_(currentDateTime).xml in C:\Temp\

    Here's what I still need to complete:

  3. Use an infinite loop in C# to find the created XML file.

  4. Once the file is found add .bak to the end of it so my loop knows to pass it over in the future (this is because multiple XML files containing the substring xref_tester will exist in C:\Temp\

I have the infinite loop set up and I have no problem getting the files in the directory. However I'm having trouble asserting that a file containing xrefTester and not ending in .bak exists.

asdf
  • 2,927
  • 2
  • 21
  • 42
  • Can you _show us_ the code that is giving you problems? It's easier to post your code instead of only describing it. – gunr2171 Sep 08 '14 at 20:14
  • @gunr2171 I just updated my question. I feel it was a little unclear. I have the infinite loop and I have an array containing all the files in the directory. However I'm struggling with the if statement and subsequent break. – asdf Sep 08 '14 at 20:14
  • 1
    Well, first, you don't have a question mark in your post anywhere. But you are saying you have problems with your code. Ok, _show it_. Let us help you fix your code by first letting us see it. – gunr2171 Sep 08 '14 at 20:16

1 Answers1

2

I don't know what you have tried or where you've been stuck, but i would use something like this:

var xmlFiles = from file in Directory.EnumerateFiles(@"C:\Temp", "*.xml")
               let fileName = Path.GetFileNameWithoutExtension(file)
               where fileName.StartsWith("xrefTester_(") && !fileName.EndsWith(".bak")
               select file;
foreach(string path in xmlFiles)
{
    string dir = Path.GetDirectoryName(path);
    string newFileName = string.Format("{0}{1}{2}", 
                Path.GetFileNameWithoutExtension(path),
                ".bak",
                Path.GetExtension(path));
    File.Move(path, Path.Combine(dir, newFileName)); // cannot exist due to logic above
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939