Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *
- "*.abc" returns files having an extension of .abc, .abcd, .abcde, .abcdef, and so on.
- *
- "*.abcd" returns only files having an extension of .abcd.
- *
- "*.abcde" returns only files having an extension of .abcde.
- *
- "*.abcdef" returns only files having an extension of .abcdef.
- private bool StriktMatch(string fileExtension, string searchPattern)
- {
- bool isStriktMatch = false;
- string extension = searchPattern.Substring(searchPattern.LastIndexOf('.'));
- if (String.IsNullOrEmpty(extension))
- {
- isStriktMatch = true;
- }
- else if (extension.IndexOfAny(new char[] { '*', '?' }) != -1)
- {
- isStriktMatch = true;
- }
- else if (String.Compare(fileExtension, extension, true) == 0)
- {
- isStriktMatch = true;
- }
- else
- {
- isStriktMatch = false;
- }
- return isStriktMatch;
- }
- class Program
- {
- static void Main(string[] args)
- {
- string[] fileNames = Directory.GetFiles("C:\document", "*.abc");
- ArrayList al = new ArrayList();
- for (int i = 0; i < fileNames.Length; i++)
- {
- FileInfo file = new FileInfo(fileNames[i]);
- if (StriktMatch(file.Extension, "*.abc"))
- {
- al.Add(fileNames[i]);
- }
- }
- fileNames = (String[])al.ToArray(typeof(String));
- foreach (string s in fileNames)
- {
- Console.WriteLine(s);
- }
- Console.Read();
- }
- List<string> fileNames = new List<string>();
- // populate all filenames here with a Directory.GetFiles or whatever
- string srcDir = "from"; // set this
- string destDir = "to"; // set this too
- // this filters the names in the list to just those that end with ".doc"
- foreach (var f in fileNames.All(f => f.ToLower().EndsWith(".doc")))
- {
- try
- {
- File.Copy(Path.Combine(srcDir, f), Path.Combine(destDir, f));
- }
- catch { ... }
- }
- string strSomePath = "c:\SomeFolder";
- string strSomePattern = "*.abc";
- string[] filez = Directory.GetFiles(strSomePath, strSomePattern);
- var filtrd = from f in filez
- where f.EndsWith( strSomePattern )
- select f;
- foreach (string strSomeFileName in filtrd)
- {
- Console.WriteLine( strSomeFileName );
- }
- string[] fileList = Directory.GetFiles(path, "*.abc");
- foreach (string file in fileList)
- {
- FileInfo fInfo = new FileInfo(file);
- if (fInfo.Extension.Length == 4) // "." is counted in the length
- {
- // exact extension match - process the file...
- }
- }
Add Comment
Please, Sign In to add comment