//a collection of the destination sub-folders
SPFolderCollection destSubFolderColl =
destSite.RootWeb.Folders[DocumentsLib].SubFolders;
//looping trough all the folders
foreach( SPFolder srcSubFolder in
srcSite.RootWeb.Folders[DocumentsLib].SubFolders )
{
if(!srcSubFolder.Name.Equals("Forms"))
{
//Getting the destinating folder for the current srcfolder. This
generates an exception
SPFolder destSubFolder = destSubFolderColl[srcSubFolder.Url];
//collection of files in the overlying folder
SPFileCollection srcSubFileColl = srcSubFolder.Files;
SPFileCollection destSubFileColl = destSubFolder.Files;
//loop trough all the files in the current filecollection
foreach( SPFile srcSubFile in srcSubFileColl)
{
//destination url
string destSubURL = srcSubFolder.Url + "/" + srcSubFile.Name;
//getting all the versions of the files
SPFileVersionCollection srcSubVersions = srcSubFile.Versions;
//looping trough all the versions of the file and adding versions to
filecollection
foreach(SPFileVersion srcSubVersion in srcSubVersions)
{
byte[] srcSubBinVersion = srcSubVersion.OpenBinary();
destSubFileColl.Add(destSubURL, srcSubBinVersion,
srcSubVersion.CreatedBy, srcSubVersion.CreatedBy, srcSubVersion.Created,
srcSubVersion.Created );
}
//open binary stream
byte[] srcSubBin = srcSubFile.OpenBinary();
//adding file to filecollection
SPFile destSubFile = destSubFileColl.Add(destSubURL, srcSubBin,
srcSubFile.ModifiedBy, srcSubFile.ModifiedBy, srcSubFile.TimeCreated,
srcSubFile.TimeLastModified);
//copy metadata by checking out and copying attributes and then
checking in the file.
destSubFile.CheckOut();
//Getting a hashtable with the metadata from the source
Hashtable hash = srcSubFile.Properties;
ICollection keys = hash.Keys;
//removing all possible metadata from the destination-file
destSubFile.Properties.Clear();
//looping through all the keys in the hashtable
foreach(object key in keys)
{
//copying the metadata
destSubFile.Properties.Add(key, hash[key].ToString());
}
destSubFile.CheckIn(STR_CHECKIN);
//deleting the initial file that was created without attributes
//destSubFile.Versions.Delete(destSubFile.Versions.Count -1);
}//foreach
}
}//foreach
}//if
}//metod-end