# FileList-to-HTML.ps1 # Version 1.2 # Last updated 3/4/2019 # This PowerShell script will create an HTML list of all files in a folder. It will generate an HTML file titled "FileList.html" # Right now, it is in a very basic functioning state but I have plans to continue developing it. Hopefully you find it of use! # You may use the "-help" parameter to view practical usage examples. param( [switch]$r,[string]$o,[switch]$help ) # Help contents function help { Write-Host "" Write-Host "FileList-to-HTML.ps1" Write-Host "Indexes files/folders and outputs a list of them to an HTML file." Write-Host "" Write-Host "- Index a directory and create an HTML list in the current directory:" -ForegroundColor Green Write-Host -ForegroundColor Magenta " powershell.exe .\FileList-to-HTML.ps1 " Write-Host "" Write-Host "- Index a directory and create an HTML list, but put the HTML file in a different folder:" -ForegroundColor Green Write-Host -ForegroundColor Magenta " powershell.exe .\FileList-to-HTML.ps1 " -NoNewline; "-o X:\path\to\somefolder" Write-Host "" Write-Host "- Recursively index a directory and create an HTML list in the current directory:" -ForegroundColor Green Write-Host -ForegroundColor Magenta " powershell.exe .\FileList-to-HTML.ps1 " -NoNewline; "-r" Write-Host "" Write-Host "- Recursively index a directory and create an HTML file, but put the HTML file in a different folder:" -ForegroundColor Green Write-Host -ForegroundColor Magenta " powershell.exe .\FileList-to-HTML.ps1 " -NoNewline; "-r -o X:\path\to\somefolder" Write-Host "" exit } # Variables $OutputFolder = $folder $OutputFile = "FileList.html" if ($help) { help } if (!($help -or $r -or $o -or $args.Count -eq 0)) { throw "That is not a valid parameter. Please use the -help parameter for more information." exit } #Initial prompt Write-Host "FileList-to-HTML.ps1 Version: [1.2]" Write-Host "" Write-Host "Indexes files/folders and outputs a list of them to an HTML file." Write-Host "" Write-Host "Please use the -help parameter to view more options." Write-Host "---------------------------------------------------------------------------" $folder = Read-Host "Enter the directory name of files (leave blank for current directory)" if ($folder -eq "") { $OutputFolder = $pwd } else { $OutputFolder = $folder } # More variables $files = Get-ChildItem $folder -File | Select-Object -ExpandProperty Name if ($r) { $files = Get-ChildItem $folder -File -Recurse | Select-Object -ExpandProperty Name } if ($o) { $OutputFolder = $o } $date = Get-Date -Format yyyy-MM-dd`vhh:mm # The counter that will count each file. $counter = 0 $files | ForEach-Object { $files[$counter] = "$($counter+1). $($files[$counter])
`n" $counter++ Write-Host "Indexing file...$counter" } # HTML file contents $HTML = @" File list

File list


Number of files: $counter
Right-click here to save this HTML file.

List of files:

$files


Generated on $date
"@ # Create the HTML file. Write-Host "Creating the HTML file..." $HTML | Out-File $OutputFolder\$OutputFile # Verify successful creation of the HTML file. if (Test-Path -Path $OutputFolder\$OutputFile) { Write-Host -ForegroundColor Green "HTML file successfully created at '$OutputFolder\$OutputFile'!" Start $OutputFolder\$OutputFile } else { throw "File was not successfully created. Please check for any errors above." } exit