Solved

Script to create folders and sub folders to diffrent document libaries

Posted on 2013-10-22
2
3,962 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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…

739 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