question

Jay-8728 avatar image
0 Votes"
Jay-8728 asked ·

Rename a file with webClient (or other)

I am downloading a file from a SharePoint document library to the file system using $webclient.DownloadFile($ServerFileLocation,$DownloadPath)
Once downloaded I would like to rename the file in SharePoint. I see no such method in webClient and wondered what the best way to go is?

office-sharepoint-server-administrationwindows-server-powershell
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

sharatha avatar image
0 Votes"
sharatha answered ·

try with the below client code:

 string filename = "YourFileName.extension";
 Response.AppendHeader("Content-Disposition","attachment; filename="+ filename  +"");

https://www.codeproject.com/Questions/508368/Topluschangeplusfileplusnameplusafterplusdownloadi

Thanks & Regards,

· 3 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Is this renaming the downloaded file or the file (source) in the sharepoint library?

0 Votes 0 ·

that should rename the file after download, check the link I shared on the above reply.

Thanks & Regards,

0 Votes 0 ·

I don't want to rename the file I have downloaded, I want to rename the file on the sharepoint server.
Locate file on SharePoint server (done)
Download a copy of that file to the file system (done)
Rename the original file in the sharepoint library (to do)

0 Votes 0 ·
sharatha avatar image
0 Votes"
sharatha answered ·

Gotcha, So you want to download files from the library to local system and then rename the files in a Library.

Below script will download all files from library to your local system:

 #Config Variables
 $SiteURL = "https://crescenttech.sharepoint.com/sites/marketing"
 $FileRelativeURL = "/sites/Marketing/Shared Documents/Discloser Asia.docx"
 $DownloadPath ="C:\Temp"
     
 #Get Credentials to connect
 $Cred = Get-Credential
     
 Try {
     #Connect to PNP Online
     Connect-PnPOnline -Url $SiteURL -Credentials $Cred
         
     #powershell download file from sharepoint online
     Get-PnPFile -Url $FileRelativeURL -Path $DownloadPath -AsFile
 }
 catch {
     write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
 }

This below script will rename all files from Library.

 #Config Variables
 $SiteURL = "https://crescent.sharepoint.com/sites/marketing"
 $LibraryName = "Documents"
      
 #Connect to PnP Online
 Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
     
 #Get All Files from the document library
 $Files = Get-PnPListItem -List $LibraryName -PageSize 1000 | Where {$_["FileLeafRef"] -like "*.*"}
      
 #Loop through each File
 Write-host -f Green "Number of Files Found:"$Files.Count
 ForEach($File in $Files)
 { 
     Write-Host ("Renaming File '{0}' at {1}" -f $File["FileLeafRef"], $File["FileRef"])
     #Rename File
     Rename-PnPFile -ServerRelativeUrl $File["FileRef"] -TargetFileName "Old_$($File['FileLeafRef'])" -OverwriteIfAlreadyExists -Force
 }

Reference:
https://www.sharepointdiary.com/2016/09/sharepoint-online-download-file-from-library-using-powershell.html
https://www.sharepointdiary.com/2017/10/sharepoint-online-rename-files-in-document-library-using-powershell.html

Thanks & Regards,

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AmosWu-MSFT avatar image
0 Votes"
AmosWu-MSFT answered ·

If you could accept not to use WebClient, you can use PowerShell CSOM to download the file and rename the file in one method.

 Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
 Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
 Function DownloadAndRename-FileFromLibrary()
 { 
     param
     (
         [Parameter(Mandatory=$true)] [string] $SiteURL,
         [Parameter(Mandatory=$true)] [string] $SourceFile,
         [Parameter(Mandatory=$true)] [string] $TargetFile,
         [Parameter(Mandatory=$true)] [string] $NewFileUrl
     )
  
     Try {
         #Setup Credentials to connect
         $username = "amos@contoso.onmicrosoft.com"
         $password = "password"
         $Cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $(convertto-securestring $Password -asplaintext -force)
         $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
         #if you are using SharePoint On premise,just delete the above Credentials script
         #Setup the context
         $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
         $Ctx.Credentials = $Credentials

         $FileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($Ctx,$SourceFile)
         $WriteStream = [System.IO.File]::Open($TargetFile,[System.IO.FileMode]::Create)
         $FileInfo.Stream.CopyTo($WriteStream)
         $WriteStream.Close()     
         Write-host -f Green "File '$SourceFile' Downloaded to '$TargetFile' Successfully!" $_.Exception.Message
         $File = $Ctx.Web.GetFileByServerRelativeUrl($SourceFile)
         $File.MoveTo($NewFileURL, [Microsoft.SharePoint.Client.MoveOperations]::Overwrite)
         $Ctx.ExecuteQuery()     
         write-host -f Green "File Renamed successfully!"
   }
     Catch {
         write-host -f Red "Error Downloading File!" $_.Exception.Message
     }
 }
  
 #Set parameter values
 $SiteURL="https://contoso.sharepoint.com/sites/dev/"
 $SourceFile="/sites/dev/Shared Documents/1.pdf"  #Relative URL 
 $TargetFile="C:\Temp\1.pdf"
 $NewFileURL="/sites/dev/Shared Documents/2.pdf"
 #Call the function to download file 
 DownloadAndRename-FileFromLibrary -SiteURL $SiteURL -SourceFile $SourceFile -TargetFile $TargetFile -NewFileUrl $NewFileURL

Reference:
https://www.sharepointdiary.com/2016/09/sharepoint-online-download-file-from-library-using-powershell.html
https://www.sharepointdiary.com/2017/10/sharepoint-online-rename-files-in-document-library-using-powershell.html
PNP powershell suggested by sharatha is also another solution.
PNP powershell installation


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 2 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello @Jay-8728,
Do you have any update on this issue? You can accept the helpful post as answer to help other community members find helpful information quickly.
You can also share your own solution here and mark it as answer, and we can learn from each other.

0 Votes 0 ·

Hello @Jay-8728,
Do you have any update on this issue?

0 Votes 0 ·