This documentation is archived and is not being maintained.

How to: Access, Copy, and Move Files

Windows SharePoint Services 3

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

You can use the GetFolder method of the SPWeb class to return a specified folder and then access individual files in the folder. After instantiating an SPWeb object (for example, as mySite), use SPFolder myFolder = mySite.GetFolder("Shared Documents") (in Visual Basic 2005, use Dim myFolder As SPFolder = mySite.GetFolder("Shared Documents")) to return the Shared Documents folder for the site.

The following example returns the collection of files in the folder and displays information about the files.

SPFileCollection myFiles = myFolder.Files;

foreach (SPFile file in myFiles)
{
   Response.Write(file.Url.ToString() + "<BR>");
   Response.Write(file.Length.ToString() + "<BR>");
}

The previous example lists the URL and size of every file within the folder.

The example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint namespace.

To copy files from one location to another, use one of the CopyTo methods of the SPFile class. In the following example, the Page_Load event handler instantiates an SPWeb object for the current site context. The Click event handler iterates through all the files in the folder, listing the name and size (in kilobytes) of each file that exceeds a multiple of the value specified by the user in a text box, and copying the file to a folder named Archive.

private SPWeb mySite;

private void Page_Load(object sender, System.EventArgs e)
{
   mySite = SPControl.GetContextWeb(Context);
}

private void Button1_Click(object sender, System.EventArgs e)
{
   int maxSize = Convert.ToInt32(TextBox1.Text);

   SPFolder myFolder = mySite.GetFolder("Shared Documents");
   SPFileCollection myFiles = myFolder.Files;

   foreach (SPFile file in myFiles)
   {

      if (file.Length>(maxSize*1024))
      {
         Response.Write(SPEncode.HtmlEncode(file.Name) + ": " 
             + file.Length/1024 + "kb<BR>");
         file.CopyTo("Archive/" + file.Name, true);
      }
   }
}

In the example, the CopyTo method uses two parameters, one that specifies the destination URL for the copied file, and the other a Boolean value that specifies whether to overwrite any file of the same name that is located at the destination.

The previous example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint, Microsoft.SharePoint.Utilities, and Microsoft.SharePoint.WebControls namespaces.

The following example moves all files from the Shared Documents list of the current site to another folder named StorageFolder, overwriting any file of the same name that may be located there.

SPWeb mySite = SPControl.GetContextWeb(Context);
SPFolder myFolder = mySite.GetFolder("Shared Documents");
SPFileCollection myFiles = myFolder.Files;

for (int i = myFiles.Count - 1; i > -1; i--)
{
    myFiles[i].MoveTo("StorageFolder/" + myFiles[i].Name, true);
}

As the example illustrates, when collections are modified in the course of code execution by deleting or moving items, the counter for iterating through the collection must decrease in value with each iteration.

The previous example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint and Microsoft.SharePoint.WebControls namespaces.

Show: