In SharePoint generate hierarchical index of Folders, subfolders, and filenames

E Lippert 0 Reputation points
2023-10-08T16:37:31.5433333+00:00

I need to create a hierarchical directory of folders, subfolders, and their filename contents for a SharePoint document St Pauls Team Site. I have been granted full privileges. To do this, I followed the general procedure below and bracketed by ===============.
 

Basically, the suggested procedure is to run at the Command Prompt the instruction:

DIR /d /s [UNC] [pipe to file]

I could not generate [UNC] using the procedure suggested below but I did find a similar string in SharePoint for the document under the tab: St Pauls Team Site. an UNC that looked similar to what was shown:

 

stpauls220.sharepoint.com/Shared%20Documents/Forms/Allitems.aspx?

 

I Piped it to: c: stpaulSPfiles.txt

 

Under Command Prompt with Administrator privilege I entered the full command

 

C:\Windows\System32>DIR /d /s \ stpauls220.sharepoint.com/Shared%20Documents/Forms/Allitems.aspx? > c: stpaulSPfiles.txt

 

The following message was returned:

+++++++++++++++++++++ 

C:\Windows\System32>DIR /d /s \ stpauls220.sharepoint.com/Shared%20Documents/Forms/Allitems.aspx? > c: stpaulSPfiles.txt

Access is denied.

 

C:\Windows\System32>

 +++++++++++++++++++

I would appreciate your guidance on the proper procedure to create a hierarchical directory of folders, subfolders, and their filename contents.

 

Regards,

Ernest Lippert

 

 

 

========================================================================

·   https://www.sharepointgeoff.com/how-to-quickly-list-documents-and-sub-folders-from-a-document-library-in-sharepoint-to-a-file/

Assurance | Random Muslings

March 5, 2012

I have been asked by quite a few people on listing documents from document libraries that include sub-folders; so I’d like to share with you a poor-man method of listing all files and folders in a document library in SharePoint with hundreds of files and folders, and displaying the contents of this in NotePad.

Now, I know there are probably lots of nice little apps out there, and I must admit writing one myself called GEGETDOCCONFIG (which does a whole lot more). There’s also information on how to use Powershell to enumerate document libraries here – but you can very quickly list all files and folders from a DIRectory assuming you can get its UNC (Universal Naming Convention) path and you can access the entire document library. All you will need to do is to use the famous DIR command from the command prompt, and you can do this without having to log onto any of the servers in your SharePoint farm.

If you’ve spent much time working in a DOS-based environment, chances are that you’re very familiar with the DIR command. When used in its most basic form, the DIR command simply displays a list of all the files and subdirectories contained within a particular directory. However, the DIR command has access to a fleet of special command line parameters that allow it to perform a host of very specific file listing and sorting operations. While you can perform many of these operations in Windows Explorer, the speed and accuracy with which you can perform them with the DIR command is astounding. Furthermore, you can easily combine these command line parameters to create unique directory listings on the fly.
You will need access to the COMMAND PROMPT, so you can use it against the UNC. A UNC is a naming convention used primarily to specify and map network drives in Microsoft Windows. Support for UNC also appears in other operating systems via technologies. UNC names are most commonly used to reach file servers or printers on a LAN. UNC names identify network resources using a specific notation. UNC names consist of three parts – a server name, a share name, and an optional file path. We will need the UNC of the relevant SharePoint location and then use DIR against it.

Follow these steps:

Step 1: Get the UNC

Take for example, a document library whose contents you would like to list:

http://documents/Products/Forms/AllItems.aspx

Its UNC from Windows Explorer would be:

\documents\products

Make a note of the UNC path you will need it in the next step.

Step 2: Generate the List

So, to list all the files and the folders, off to DOS we go:

Click Start -> Accessories -> Command Prompt

Then, enter this command:

DIR /d /s (name of the above UNC path) > (name of the file you want to pipe the list to)

For example:

DIR /d /s \documents\products > c:\productfilelist.txt

This will list all the files and folders from the UNC path into productfilelist.txt. Then, all you need to do is open this in notepad or clean it up in Excel.

==========================================================================

