I've found this great script that downloads all images and their folders from a given library.
I'd like to update it so that it only gets images that have a certain value for the product code. However when I try to just log the code filed I'm getting a null value.
$destination = "D:\\images"
$webUrl = "https://site.local/"
$listUrl = "https://site.local/images"
##############################################################
$web = Get-SPWeb -Identity $webUrl
$list = $web.GetList($listUrl)
$spQuery = New-Object Microsoft.SharePoint.SPQuery
$camlQuery = '<Query>
<Where>
<and>
<Neq>
<FieldRef Name='Code' />
<Value Type='Lookup'>''</Value>
</Neq>
<Eq>
<FieldRef Name='Created' />
<Value Type='DateTime'><Today /></Value>
</Eq>
</and>
</Where>
</Query>'
$spQuery.Query = $camlQuery
$spListItems = $list.GetItems($spQuery)
function ProcessFolder {
param($folderUrl)
$folder = $web.GetFolder($folderUrl)
foreach ($file in $folder.Files) {
#Customising Varun Malhotra script
#Get value of Product Code
$item = $list.Items[0]
Write-Host $item["Code"].ToString()
Write-Host $item
#Ensure destination directory
$destinationfolder = $destination + "/" + $folder.Url
if (!(Test-Path -path $destinationfolder))
{
$dest = New-Item $destinationfolder -type directory
}
Write-Host $file
#Download file
$binary = $file.OpenBinary()
$stream = New-Object System.IO.FileStream($destinationfolder + "/" + $file.Name), Create
$writer = New-Object System.IO.BinaryWriter($stream)
$writer.write($binary)
$writer.Close()
}
}
#Download root files
ProcessFolder($list.RootFolder.Url)
#Download files in folders
foreach ($folder in $list.Folders) {
ProcessFolder($folder.Url)
}
I think I'm not getting the $item correctly as I've already got the list of files??
EDIT: I've updated the script with a Caml Query that will find only the items I want. I'm now not sure how I can get $spListItems to fit into the ProcessFolder function so it can run for ALL folders in the library but only find the Caml selected items?