File mask

This forum is not monitored by the SmartFTP staff. If you have a license please contact technical support by email.
Post Reply
RBygrave
Posts: 24
Joined: Mon Jan 14, 2008 8:36 pm

File mask

Post by RBygrave » Fri Sep 24, 2010 6:35 pm

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

mb
Posts: 12030
Joined: Thu Oct 11, 2001 1:24 pm
Display name: Mat (SmartFTP)
Contact:

Re: File mask

Post by mb » Fri Sep 24, 2010 6:36 pm

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

RBygrave
Posts: 24
Joined: Mon Jan 14, 2008 8:36 pm

Re: File mask

Post by RBygrave » Fri Sep 24, 2010 7:33 pm


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.

mb
Posts: 12030
Joined: Thu Oct 11, 2001 1:24 pm
Display name: Mat (SmartFTP)
Contact:

Re: File mask

Post by mb » Sat Sep 25, 2010 6:37 am

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).
Last edited by mb on Sat Sep 25, 2010 7:04 am, edited 1 time in total.

mb
Posts: 12030
Joined: Thu Oct 11, 2001 1:24 pm
Display name: Mat (SmartFTP)
Contact:

Re: File mask

Post by mb » Sat Sep 25, 2010 6:49 am

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;

	}

}
Last edited by mb on Sat Sep 25, 2010 7:04 am, edited 1 time in total.

RBygrave
Posts: 24
Joined: Mon Jan 14, 2008 8:36 pm

Re: File mask

Post by RBygrave » Sat Sep 25, 2010 9:31 pm

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

Post Reply