Microsoft 365 and Office SharePoint For business Windows
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Ling Zhou_MSFT 23,620 Reputation points Microsoft External Staff
    2023-10-13T01:33:47.0966667+00:00

    Hi @E Lippert,

    Great to know that and thanks for sharing the update here.

    By the way, since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others.". and according to the scenario introduced here: Answering your own questions on Microsoft Q&A, I would make a brief summary of this thread:

    [In SharePoint generate hierarchical index of Folders, subfolders, and filenames]

    Issue Symptom:
    How to get a hierarchical directory of folders, subfolders, and their filename contents for a SharePoint document.

    Current status:
    We can use the "Export to Excel" feature.

    User's image

    You could click the "Accept Answer" button for this summary to close this thread, and this can make it easier for other community members to see the useful information when reading this thread. Thanks for your understanding!

    1 person found this answer helpful.

  2. Ling Zhou_MSFT 23,620 Reputation points Microsoft External Staff
    2023-10-09T08:26:21.0933333+00:00

    Hi @E Lippert,

    Thank you for posting in this community.

    I checked out what you shared but I am curious as to how your method of exporting the document file and subfile structure seems to be incorrect. SharePoint requires a specialized PowerShell module to execute the relevant PowerShell commands.

    Let me confirm your question, you want to get the structure of folders, sub-files and files of a certain document library. Correct me if I am wrong.

    If you are using SharePoint On-Prem:

    1.Open your SharePoint Management Shell (I used SharePoint 2016). Please make sure that you have full access to the document Library to which you want to export the structure or that you are the administrator of the site.

    User's image

    2.Try the following PowerShell commands to get the structure of folders, subfolders and files. Please replace your own SiteURL, ListName and Excel file path.

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
     
    #Function to get all files of a folder
    Function GetFiles-ByFolder([Microsoft.SharePoint.SPFolder]$Folder)
    {
        write-host -f Yellow "Processing Folder:"$Folder.URL
        $ContentFolder = "Folder" + "," + $Folder.Name +","+ $Folder.URL
        Add-content $OutPutFile $ContentFolder
        Write-host "Folder:"  $ContentFolder
        Foreach($File in $Folder.Files) 
        {
            $Content = "File" + "," + $Folder.Name + "," + $Folder.URL +"," + $File.Name
            #Append content to CSV file
            Add-content $OutPutFile $Content
            Write-host "File:" $Content
        }
         
        #Call the function for each subfolder - Excluding "Forms"
        $Folder.SubFolders | Where {$_.Name -ne "Forms" } | Foreach-Object {
        GetFiles-ByFolder $_
        }
    }
      
    #Variables
    $SiteURL = "http://YourSiteURL"
    $ListName ="YourListName"
    $OutPutFile = "C:\YourPath\LibraryFiles.csv"
     
    #Delete the CSV file if exists
    If (Test-Path $OutPutFile) { Remove-Item $OutPutFile }
     
    #Write CSV headers
    Add-Content $OutPutFile "Type, Folder Name, Relative URL, File Name"
     
    #Get site and list objects
    $Web = Get-SPWeb $SiteURL
    $List = $Web.Lists[$ListName]
    $Folder = $List.RootFolder
     
    #Call the function for Root folder
    GetFiles-ByFolder $Folder
    

    3.Here is the result in my side:

    User's image

    If you are using SharePoint Online:

    1.Install PnP PowerShell Mode.

    2.Try the following PowerShell commands to get the structure of folders, subfolders and files.

    #Parameters
    $SiteURL = "https://YourSiteURL"
    $ListName= "YourListName"
    $ReportOutput = "C:\YourPath\ListInventory.csv"
      
    #Connect to SharePoint Online site
    Connect-PnPOnline $SiteURL -Interactive
      
    #Array to store results
    $Results = @()
      
    #Get all Items from the document library
    $List  = Get-PnPList -Identity $ListName
    $ListItems = Get-PnPListItem -List $ListName -PageSize 500
    Write-host "Total Number of Items in the List:"$List.ItemCount
     
    $ItemCounter = 0
    #Iterate through each item
    Foreach ($Item in $ListItems)
    {
    		#If you don't need to display certain information, you can delete it.
            $Results += New-Object PSObject -Property ([ordered]@{
                Name              = $Item["FileLeafRef"]
                Type              = $Item.FileSystemObjectType
                FileType          = $Item["File_x0020_Type"]
                RelativeURL       = $Item["FileRef"]
                CreatedBy         = $Item["Author"].Email
                CreatedOn         = $Item["Created"]
            })
        $ItemCounter++
        Write-Progress -PercentComplete ($ItemCounter / ($List.ItemCount) * 100) -Activity "Processing Items $ItemCounter of $($List.ItemCount)" -Status "Getting Metadata from Item '$($Item['FileLeafRef'])"         
    }
     
    #Export the results to CSV
    $Results | Export-Csv -Path $ReportOutput -NoTypeInformation
    Write-host "Document Library Inventory Exported to CSV Successfully!"
    

    3.Here is the result in my side:

    User's image


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. E Lippert 0 Reputation points
    2023-10-12T16:49:17.9133333+00:00

    Sorry for not responding sooner. I found that I could use the "Download to Excel" feature to research and solve my most immediate questions, some of which were a result of my unfamiliarity with SharePoint. The main problem resulted from the proliferation of subfolders, etc. due to a succession of secretaries who did not receive adequate direction. I may try some of the code provided at a later time. Thanks again for your help.

    Ernie

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.