0

I have sharepoint library as below: SP Library: "Test"

Folder1

  File1

  Folder2

     File1

  Folder3

     File2

I need as below in my local folder: Local folder: "c:\"

Folder1

   abc0001_File1
   abc0002_File1
   abc0002_File2

Is there any powershell script by which I can rename files of a SharePoint library while downloading to local drive.

1 Answer 1

0

It could be something like this

$webUrl = "http://sp2013-sp/Test"
$rootFolderUrl = "Shared%20Documents"
$destinationfolder= "c:/test"

function BuildPrefix($folderName, $previousPrefix) {
    return $previousPrefix + "abc" + $folderName + "_"
}
function isHidden($folder) {
    $attribs = $folder.Properties["vti_winfileattribs"]
    if ($attribs -eq $null) {
        return $false
    }
    $attribs = [Convert]::ToInt64($attribs, 16)
    return ($attribs -band 2) -eq 2
}
function ProcessFolder($folder, $prefix) {
    foreach ($file in $folder.Files) {
            $binary = $file.OpenBinary()
            $stream = New-Object System.IO.FileStream($destinationfolder + "/" + $prefix + $file.Name), Create
            $writer = New-Object System.IO.BinaryWriter($stream)
            $writer.write($binary)
            $writer.Close()
        }
    foreach ($subFolder in $folder.SubFolders) {
        if (isHidden $subFolder) {
            continue
        }
        $newPrefix = BuildPrefix $subFolder.Name $prefix
            ProcessFolder $subFolder $newPrefix
    }
}

if (!(Test-Path -path $destinationfolder))
{
    New-Item $destinationfolder -type directory | Out-Null
}

$gc = Start-SPAssignment
$web = $gc |Get-SPWeb -Identity $webUrl
$folder = $web.GetFolder($rootFolderUrl )

ProcessFolder $folder "" 

Stop-SPAssignment $gc

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.