SharePoint - Move items in doc library from folders to root

Asked By Chester Thomas on 18-Mar-11 03:55 AM
Hi experts,

I have a document library which has folders created daily with at least 100s of documents created inside daily. How can I remove the folders and move all the documents to the root of the folder programmatically?

Chester
Sahil Kumar replied to Chester Thomas on 18-Mar-11 05:50 AM
hi

   its very important to have this programatic service which will do this for us instead of doing it manually....

this one function and a class will do this all...you just need to send your source and destination path in this function it will do all for you....

public void CopyFiles(string source, string destination)
        {
            Stack<LocRecords> direcotiresLoc = new Stack<LocRecords>();
            direcotiresLoc.Push(new LocRecords(source,destination));
            while(direcotiresLoc.Count>0)
            {
                LocRecords location = direcotiresLoc.Pop();
                string destPath=Path.Combine(location.Destiantion, Path.GetFileName(location.Source));
                foreach (string dir in System.IO.Directory.GetDirectories(location.Source))
                {                    
                    if (!System.IO.Directory.Exists(dir))
                    {
                        System.IO.Directory.CreateDirectory(destPath);
                    }
                    direcotiresLoc.Push(new LocRecords(dir, destPath));
                }

                foreach (string files in Directory.GetFiles(location.Source))
                {
                    File.Copy(files, destPath);
                }
            }
        }

public class LocRecords
    {
        public string Source { get; set; }
        public string Destiantion { get; set; }
        public LocRecords(string source, string dest)
        {
            Source = source;
            Destiantion = dest;
        }
    }

I used this stack approach because it is more faster then recursive methods. It moves files on LAN with much faster speed as compare to recursive methods.


I hope this going to help you.... Please let me know if you need my help....