Share via

SPFile.MoveTo method (String)

Moves the file to the destination URL but does not overwrite an existing file of the same name.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)


Public Sub MoveTo ( _
    newUrl As String _
Dim instance As SPFile
Dim newUrl As String

public void MoveTo(
    string newUrl


  • newUrl
    Type: System.String

    A string that specifies the destination URL.


Exception Condition

A file of that name already exists at the specified location.


An error occurred when moving the file.


The MoveTo method moves files to another location within the same site.


The following code example moves files older than a specified date from one folder to another folder.

Dim site As SPSite = SPContext.Current.Site
Dim web As SPWeb = site.AllWebs("Site_Name/Subsite_Name")
Dim files As SPFileCollection = web.GetFolder("Source_Folder").Files
Dim i As Integer

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

    If files(i).TimeCreated < Convert.ToDateTime
        ("12/01/2004 12:00:00 AM") Then

        files(i).MoveTo("Destination_Folder/" & files(i).Name)

    End If

Next i
SPSite oSiteCollection = SPContext.Current.Site;
SPWeb oWebsite = oSiteCollection.AllWebs["Site_Name/Subsite_Name"];
SPFileCollection collFiles = oWebsite.GetFolder("Source_Folder").Files;
for (int intIndex=collFiles.Count-1; intIndex>-1; intIndex--)
    if (collFiles[intIndex].TimeCreated < 
        Convert.ToDateTime("12/01/2007 12:00:00 AM"))
        collFiles[intIndex].MoveTo("Destination_Folder/" + collFiles[intIndex].Name);


Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.

See also


SPFile class

SPFile members

MoveTo overload

Microsoft.SharePoint namespace