0

I would like to verify if a file exists on multiple servers. The only thing that's different on the servers is the number on the server, ex. Server1, Server2, Server3, etc.

How would I write a loop that replaces each number with the next highest number up until, say Server 10?

Here is what I have so far:

var fileLocation = @"\\Server1\documents\File.txt";
var newFileInfoTest = fileLocation.Replace("Server1", "Server2");

if (File.Exists(newFileInfoTest))
    txtTextBox.Text = "Server1 -- File copy successful.";
else
    txtTextBox.Text = "Server1 -- File copy unsuccessful";
1
  • 2
    There is no loop in your code example. Please provide the code you need help with. Commented Oct 4, 2019 at 17:43

4 Answers 4

3

"How would I write a loop that replaces each number with the next highest number up until, say Server 10?"

You can set the server name in a loop, using the loop iterator value as part of the server name

for(int i = 1; i <= 10; i++)
{
    txtTextBox.Text = File.Exists($@"\\Server{i}\documents\File.txt")
        ? $"Server{i} -- File copy successful."
        : $"Server{i} -- File copy unsuccessful";
}

Note that the code above will overwrite the txtTextBox.Text value on each iteration. You may instead want to capture all the statuses in the loop and then display them at the end:

txtTextBox.Text = string.Join(Environment.NewLine, Enumerable.Range(1, 10)
    .Select(i => File.Exists($@"\\Server{i}\documents\File.txt")
        ? $"Server{i} -- File copy successful."
        : $"Server{i} -- File copy unsuccessful."));

In the comments you asked:

"How would you do this if the file location was in a variable?"

One way to do this is to use a format string with a placeholder ({0}) where the number would go, and then use string.Format to fill in that placeholder inside the loop.

We can extract the server name from this string using string.Split on the \ character and grabbing the first item.

For example:

var serverPath = @"\\Server{0}\documents\File.txt";

txtTextBox.Text = string.Join(Environment.NewLine, Enumerable.Range(1, 10)
    .Select(i =>
    {
        var thisPath = string.Format(serverPath, i);
        var serverName = thisPath.Split(new[] { '\\' },
            StringSplitOptions.RemoveEmptyEntries).First();
        return File.Exists(thisPath)
            ? $"{serverName} -- File copy successful."
            : $"{serverName} -- File copy unsuccessful.";
    }));
Sign up to request clarification or add additional context in comments.

4 Comments

Actually, I did the same:)
Thanks @RufusL, this was what I was looking for. How would you do this if the file location was in a variable? Or would that be a different question to ask?
See my answer for the variable version.
I posted a sample that uses a variable to store the template for the server path, if that helps.
1

You can do something like this, make sure your message will not get over write, you can use string builder instead of concatenation. hope you can get the logic

    var msg = string.Empty;
    for(int i = 1; i <  11; i++) { 
        var fileLocation = $"\\Server{i}\documents\File.txt";

         if (File.Exists(fileLocation))
         {
           msg += $"Server{i} -- File copy successful.";
         }
         else
         {
           msg += $"Server{i} -- File copy unsuccessful."; 
         }
    }

    txtTextBox.Text = msg;

1 Comment

@SQLPolice - Right, the first answer will be always in hurry!
0

Try this:

for (int i = 0; i < 11; i++){
 var fileLocation = $@"\\Server{i}\documents\File.txt";

 if (File.Exists(fileLocation))
    txtTextBox.Text = $"Server{i} -- File copy successful.";
 else
    txtTextBox.Text = $"Server{i} -- File copy unsuccessful";
 }
}

Comments

0

"How would I write a loop that replaces each number with the next highest number up until, say Server 10?"

var numberofServers = 10;
for(int i =0; i <= numberOfServers; i++){
var fileLocation = $"\\Server{i}\\documents\\File.txt";

if(File.Exists(fileLocation)){
//Success
}    
else{
//UnSuccessful
}
}

Comments

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.