0

I have written following powershell script to check if a folder exists in a document library. My function is not able to find the folder which is existing. I am not sure, if it is due to the pattern of folder name or the function code it self. Appreciate the help Thanks Nate

Function CheckFolder($listParm)
{
TRY
{
    $ErrActionPref = "CONTINUE";
    $spQry = New-Object Microsoft.SharePoint.SPQuery
    $camlQry = "<Where>
      <And>
       <Contains>
         <FieldRef Name='ContentType' />
         <Value Type='Computed'>Folder</Value>
       </Contains>
    </And>
    <And>
       <Eq>
         <FieldRef Name='Title' />
         <Value Type='Text'>Name of Folder-To-Check</Value>
       </Eq>
    </And>
   </Where>"

    $spQry.ViewAttributes = "Scope = 'Recursive'"
    $spQry.Query = $camlQry
    $spLstItms = $listParm.GetItems($spQry)
    if ($spLstItms.Items.Count -gt 0) 
   {

      foreach ($item in $spLstItms)
      {
         #items found
          return $true
      }
   }

}
CATCH
{

 #Error handling
 return $false   
}
return  $false
}

I am calling this CheckFolder function as follows

if ((CheckFolder $list) -eq $true)
{
   #Matching folder found
}
0

If you want to keep on with a CAML query (as you don't know the full path of the folder, but nly its name, as you stated in the comments), try this query instead:

    $camlQry = "<Where>
      <And>
         <Eq>
            <FieldRef Name='FSObjType' />
            <Value Type='Lookup'>1</Value>
         </Eq>
    </And>
    <And>
       <Eq>
         <FieldRef Name='FileLeafRef' />
         <Value Type='Text'>Name of Folder-To-Check</Value>
       </Eq>
    </And>
   </Where>"
4

Nate,

try the following code

$web = Get-SPWeb "http://webAddress"
 $folderPath = "folderPath"
 $relativeUrl = $web.ServerRelativeUrl
 $folder = $web.GetFolder($relativeUrl+$folderPath)
 if ($folder.Exists)
 {
    Write.Host("Folder exists");
 }
 else
 {
    Write.Host("Folder does not exists");
 }

Let me know if you have any questions.

  • It would be good if you add some information from what is described in the link which you have referred. Only link answers are sometimes not convenient to other readers because external links have chances to break. – Asad Refai Jul 27 '15 at 5:36
  • Thanks, I dont know the absolute path of the folder or I dont even know if the folder exists at all. The function is written to query for all the folders with that name and give me the list. – Nate Jul 27 '15 at 17:16
  • Folder path is essentially the folder name after the library URL. for ex: if the library URL is "siteURL/SiteAssets" and the folder name is "HTML" eventually the folder path for the folder "HTML" would be "siteURL/SiteAssets/HTML" – Naveen Jul 27 '15 at 17:41
  • I want to check if the specific folder exists at root level or one of the subfolder levels. If the folder does not exist in the library, than my function should handle the error and gracefully come out. The logic is present in the code I have written above. I am not sure why it is failing though. – Nate Jul 27 '15 at 18:43
3

The simplest way to check existence of a folder is the following (code not tested, may contain syntax errors, but the idea is clear):

$web = Get-SPWeb "<Sub-site absolute URL>"
$folder = $web.GetFolder("<absolute, or server-relative URL of the folder>")
$folder.Exists
  • Thank you for the suggestion, I dont know the absolute location of the folder, – Nate Jul 27 '15 at 17:15
  • I want to check if the specific folder exists at root level or one of the subfolder levels. If the folder does not exist in the library, than my function should handle the error and gracefully come out. The logic is present in the code I have written above. I am not sure why it is failing though – Nate Jul 27 '15 at 18:43
  • This is exactly what the PS code I proposed does... – Evariste Jul 27 '15 at 19:32
  • I dont know the exact URL of the folder. The GetFolder function will only work, when you know the path of the folder. – Nate Jul 27 '15 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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