.ps1 code:
Add-PsSnapin Microsoft.SharePoint.PowerShell
$websiteurl = read-host "Enter site URL"
$web = Get-SPWeb $websiteurl
$LibraryName = read-host "Enter Library Name"
$docLibrary = $web.Lists[$LibraryName]
$SourceFolderURL = read-host "Enter the source folder URL"
$TargetFolderURL = read-host "Enter the target folder URL"
[string]$CurrDir=$args[0]
$logfile = $CurrDir + "\log.log"
$errorActionPreference = 'SilentlyContinue'
Function ProcessFolder([string]$sourcefolder,[string]$targetfolder)
{
write-host "Source Folder $sourcefolder"
write-host "Target Folder $targetfolder"
write-host "Copying files started..............................."
$SFolder = $web.GetFolder($sourcefolder)
$TFolder = $web.GetFolder($targetfolder)
$Sfoldername = $SFolder.name
$tfoldername = $TFolder.name
foreach ($file in $SFolder.Files)
{
try
{
$file.CopyTo("$targetfolder/"+$file.Name,$true)
[Microsoft.SharePoint.SPFile]$spFile = $web.GetFile("$targetfolder" + "/" + $file.Name)
}
# Display and log the filename which is failed to copy to destination folder
catch
{
$fname = $file.Name
Write-Error 'An error occured while copying the file '$fname 'from Folder' $sourcefolder
}
$sourceitem = $file.item
$targetitem = $spFile.item
if($spFile.Exists -eq $true)
{
try
{
$targetitem["Author"] = $sourceitem["Author"]
$targetitem["Editor"] = $sourceitem["Editor"]
$targetitem["Created"] = $sourceitem["Created"]
$targetitem["Modified"] =$sourceitem["Modified"]
$targetitem.Update()
}
# Display and log the filename which is failed to update metadata destination folder
catch
{
$fname = $file.Name
Write-Error 'An error occured while updating the metada fields for the file' $fname 'from Folder' $sourcefolder 'as the user does not exists '
}
}
}
foreach ($subfolder in $SFolder.SubFolders)
{
$subfoldername = $subfolder.name
$folderobj = $docLibrary.AddItem($TFolder.URL, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "$subfoldername")
$folderobj.Update();
$targetfolderurl = $folderobj.URL
#write-host $folderobj.URL
ProcessFolder "$websiteurl/$subfolder" "$websiteurl/$targetfolderurl"
}
}
ProcessFolder "$SourceFolderURL" "$TargetFolderURL"
Remove-PsSnapin Microsoft.SharePoint.PowerShell
Add-PsSnapin Microsoft.SharePoint.PowerShell
$websiteurl = read-host "Enter site URL"
$web = Get-SPWeb $websiteurl
$LibraryName = read-host "Enter Library Name"
$docLibrary = $web.Lists[$LibraryName]
$SourceFolderURL = read-host "Enter the source folder URL"
$TargetFolderURL = read-host "Enter the target folder URL"
[string]$CurrDir=$args[0]
$logfile = $CurrDir + "\log.log"
$errorActionPreference = 'SilentlyContinue'
Function ProcessFolder([string]$sourcefolder,[string]$targetfolder)
{
write-host "Source Folder $sourcefolder"
write-host "Target Folder $targetfolder"
write-host "Copying files started..............................."
$SFolder = $web.GetFolder($sourcefolder)
$TFolder = $web.GetFolder($targetfolder)
$Sfoldername = $SFolder.name
$tfoldername = $TFolder.name
foreach ($file in $SFolder.Files)
{
try
{
$file.CopyTo("$targetfolder/"+$file.Name,$true)
[Microsoft.SharePoint.SPFile]$spFile = $web.GetFile("$targetfolder" + "/" + $file.Name)
}
# Display and log the filename which is failed to copy to destination folder
catch
{
$fname = $file.Name
Write-Error 'An error occured while copying the file '$fname 'from Folder' $sourcefolder
}
$sourceitem = $file.item
$targetitem = $spFile.item
if($spFile.Exists -eq $true)
{
try
{
$targetitem["Author"] = $sourceitem["Author"]
$targetitem["Editor"] = $sourceitem["Editor"]
$targetitem["Created"] = $sourceitem["Created"]
$targetitem["Modified"] =$sourceitem["Modified"]
$targetitem.Update()
}
# Display and log the filename which is failed to update metadata destination folder
catch
{
$fname = $file.Name
Write-Error 'An error occured while updating the metada fields for the file' $fname 'from Folder' $sourcefolder 'as the user does not exists '
}
}
}
foreach ($subfolder in $SFolder.SubFolders)
{
$subfoldername = $subfolder.name
$folderobj = $docLibrary.AddItem($TFolder.URL, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "$subfoldername")
$folderobj.Update();
$targetfolderurl = $folderobj.URL
#write-host $folderobj.URL
ProcessFolder "$websiteurl/$subfolder" "$websiteurl/$targetfolderurl"
}
}
ProcessFolder "$SourceFolderURL" "$TargetFolderURL"
Remove-PsSnapin Microsoft.SharePoint.PowerShell
Nice one .
ReplyDeleteThank You so much!!! This was really helpful!! I really appreciate it!!
ReplyDelete