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()
2
  • Do you want to create folder as well in to document library? Commented Jun 26, 2015 at 7:08
  • Ideally it wouldn't have folders so the script you provided works perfectly. Commented Jun 29, 2015 at 23:36

1 Answer 1

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.

3
  • 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? Commented Jul 14, 2015 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\" Commented Jul 14, 2015 at 3:58
  • Ok.. good. :):) Commented Jul 14, 2015 at 4:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.