1

I wrote this code in c# for change location of folder and all sub-folders of this folder. I want to change name of all folders when copy it to destination. for example I want to change name from 1 to .... . how should I change my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace Folder
{
    class clssMoveFolder
    {

        string temppath;
        public string StrtxtSource;
        public string destDirName;

        public void Directorycopy(string sourceDirName, string destDirName, bool cutSubDirs, string strExtension)
        {
           try
            {

                DirectoryInfo dir = new DirectoryInfo(sourceDirName);
                DirectoryInfo[] dirs = dir.GetDirectories();
                // If the source directory does not exist, throw an exception.

                if (!dir.Exists)
                {
                    throw new DirectoryNotFoundException("Source directory does not exist or could not be found: " + sourceDirName);
                }
                FileInfo[] files = dir.GetFiles();
                if (!Directory.Exists(destDirName))
                {
                    Directory.CreateDirectory(destDirName);
                }
                //Get the file contents of the directory to copy.
                for (int a = 0; a < files.Length; ++a)
                {
                    // Create the path to the new copy of the file.
                    if (files[a].Extension == "."+strExtension  )
                    {
                        temppath = Path.Combine(destDirName, files[a].Name);
                        files[a].MoveTo(temppath);
                    }
                    else if (files[a].Extension == strExtension)
                    {
                        temppath = Path.Combine(destDirName, files[a].Name);
                        files[a].MoveTo(temppath);
                    }
                    else if (strExtension == "AllFiles")
                    {
                        temppath = Path.Combine(destDirName, files[a].Name);
                        files[a].MoveTo(temppath);
                    }
                    else
                        files[a].Delete();
                   dir.Refresh();
                }
                FileInfo[] filesCheck = dir.GetFiles();
                if (dirs.Length == 0 && filesCheck.Length == 0 && dir.Name != StrtxtSource)
                {
                    dir.Delete();
                }

                // If copySubDirs is true, copy the subdirectories.
                if (cutSubDirs)
                {

                    foreach (DirectoryInfo subdir in dirs)
                    {

                        // Copy the subdirectories.

                        string temppath= Path.Combine(destDirName, subdir.Name);

                        Directorycopy(subdir.FullName, temppath,cutSubDirs,strExtension);
                    }
                }
            }
            catch (Exception e)
            {

                MessageBox.Show(e.ToString());
            }
        }

    }
}
Micle
  • 11
  • 1
  • 2
    Have you read [this answer](http://stackoverflow.com/questions/3218910/rename-a-file-in-c-sharp)? – RaidenF Feb 24 '16 at 12:02
  • @K.Gkinis Yes, but I need a useful technique to automatically set from 1 to .... – Micle Feb 24 '16 at 12:03
  • I really do not understand. Do you want to rename a file from 1 to something else? If you want to rename something to "...." you can't in Windows (and most Operating Systems) – RaidenF Feb 24 '16 at 12:05
  • no @K.Gkinis for example we have three folder A,B, and C. I want to move these folders to another path and change names to 1, 2, 3. – Micle Feb 24 '16 at 12:07
  • it can be acheived using simple for loop and this System.IO.File.Move("oldfilename", "newfilename"); – Dah Sra Feb 24 '16 at 12:11
  • @dahsra It doesn't very simple for me. infact I confused which part of my code would be replaced ! – Micle Feb 24 '16 at 12:16

1 Answers1

0

If you just want to rename a file after copying it you could instead directly copy it under a different name. E.g. instead of copying a.txt and rename it to b.txt afterwards, you could just copy the original a.txt as b.txt to the destination directory.

Renaming can be done by using the File.Move method.

File.Move("a.txt", "b.txt");

If you want to be able to do this even if someone else copies files into a directory (e.g. by using the windows explorer or files written by another application), you may want to take a closer look at the FileSystemWatcher. You can use it to monitor changes in a specified directory (optionally including it's subdirectories) and react on these changes.

...
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"D:\SomeTestDirectory";
watcher.Filter = "*.*";
watcher.NotifyFilter = NotifyFilters.FileName; // Trigger only on events associated with the filename
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;            // Starts the "watching"
...

private static void OnChanged(object source, FileSystemEventArgs e)
{
   // Do whatever you want here, e.g. rename the file.
   Console.WriteLine("File: " +  e.FullPath + " " + e.ChangeType);
}
Markus Safar
  • 6,324
  • 5
  • 28
  • 44
  • Is there anyway to modify the above code that mentioned by OP ? the code mentioned in the question is complex. –  Feb 24 '16 at 14:55