Solved

Script to create folders and sub folders to diffrent document libaries

Posted on 2013-10-22
2
4,052 Views
Last Modified: 2013-10-24
Experts,
i have a script where i can run to create folder and a subfolder. The script works perfectly for one document library. If i have a subsite in my sharepoint 2013 and many document libraries, all starts with library* , how can the script check and create folders if needed

Here is the script i have which works on ONE document library. As soon as i change the $List=$web.Lists | where {$_.Title -like "Libra*"} it stop working. i assume it may have something to do with foreach command. Here is the script

#Get the Web
$web=Get-SPWeb "http://portal.domain.com/subsite"
#Get the List/Library
$List=$web.Lists | where {$_.Title -like "Libra*"}
$foldername = Get-Date -Format "yyyy MMMMM"

foreach($L in $List) {

 }
if($List)  #Check If List exists! }


      {
       # Create a Folder "Archives"
        # Check Folder Doesn't exists in the Library!
        $folder = $list.ParentWeb.GetFolder($list.RootFolder.Url + "/" +"Archives");
 
              if ($folder.Exists -eq $false)
   {
          #Create a Folder
          $folder = $list.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "Archives")
          $folder.Update();
   }
   
        #Create a Sub-Folder "$foldername" inside "Archives"
        #Check if sub-folder doesn't exists already
        $Subfolder = $list.ParentWeb.GetFolder($folder.URL + "/" + "$foldername");
        if ($Subfolder.Exists -eq $false)
   {
          #Create a folder Inside "Archives"
          $Subfolder = $list.AddItem($folder.URL, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $foldername)
          $Subfolder.Update();


   }
 }

$web.Dispose()
0
Comment
Question by:stratoman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 

Accepted Solution

by:
stratoman earned 0 total points
ID: 39595558
we rewrote the script but still not creating folder and sub folders in document libraries

$web = Get-SPWeb "http://portal.domain.com/subsite"
 $Libraries = $web.Lists | where {$_.Title -like "Libra*"}
 $foldername = Get-Date -Format "yyyy-MMMMM"
 
 foreach ($library in $Libraries) {
 
 Write-Output "Getting Library $($library.Title)"    
         # Check Folder Doesn't exists in the Library!
         $folder = $library.ParentWeb.GetFolder($library.RootFolder.Url + "/" +"Archives");}
 
#Use the ! to signify not, cleaner code to determine if the folder is a variable rather than a null
  if (!$folder)
    {
           $folder = $library.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "Archives")
           $folder.Update();
           write-host "Folder created"
}
else
{
 write-host "Folder already exists"
}
         #Check if sub-folder doesn't exists already
         $Subfolder = $library.ParentWeb.GetFolder($folder.URL + "/" + $foldername);
#The former for-each loop was in the wrong place.
 
#Again this has been shortened to make cleaner
 if (!$Subfolder)  
 {
           #Create a folder Inside "Archives"
           $Subfolder = $library.AddItem($folder.URL, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $foldername)
                  $Subfolder.Update();
  write-host "Subfolder created"
}
else
{
  write-host "Subfolder already exists"
}
0
 

Author Closing Comment

by:stratoman
ID: 39597592
closing this
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A brief introduction to what I consider to be the best editor for PowerShell.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question