0

I have created some code which pulls a list of file names off the server and stores them, I also get the folder names from a directory on my computer and I need to compare them both to find out if there are any names which are on the local machine and not on the server.

serverlistarray contains the list of folders on the server (1 folder per entry), local listarray contains the directory listing on the local computer, this is also has 1 folder per entry. I read the local files using this:

String[] localfilelistarray = System.IO.Directory.GetDirectories(/*file location*/);

My problem lies within the get directory's, it includes the full path of the file which I cannot compare. The code below is what I have tried to do, I check the serverlistarray for the file in locallistarray and if they match i put them in the list.

for (int b = 0; b < localfilelistarray.Length; b++)
{
  //problem with the compare
  if (!serverlistarray.Contains(localfilelistarray[b].Replace(/*file path before filename*/, "")))
  {
    //add to list variable
  }
}

I then use this code to do what I want for everything in the list (for now its a message box to show the filename).

for (int f = 0; f < delete.Count(); f++)
{
  MessageBox.Show(/*list variable*/);
}

BUT I get every file even if it is in the serverlistarray and I cant work out whats wrong. I believe its to do with the comparison of the two arrays but when I put message boxes in that loop thay come out as expected but the comparison doesnt seem to work.

Can anyone see what ive done wrong?

ive tried using replace and direct comparisons to no avail. I though of adding the file path to the serverlistarray BUT I can not do this as I need the raw file name for server operations and other parts of this code.

EDIT:

serverlistarray example:

[0]folder1
[1]folder2
[2]folder3

localfilearray example:

[0]c:\\users\Noliver\folder1
[1]c:\\users\Noliver\folder2
[2]c:\\users\Noliver\folder3
[3]c:\\users\Noliver\folder4
[4]c:\\users\Noliver\folder5

expected output (message boxes in for loop):

c:\\users\Noliver\folder4
&
c:\\users\Noliver\folder5

EDIT 2:

Ive tried this code with no lick using the file path:

for (int b = 0; b < localfilelistarray.Length; b++)
{
  String[] temp = System.IO.Path.GetFileName(localfilelistarray[b]).Split('.');
  MessageBox.Show(localfilelistarray[b] + " - " + temp[0]);
  for (int t = 0; t < serverlistarray.Length;t++)
  {
    MessageBox.Show("server " + serverlistarray[t] + " - " + localfilelistarray[b] + " - " + temp[0]);
  }
  if (!update.Contains(temp[0]))
  {
    delete.Add(localfilelistarray[b]);
  }
}

the output from this showed that the serverlistarray contained the file name from temp[0] (the file name with the extension taken off) but all the files were in the list variable. could this come down to new lines or a space at the end of one of the strings? for some reason the if statement checking if the array contains the value doesnt work.

NoLiver92
  • 882
  • 3
  • 15
  • 39
  • Can you give an example of the contents of localfilelistarray and serverlistarray, as well as what you are expecting as a result? – Patrick Jan 13 '15 at 14:34
  • @Patrick updated code with the formats and expected output – NoLiver92 Jan 13 '15 at 14:38
  • You can use Path.GetFileName to remove the directory (this will work on folders as well as files). Also introduce some temp variables so you can see what you are comparing. – Lee Willis Jan 13 '15 at 14:43
  • @Lee this still doesnt work – NoLiver92 Jan 13 '15 at 15:01
  • If there are trailing spaces in your strings only *you* can tell us, we can't guess what data you have. – Patrick Jan 13 '15 at 15:10
  • @Patrick I understand that but is there an easy way to detect them? Using the message boxes it looks like there isnt any but I was posing a possible question why it could be failing – NoLiver92 Jan 13 '15 at 15:11
  • Why aren't you debugging your code instead? Then you can just mouse over the values and compare them? – Patrick Jan 13 '15 at 15:12
  • If you can't do that and really want to know the number of trailing spaces, you can use `int trailing = str.Length - str.TrimEnd().Length;` which will get you the number of trailing spaces of the string str. – Patrick Jan 13 '15 at 15:22
  • @Patrick ive found the problem, it relied on another if statement which was the other way around. filepath.contains(filename) so I have changed that and now it works – NoLiver92 Jan 13 '15 at 15:26

2 Answers2

0

From the way the string is stored the deepest folder name were just written as if they are a file name. So I used Path.GetFileName to extract it.

localfilearray.Where(x => !serverlistarray.Contains(Path.GetFileName(x)))

This work as well on most of case, if you don't like the name of previous one :

localfilearray.Where(x => !serverlistarray.Contains(x.Split('\\').Last()))

To use the above code with what you have now, you can :

  1. Expand your existing delete list to include what to delete :

    delete.AddRange(localfilearray.Where(x => !serverlistarray.Contains(x.Split('\\').Last())));
    
  2. Or, just declare it like this :

    var delete = new List(localfilearray.Where(x => !serverlistarray.Contains(x.Split('\\').Last())));`
    
Xiaoy312
  • 14,292
  • 1
  • 32
  • 44
  • @NoLiver92: This replaces your whole for loop in your question. You can basically replace your list with `var list = localfilearray.Where(x => !serverlistarray.Contains(Path.GetFileName(x))).ToArray()` – Patrick Jan 13 '15 at 15:23
0

I'd advise against splitting on "\" etc., instead use DirectoryInfo to get the name of the directory:

var directoryInfo = new DirectoryInfo(@"c:\MyDir");
String directoryName = directoryInfo.Name; // returns "MyDir"
BCdotWEB
  • 1,009
  • 1
  • 14
  • 35