Page 1 of 1

File mask

Posted: Fri Sep 24, 2010 6:35 pm
by RBygrave
In the first version of the libary I had I do do a file mask on the file listing.

Now I have upgraded I no longer see the overloaded function GetFiles()

So now how do I apply a mask such as zbc*.t* to a directory listing?

Thanks

Re: File mask

Posted: Fri Sep 24, 2010 6:36 pm
by mb
You need to filter the files yourself. What programming language do you use?

Re: File mask

Posted: Fri Sep 24, 2010 7:33 pm
by RBygrave

You need to filter the files yourself. What programming language do you use?

I use C#. What happened to the functionality that was in the libary before? I now have legacy code that will not upgrade without modifications.

Re: File mask

Posted: Sat Sep 25, 2010 6:37 am
by mb
Maybe you mean the ReadDirectoryFromCommand method.

If your server supports globbing you can use the function as following"
ReadDirectoryFromCommand("LIST zbc*.t*")
But the safer way is to manually enumerate the directory (see below).

Re: File mask

Posted: Sat Sep 25, 2010 6:49 am
by mb
For file globbing you can use the following function. Credits go to to Erwin at http://www.codeproject.com/KB/string/wildcmp.aspx

Code: Select all

   



public static class StringExtensions

{

	public static bool WildcardMatch(this string str, string compare, bool ignoreCase)

	{

		if (ignoreCase)

			return str.ToLower().WildcardMatch(compare.ToLower());

		else

			return str.WildcardMatch(compare);

	}



	public static bool WildcardMatch(this string str, string compare)

	{

		if (string.IsNullOrEmpty(compare))

			return str.Length == 0;

		int pS = 0;

		int pW = 0;

		int lS = str.Length;

		int lW = compare.Length;

		while (pS < lS && pW < lW && compare[pW] != '*')

		{

			char wild = compare[pW];

			if (wild != '?' && wild != str[pS])

				return false;

			pW++;

			pS++;

		}

		int pSm = 0;

		int pWm = 0;

		while (pS < lS && pW < lW)

		{

			char wild = compare[pW];

			if (wild == '*')

			{

				pW++;

				if (pW == lW)

					return true;

				pWm = pW;

				pSm = pS + 1;

			}

			else if (wild == '?' || wild == str[pS])

			{

				pW++;

				pS++;

			}

			else

			{

				pW = pWm;

				pS = pSm;

				pSm++;

			}

		}

		while (pW < lW && compare[pW] == '*')

			pW++;

		return pW == lW && pS == lS;

	}

}

Re: File mask

Posted: Sat Sep 25, 2010 9:31 pm
by RBygrave
This will work for me, Thanks a lot.

In the first version there was a overloaded function called GetFiles(). You could pass in a wildcard as a parameter and it would only get the matches.

Richard