File Renamer : File Command « File Stream « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » File Stream » File CommandScreenshots 
File Renamer
     
/*  

Cafechess Library: Cover basic chess programming needs, like GUI, Engine integration, 
FEN and PGN parsing.

                 \\|//
                 (o o)
--------------ooO-(_)-Ooo----------------------------------
Copyright (C) 2009  By Gregory A. Prentice Snoqualmie, WA
                    www.cafechess.org
                    cafechess@comcast.net
                      Oooo
--------------oooO----(  )---------------------------------
              (  )    (_/
               \_)

Microsoft Public License (Ms-PL)
               
Revisions:

Author        Date    Description:

*/
using System;
using System.IO;
namespace Cafechess.IO
{
    /// <summary>
    /// Summary description for FileRenamer.
    /// </summary>
    public class FileRenamer : DirectoryWalker
    {
        string coReplaceText;
        string coReplaceWith;
        public string ReplaceText
        {
            get return coReplaceText; }
            set coReplaceText = value; }
        }
        public string ReplaceWith
        {
            get return coReplaceWith; }
            set coReplaceWith = value; }

        }
        public FileRenamer()
            : base()
        {
            FoundFileEvent += new DirectoryWalker.FoundFile(RenameFile);
            ReplaceText = "";
            ReplaceWith = "";
        }
        private void RenameFile(string name)
        {
            string newname;
            if (name.IndexOf(ReplaceText>= 0)
            {
                newname = name.Replace(ReplaceText, ReplaceWith);
                System.IO.File.Move(name, newname);
            }
        }
    }

    public class DirectoryWalker
    {
        string coDirectoryName;
        string coDirectoryPattern;
        string coFilePattern;
        bool coRecursive;
        bool coFinished;
        bool coCancel;
        public delegate void FoundFile(string fileName);
        public event FoundFile FoundFileEvent;
        public delegate void FoundDirectory(string directoryName);
        public event FoundDirectory FoundDirectoryEvent;
        public delegate void FinishedSearch();
        public event FinishedSearch FinishedSearchEvent;

        public string DirectoryName
        {
            get return coDirectoryName; }
            set coDirectoryName = value; }
        }
        public string DirectoryPattern
        {
            get return coDirectoryPattern; }
            set coDirectoryPattern = value; }
        }
        public string FilePattern
        {
            get return coFilePattern; }
            set coFilePattern = value; }
        }
        public bool Recursive
        {
            get return coRecursive; }
            set coRecursive = value; }
        }
        public bool Cancel
        {
            get return coCancel; }
            set coCancel = value; }
        }
        public bool Finished
        {
            get return coFinished; }
            set coFinished = value; }
        }

        public DirectoryWalker()
        {
            DirectoryName = System.IO.Path.DirectorySeparatorChar.ToString();
            DirectoryPattern = null;
            FilePattern = "*.*";
            Recursive = false;
            Cancel = false;
        }

        void Search(string directoryName)
        {
            try
            {
                // Find files in the current directory.
                SearchFiles(directoryName);
                if (Recursive && Cancel == false)
                {
                    foreach (string d in Directory.GetDirectories(directoryName))
                    {
                        if (FoundDirectoryEvent != null)
                            FoundDirectoryEvent(d);
                        SearchFiles(d);
                        if (Cancel == true)
                            break;
                        Search(d);
                    }
                }
            }
            catch (System.Exception excpt)
            {
                Console.WriteLine(excpt.Message);
            }
        }

        void SearchFiles(string directoryName)
        {
            if (Cancel == false)
            {
                foreach (string f in Directory.GetFiles(directoryName, FilePattern))
                {
                    if (FoundFileEvent != null)
                        FoundFileEvent(f);
                    if (Cancel == true)
                        break;
                }
            }
        }

        public void Search()
        {
            Finished = false;
            Search(DirectoryName);
            Cancel = false;
            Finished = true;
            if (FinishedSearchEvent != null)
                FinishedSearchEvent();
        }

        public void Search(string filename, bool recursive)
        {
            DirectoryName = System.IO.Path.GetDirectoryName(filename);
            FilePattern = System.IO.Path.GetFileName(filename);
            Search();
        }
        public void AddEvents(IDirectoryWalkerEvents ie)
        {
            FoundFileEvent += new DirectoryWalker.FoundFile(ie.FoundFile);
            FoundDirectoryEvent += new FoundDirectory(ie.FoundDirectory);
            FinishedSearchEvent += new FinishedSearch(ie.FinishedSearch);
        }
        public void RemoveEvents(IDirectoryWalkerEvents ie)
        {
            FoundFileEvent -= new DirectoryWalker.FoundFile(ie.FoundFile);
            FoundDirectoryEvent -= new FoundDirectory(ie.FoundDirectory);
            FinishedSearchEvent -= new FinishedSearch(ie.FinishedSearch);
        }

        public interface IDirectoryWalkerEvents
        {
            void FoundFile(string filename);
            void FoundDirectory(string directoryName);
            void FinishedSearch();
        }
    }
}

   
    
    
    
    
  
Related examples in the same category
1.C# Implementation of the File Copy Command
2.Use the FileInfo Class to Delete Files with Ease
3.File Move Implementation
4.Rename File Name
5.Rename File Extension
6.Backup File
7.Copy directory
8.Find string in a file
9.Converts a number of bytes to a shorter, more readable string representation
10.Compares the specified file1. This method accepts two strings the represent two files to compare.
11.Format File Size
12.Is File Existed
13.Is File readable or writeable
14.Replace in file
15.Send a file to the recycle bin
16.Copy file and folder
17.Gets last write time for a file.
18.Execute File
19.Gets the file size text.
20.Get File Bytes And Readable Size Information
21.Copy folder
22.Empty a Folder
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.