Copy/Rename

by SharePointGeek at 2013-03-06 07:16:03

I’m running this loop to copy/rename a group of files. It does the copy/rename as I hoped, but all files have a .pdf extension instead of it’s native (.docx, .xls, .pptx, etc…) extension.

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

$Url = "https://spsvcs/"
$UrlSub = "cmms"
$FullPath = $Url + $UrlSub
$destinationFolder = "c:\eam\multi"
$sourceCsv = "c:\eam\multi.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("-","").replace("T","")
$extension = [System.IO.Path]::GetExtension($file.Name)

$stream = New-Object System.IO.FileStream($destinationfolder + $newFileName + $extension), Create
$writer = New-Object System.IO.BinaryWriter($stream)
$writer.write($binary)
$writer.Close()
}
$web.Dispose()
by DonJ at 2013-03-06 08:32:19
Please consider using the CODE button to format your code here - makes it a lot easier to follow.

The problem is in your logic. You are taking the filename extension from $file, which is getting the file in $FullPath, but $FUllPath is not being changed inside your ForEach loop. I suspect the first file in your list is a PDF, and so it’s only grabbing that.
by SharePointGeek at 2013-03-06 11:25:57
DonJ - Thanks! I believe I addressed the looping issue with the code below, but now I’m getting this error:

Exception calling "OpenBinary" with "0" argument(s): "There is no Web named "/cmms"."
At C:\eam\Export.ps1:20 char:35
+ $binary = $file.OpenBinary <<<< ()
+ CategoryInfo : NotSpecified: (:slight_smile: , MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException


May challenge is how best to represent this site collection.

Updated code:


[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

#$ErrorActionPreference = "SilentlyContinue"

$Url = "https://spsvcs/"
$UrlSub = "cmms"
$FullPath = $Url + $UrlSub
$destinationFolder = "c:\eam\multi&quot;
#$destinationFolder = "\hsypofila002\strsSTG\Sharepoint\m"
$sourceCsv = "c:\eam\multi.CSV"
#$sourceCsv = "c:\eam\wave2.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($fileUrl)
$binary = $file.OpenBinary()

$dateTimeStamp = Get-Date -format s
$newFileName = $fileObject.DocumentType + "" + $fileObject.SAPObjectNumber + "" + $dateTimeStamp.replace(":","").replace("-","").replace("T","")
$extension = [System.IO.Path]::GetExtension($file.Name)

$stream = New-Object System.IO.FileStream($destinationfolder + $newFileName + $extension), Create
$writer = New-Object System.IO.BinaryWriter($stream)
$writer.write($binary)
$writer.Close()
}
$web.Dispose()
by DonJ at 2013-03-06 11:47:48
Out of my league now. Might ask on the SharePoint forum.
by SharePointGeek at 2013-03-06 12:06:07
Thanks!