2

I have following code

which allows me to transfer automatically one file to sharepoint. Is it possible to modify it the way it will upload every file from Temp to DL ? Thank you:

   # Set the variables
$WebURL = “http://contoso.com/spaces/”
$DocLibName = “Doc_DL”
$FilePath = “C:\Temp\PSW”

# Get a variable that points to the folder
$Web = Get-SPWeb $WebURL
$List = $Web.GetFolder($DocLibName)
$Files = $List.Files

# Get just the name of the file from the whole path
$FileName = $FilePath.Substring($FilePath.LastIndexOf("\")+1)

# Load the file into a variable
$File= Get-ChildItem $FilePath

# Upload it to SharePoint
$Files.Add($DocLibName +"/" + $FileName,$File.OpenRead(),$false)
$web.Dispose()
1

Untested, but it would be something like this:

# Set the variables
$WebURL = "http://contoso.com/spaces/"
$DocLibName = "Doc_DL"
$FilePath = "C:\Temp\PSW"

# Get a variable that points to the folder
$Web = Get-SPWeb $WebURL
$List = $Web.GetFolder($DocLibName)
$ListFiles = $List.Files

# Get just the name of the file from the whole path
$FileName = $FilePath.Substring($FilePath.LastIndexOf("\")+1)

# Load the file into a variable
$Files = Get-ChildItem $FilePath

# Upload it to SharePoint
foreach($File in $Files){
  $ListFiles.Add($DocLibName +"/" + $FileName,$File.OpenRead(),$false)
}
$web.Dispose()
  • Unfortunitly notI'v git error: a file with the name Doc_DL already exist – Adam Miklaszewski Feb 17 '17 at 14:36
  • again, it was untested, but that should be the structure, you might have to change the $false parameter to $true – Eric Alexander Feb 17 '17 at 14:42
0

I have found two intresting codes that resolve my issue:

Copy Files from folder

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

#Script settings
$webUrl = "http://vs-server12"

$docLibraryName = "Documents"
$docLibraryUrlName = "Shared%20Documents"
$localFolderPath = "C:\temp\Docs4SharePoint"

#Open web and library
$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();
}

#Dispose web

$web.Dispose()

And this one copy folders, subfolders and files.

https://gallery.technet.microsoft.com/office/Upload-local-folder-its-7c6f6a06

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.