I have the following powershell script that cycles through a list of files and renames them. I'd like to introduce some error handling, but not sure where to begin.
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$Url = "https://....."
$UrlSub = "docs"
$FullPath = $Url + $UrlSub
$destinationFolder = "c:22713"
$sourceCsv = "c:filename.CSV"
$Site = New-Object -TypeName Microsoft.SharePoint.SPSite $Url
$web = $Site.OpenWeb($UrlSub)
$fileObjects = Import-CSV $sourceCsv
ForEach ($fileObject in $fileObjects)
{
$fileUrl = $fileObject.DOC_FILENAME.replace($Url,"")
$file = $web.GetFile($FullPath)
$binary = $file.OpenBinary()
$dateTimeStamp = Get-Date -format s
$newFileName = $fileObject.DocumentType + "_" + $fileObject.SAPObjectNumber + "_" + $dateTimeStamp.replace(":","").replace("-","")
$extension = [System.IO.Path]::GetExtension($file.Name)
$stream = New-Object System.IO.FileStream(($destinationfolder + $newFileName + $extension), [System.IO.FileMode]::Create)
$writer = New-Object System.IO.BinaryWriter($stream)
$writer.write($binary)
$writer.Close()
}
$web.Dispose()
See Question&Answers more detail:os