# add a background color to directory rows
Function Add-DirBackground
{
[CmdletBinding()]
Param (
[Parameter(Mandatory, ValueFromPipeline)]
[string]$Line
)
Process
{
If ($Line.Contains("*dir*"))
{
$Line = $Line.Replace("<tr>", "<tr class=""dir"">")
$Line = $Line.Replace("*dir*", "")
}
Return $Line
}
}
$head = @"
<Title>$Foldername - Folder structure</Title>
<style>
body { font-family:Calibri;
font-size:11pt; }
td, th { border:1px solid #ddd;
border-collapse:collapse;
width: 160px;
}
th { color:white;
background-color:#344D59;
padding-top: 12px;
padding-bottom: 12px;
font-size:11pt; }
table { width:1050px; margin-bottom:4px;
border-collapse: collapse
}
h2 {
font-family:Arial;
color:#212E53;
font-size:14pt;
}
h4 {
font-family:Arial;
color:#212E53;
font-size:12pt;
}
.dir {
background-color: #B8CBD0;
font-weight: bold;
font-style: italic;
font-size:11pt;
}
</style>
"@
# Folder select
Add-Type -AssemblyName System.Windows.Forms
$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
$FolderBrowser.Description = 'Select the folder containing the data'
$result = $FolderBrowser.ShowDialog((New-Object System.Windows.Forms.Form -Property @{ TopMost = $true }))
if ($result -eq [Windows.Forms.DialogResult]::OK)
{
$FolderBrowser.SelectedPath
}
else
{
exit
}
# Variables
$path = $FolderBrowser.SelectedPath
$Foldername = Split-Path -Path $FolderBrowser.SelectedPath -Leaf
$outputfile = "$path\$Foldername.html"
$docfile = "$path\$Foldername" + "-Folder index.docx"
$Folders = get-childitem -Path $path -recurse | where-object { $_.PSIsContainer }
$Files = get-childitem -Path $path -recurse | where-object { -not $_.PSIsContainer }
# Request
$Data = Get-ChildItem -Recurse $path | Sort-Object fullname | Foreach {
$name = $_.fullName
#$name = $_.fullName.replace($path,$_.parent)
$parent = Split-Path $name -parent
$node = Split-Path $name -leaf
$dir = $_.PSISContainer
if ($dir) { $node += '*dir*' }
$level = ($name.ToCharArray() -eq "\").Count
[PSCustomObject]([ordered]@{
level = $level
dir = $dir
parent = $parent
node = $node
})
}
# Output to HTML
$Data |
Select-Object -Property @{ Name = 'Path'; Expression = { $_.Parent } },
@{ Name = 'File name'; Expression = { $_.Node } },
@{ Name = 'Comments'; Expression = { $_.Null } },
@{ Name = 'Doc'; Expression = { $_.Null } } |
ConvertTo-Html -head $head -Body "<h2>Folder name: $($Foldername)</h2> <td><h4>Folders: $($folders.Count) / Files: $($Files.Count)</h4></td>" | Add-DirBackground | out-file $outputfile
# Convert html to docx
[ref]$SaveFormat = "microsoft.office.interop.word.WdSaveFormat" -as [type]
$word = New-Object -ComObject word.application
$word.visible = $false
$Selection = $Word.Selection
$doc = $word.documents.open($outputfile)
# change page orientation to landscape
$doc.PageSetup.Orientation = 1
# Set margins size
$doc.PageSetup.LeftMargin = 25
$doc.PageSetup.RightMargin = 25
$doc.PageSetup.TopMargin = 25
$doc.PageSetup.BottomMargin = 50
# set top row to repeat across pages
$doc.Tables[1].Rows[1].HeadingFormat = -1
$doc.saveas([ref]$docfile, [ref]$SaveFormat::wdFormatDocumentDefault)
$doc.close()
$word.Quit()
$word = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()
# Remove HTML file
Remove-Item $outputfile -Force
# open Doc file
ii $docfile
ASKER
$Data = Get-ChildItem -Recurse $path | Sort-Object PsParentPath, {!$_.PsISContainer}, Name | Foreach {
ASKER
ASKER
Sort-Object -Property @{Expression = ({If($_.PSISContainer) {$_.FullName} Else {Split-Path $_.FullName -parent}})}, {!$_.PSISContainer}, {$_.FullName}|
ASKER
ASKER
Sort-Object {If($_.PsIsContainer) {$_.FullName} Else {$_.DirectoryName}}, {!$_.PsIsContainer}, FullName
ASKER
Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.
TRUSTED BY
Open in new window