How to: Access, Copy, and Move Files

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.

Example

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

Dim myFiles As SPFileCollection = myFolder.Files
Dim file As SPFile

For Each file In  myFiles

    Response.Write(file.Url.ToString() & "<BR>")
    Response.Write(file.Length.ToString() & "<BR>")

Next file
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 mySite As SPWeb

Private Sub Page_Load(sender As Object, e As System.EventArgs)

    mySite = SPControl.GetContextWeb(Context)

End Sub 'Page_Load

Private Sub Button1_Click(sender As Object, e As System.EventArgs)

    Dim maxSize As Integer = Convert.ToInt32(TextBox1.Text)
    Dim myFolder As SPFolder = mySite.GetFolder("Shared Documents")
    Dim myFiles As SPFileCollection = myFolder.Files
    Dim file As SPFile

    For Each file In  myFiles

        If file.Length > maxSize * 1024 Then

            Response.Write(SPEncode.HtmlEncode(file.Name) & " :: " 
                & file.Length / 1024 & "kb<BR>")
            file.CopyTo("Archive/" & file.Name, True)

        End If

    Next file

End Sub 'Button1_Click
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.

Dim mySite As SPWeb = SPControl.GetContextWeb(Context)
Dim myFolder As SPFolder = mySite.GetFolder("Shared Documents")
Dim myFiles As SPFileCollection = myFolder.Files
Dim i As Integer

For i = myFiles.Count - 1 To 0 Step -1

    myFiles(i).MoveTo("StorageFolder/" & myFiles(i).Name, True)

Next i
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.

See Also

Reference

Microsoft.SharePoint

Concepts

Working with List Objects and Collections

Getting Started with Programmatically Customizing a SharePoint Web Site in Visual Studio

Security Validation and Making Posts to Update Data

Elevation of Privilege