Solved

Script to create folders and sub folders to diffrent document libaries

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now