1

In SP2010, I'm trying to move files from one library to another using Powershell. The condition for file move is that the files should be greater than 3 years/1095 days from the date of creation.

I'm using the below script to first copy the files from source to destination and then delete the source file.

This works perfectly for files which passes the condition that is for files which are greater than 1095 days, the file gets copied to destination and then gets deleted in source.

But when the loop faces the 1st file which doesn't pass the condition, the loop starts repeating infinitely and doesn't move to the next document. What is that I'm missing?

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Clear-Content -path "C:\temp\MovedFiles.csv"
$OuputFile = "C:\temp\MovedFiles.csv" 
#Custom Function to Copy Files from Source Folder to Target
Function Copy-Files($SourceFolder, $TargetFolder)
{
write-host "Copying Files from:$($SourceFolder.URL) to $($TargetFolder.URL)"
#Get Each File from the Source
$SourceFilesColl = $SourceFolder.Files
write-host "Collection count:" $SourceFilesColl.Count
#Iterate through each item from the source
for($i = 0; $i -lt $SourceFilesColl.Count)
{
 $SourceFile=$SourceFilesColl[$i]
 write-host "Source File:" $SourceFile
 $today = get-date -Format "MM/dd/yyyy"
 $Created= $SourceFile.TimeCreated
 $TS = New-Timespan -start $Created -End $today
 $datedifference = $ts.days

 if($datedifference -gt 1095)
 {
    #Copy File from the Source
 Write-host "Coping File:"$SourceFile.Name
 $NewFile = $TargetFolder.Files.Add($SourceFile.Name, $SourceFile.OpenBinary(),$True)
  Foreach($Field in $SourceFile.Item.Fields)
 {
    If(!$Field.ReadOnlyField)
    {
        if($NewFile.Item.Fields.ContainsField($Field.InternalName))
        {
            $NewFile.Item[$Field.InternalName] = $SourceFile.Item[$Field.InternalName]
        }
    }
}

 #Update
$NewFile.Item.UpdateOverwriteVersion()
Write-host "Copied File:"$SourceFile.Name
$movedfiles= Add-Content -Path C:\temp\MovedFiles.csv -Value "$($SourceFile.Name.ToString())"

 $SourceFile.Delete()
 Write-host "Deleted File from source:"$SourceFile.Name 
 } 
 else
{
 write-host "This file not moved:" $SourceFile.Name
}
} 
}
#Variables for Processing
$WebURL="SiteURL"
$SourceLibrary ="SourceLibrary"  
$TargetLibrary = "TargetLibrary"

#Get Objects
$Web = Get-SPWeb $WebURL
$SourceFolder = $Web.GetFolder($SourceLibrary)
$TargetFolder = $Web.GetFolder($TargetLibrary)

#Call the Function to Copy All Files
Copy-Files $SourceFolder $TargetFolder

$tempCSV = Import-Csv C:\temp\MovedFiles.csv -Header "File" | select -skip 0
$tempCSV | Export-CSV C:\temp\MovedFiles.csv -NoTypeInformation
  • can you try ++$i in the else condition? so that $i is value change – Waqas Sarwar MVP Apr 17 at 12:38
  • 1
    @WaqasSarwarMVP great. that worked :) – George Apr 17 at 12:47
  • i added that part as answer if you marked it that will be helpful for everyone. – Waqas Sarwar MVP Apr 17 at 13:10
1

looks like their is no increment for the $i variable in the Else condition. so thats why it stuck on the same item all the item.

Can you put this in else condition.

++$i
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.