This script will iterate all folders and files from local drive(C:\Users\kak5cob\Desktop\MSIK_SCORM) and uploading into SharePoint document library with the same folder structure.
if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{
Add-PSSnapin Microsoft.SharePoint.PowerShell
}
$webUrl = "http://server:1000"
$docLibraryName = "WBT"
$RootFolderName = "WBT\Test\Test1"
$localFolderPath = "C:\Users\kak5cob\Desktop\MSIK_SCORM"
#Open web and library
$web = Get-SPWeb $webUrl
$docLibrary = $web.Lists[$docLibraryName]
function CreateDirectories($path,$folderName)
{
$fc = new-object -com scripting.filesystemobject
$localFolder = $fc.getfolder($path)
foreach ($file in $localFolder.files)
{
$file | select Path
$folderLocalPath = $localFolder.Path
$pos = $folderLocalPath.IndexOf($lclFolder.name)
$leftPart = $folderLocalPath.Substring(0, $pos)
$rightPart = $folderLocalPath.Substring($pos-1)
#$rightPart = $rightPart -replace $i.name, ""
#$rightPart = $rightPart.TrimEnd($i.name)
#Open file
$fileStream = ([System.IO.FileInfo] (Get-Item $file.path)).OpenRead()
#$parentFolder = $docLibrary.ParentWeb.GetFolder($docLibrary.RootFolder.ServerRelativeUrl+ $rightPart)
$parentFolder = $docLibrary.ParentWeb.GetFolder($RootFolderName+ $rightPart)
#Add file
$spFile = $parentFolder.Files.Add($file.Name, [System.IO.Stream]$fileStream, $true)
$spFile.Update()
write-host "File Uploaded:" $spFile.Name
#Close file stream
$fileStream.Close();
}
foreach ($i in $localFolder.subfolders)
{
$folderLocalPath = $i.Path
$pos = $folderLocalPath.IndexOf($lclFolder.name)
$leftPart = $folderLocalPath.Substring(0, $pos)
$rightPart = $folderLocalPath.Substring($pos-1)
#$rightPart = $rightPart -replace $i.name, ""
$rightPart = $rightPart.TrimEnd($i.name)
#$folder = $docLibrary.ParentWeb.GetFolder($folderName)
$folder = $docLibrary.ParentWeb.GetFolder($RootFolderName+ $rightPart)
#$subfolder = $docLibrary.AddItem($docLibrary.RootFolder.ServerRelativeUrl+ $rightPart, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $i.name)
$subfolder = $docLibrary.AddItem($RootFolderName+ $rightPart, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $i.name)
$subfolder.Update()
#Recursive call to create child folder
CreateDirectories $i.Path $i.Name
}
}
$fcObject = new-object -com scripting.filesystemobject
$lclFolder = $fcObject.getfolder($localFolderPath)
#Create a folder
$rootfolder = $docLibrary.ParentWeb.GetFolder($RootFolderName)
$RootDocfolder = $docLibrary.AddItem($rootfolder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $lclFolder.name)
$RootDocfolder.Update();
CreateDirectories $localFolderPath $lclFolder.name
if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{
Add-PSSnapin Microsoft.SharePoint.PowerShell
}
$webUrl = "http://server:1000"
$docLibraryName = "WBT"
$RootFolderName = "WBT\Test\Test1"
$localFolderPath = "C:\Users\kak5cob\Desktop\MSIK_SCORM"
#Open web and library
$web = Get-SPWeb $webUrl
$docLibrary = $web.Lists[$docLibraryName]
function CreateDirectories($path,$folderName)
{
$fc = new-object -com scripting.filesystemobject
$localFolder = $fc.getfolder($path)
foreach ($file in $localFolder.files)
{
$file | select Path
$folderLocalPath = $localFolder.Path
$pos = $folderLocalPath.IndexOf($lclFolder.name)
$leftPart = $folderLocalPath.Substring(0, $pos)
$rightPart = $folderLocalPath.Substring($pos-1)
#$rightPart = $rightPart -replace $i.name, ""
#$rightPart = $rightPart.TrimEnd($i.name)
#Open file
$fileStream = ([System.IO.FileInfo] (Get-Item $file.path)).OpenRead()
#$parentFolder = $docLibrary.ParentWeb.GetFolder($docLibrary.RootFolder.ServerRelativeUrl+ $rightPart)
$parentFolder = $docLibrary.ParentWeb.GetFolder($RootFolderName+ $rightPart)
#Add file
$spFile = $parentFolder.Files.Add($file.Name, [System.IO.Stream]$fileStream, $true)
$spFile.Update()
write-host "File Uploaded:" $spFile.Name
#Close file stream
$fileStream.Close();
}
foreach ($i in $localFolder.subfolders)
{
$folderLocalPath = $i.Path
$pos = $folderLocalPath.IndexOf($lclFolder.name)
$leftPart = $folderLocalPath.Substring(0, $pos)
$rightPart = $folderLocalPath.Substring($pos-1)
#$rightPart = $rightPart -replace $i.name, ""
$rightPart = $rightPart.TrimEnd($i.name)
#$folder = $docLibrary.ParentWeb.GetFolder($folderName)
$folder = $docLibrary.ParentWeb.GetFolder($RootFolderName+ $rightPart)
#$subfolder = $docLibrary.AddItem($docLibrary.RootFolder.ServerRelativeUrl+ $rightPart, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $i.name)
$subfolder = $docLibrary.AddItem($RootFolderName+ $rightPart, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $i.name)
$subfolder.Update()
#Recursive call to create child folder
CreateDirectories $i.Path $i.Name
}
}
$fcObject = new-object -com scripting.filesystemobject
$lclFolder = $fcObject.getfolder($localFolderPath)
#Create a folder
$rootfolder = $docLibrary.ParentWeb.GetFolder($RootFolderName)
$RootDocfolder = $docLibrary.AddItem($rootfolder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $lclFolder.name)
$RootDocfolder.Update();
CreateDirectories $localFolderPath $lclFolder.name
No comments:
Post a Comment