What I'm looking for is some sort of software that is able to output to a text file the flattened directory tree/hierarchy, including any file in it. It's for backup purposes.

Say for example, I have D:\Software containing a couple of folders, D:\Software\Soft1 and D:\Software\Soft2, and a couple of files, D:\Software\Soft1\run1.exe and D:\Software\start.bin

The text file in output should look like this:






I hope I made it clear what I need.

1 Answer 1


From my understanding, given an input folder, you'd like to flatten the folder tree/hierarchy and write the result to a text file with the format you specified.

I've written a PowerShell script to achieve that.

Create a .ps1 text file (e.g. Flatten-Dir.ps1) with the following content:

    # The input/root folder to process
    [Parameter(Mandatory = $true)]

    # The output folder that will contain the flattened folder tree
    [Parameter(Mandatory = $true)]

function recdir($InputFolder)
    Get-ChildItem -Path $InputFolder | foreach {
        if($_.Attributes -eq "Directory")
            Write-Output ("[" + $_.FullName + "]`r`n")
            recdir $_.FullName
            Write-Output ("[" + $_.FullName + "]`r`n")

Set-Content -Path $OutputFile -Force ("[" + $InputFolder + "]`r`n")
recdir $InputFolder | Add-Content -Path $OutputFile

Then run it in a PowerShell environment, providing the input folder you want to process, followed by the output file which will contain the flattened folder tree. For example:

Flatten-Dir.ps1 C:\tmp\Software C:\tmp\flattened_tree.txt

The script uses a custom recursive function in order to achieve that particular desired output. If anyone thinks there's a better/easier way to do it, let me know.

As a test, if this is the folder structure with some files...

Tree sample

... then this is what the script writes in the output file:










  • Thank you, I'll try it out once my PC stops acting up. Much obliged! Commented Feb 7, 2016 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.