0

I have the below script that currently only uploads the content of a single folder. However I have ~300 folders to upload and changing the folder name each time before running the script is not very practical. Is there a way to upload the documents (with or with out the folders)?


Current Script:

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}



$webUrl = "WEBSITE URL"

$docLibraryName = "Documents"

$docLibraryUrlName = "Shared%20Documents"

$localFolderPath = "C:\PARENT\CHILD FOLDER\"


$web = Get-SPWeb $webUrl

$docLibrary = $web.Lists[$docLibraryName]

$files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles()

ForEach($file in $files)
{

    #Open file
    $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

    #Add file
    $folder =  $web.getfolder($docLibraryUrlName)

    write-host "Copying file " $file.Name " to " $folder.ServerRelativeUrl "..."
    $spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)
    write-host "Success"

    #Close file stream
    $fileStream.Close();
}

$web.Dispose()
  • Do you want to create folder as well in to document library? – Kalpesh Vaghela Jun 26 '15 at 7:08
  • Ideally it wouldn't have folders so the script you provided works perfectly. – Roland Jun 29 '15 at 23:36
2

Below script will upload file recursively from folder to document library

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{
     Add-PSSnapin Microsoft.SharePoint.PowerShell
}

Function UploadFiles($web,$path,$docLibrary)
{
$files = Get-ChildItem $path

foreach ($file in $files) 
{  

   if($file.GetType().Name -eq "DirectoryInfo") 
   {
        #Item Is Folder
        Write-Host  $file.FullName
        Write-Host "Calling inner function"
        UploadFiles $web $file.FullName $docLibrary
   }
   else
   {
        #Item is File
        Write-Host  $file.FullName
         #Open file
        $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

        #Add file
        $folder =  $web.getfolder($docLibraryUrlName)

        write-host "Copying file " $file.Name " to " $folder.ServerRelativeUrl "..."
        $spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)
        write-host "Success"

        #Close file stream
        $fileStream.Close();
   }

}
}
$webUrl = "WEBSITE URL"
$docLibraryName = "Documents"
$docLibraryUrlName = "Shared%20Documents"
$localFolderPath = "C:\FolderWhichContainsAllFilesWithSubFolder\"
$web = Get-SPWeb $webUrl
$docLibrary = $web.Lists[$docLibraryName]
UploadFiles $web $localFolderPath $docLibrary
$web.Dispose()

Hope it will help to you.

  • Sorry for late comment (Had sort out other back end stuff so the server could talk to each other) When I run this script it doesn't execute. it goes into a >> mode where I can continue writing after doing a copy paste into it (after putting in my server details, etc). How do you get out of the >> so the code can run? – Roland Jul 14 '15 at 2:56
  • Moved the following lines above the function call. Works fine now. $webUrl = "WEBSITE URL" $docLibraryName = "Documents" $docLibraryUrlName = "Shared%20Documents" $localFolderPath = "C:\FolderWhichContainsAllFilesWithSubFolder\" – Roland Jul 14 '15 at 3:58
  • Ok.. good. :):) – Kalpesh Vaghela Jul 14 '15 at 4:02

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.