Reputation: 1715
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";
Upvotes: 0
Views: 397
Reputation: 9764
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;
Upvotes: 1
Reputation: 37070
"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.";
}));
Upvotes: 3
Reputation: 652
"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
}
}
Upvotes: 0
Reputation: 1791
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";
}
}
Upvotes: 0