1

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:

[D:\Software]

[D:\Software\Soft1]

[D:\Software\Soft1\run1.exe]

[D:\Software\Soft2]

[D:\Software\start.bin]

I hope I made it clear what I need.

3

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:

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

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

function recdir($InputFolder)
{
    Get-ChildItem -Path $InputFolder | foreach {
        if($_.Attributes -eq "Directory")
        {
            Write-Output ("[" + $_.FullName + "]`r`n")
            recdir $_.FullName
        }
        else
        {
            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:

[C:\tmp\Software]

[C:\tmp\Software\Soft1]

[C:\tmp\Software\Soft1\run.exe]

[C:\tmp\Software\Soft1\setting.ini]

[C:\tmp\Software\Soft1\test.bat]

[C:\tmp\Software\Soft2]

[C:\tmp\Software\Soft2\Soft3]

[C:\tmp\Software\Soft2\abc.txt]

[C:\tmp\Software\Soft2\def.htm]

[C:\tmp\Software\start.bin]
  • Thank you, I'll try it out once my PC stops acting up. Much obliged! – Grumpy ol' Bear Feb 7 '16 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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