Solved

Script to create folders and sub folders to diffrent document libaries

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
A brief introduction to what I consider to be the best editor for PowerShell.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

911 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

15 Experts available now in Live!

Get 1:1 Help Now