Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 578 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use PowerShell to generate a list of files and directories

#1
I'm writing a PowerShell script to make several directories and copy a bunch of files together to "compile" some technical documentation. I'd like to generate a manifest of the files and directories as part of the readme file, and I'd like PowerShell to do this, since I'm already working in PowerShell to do the "compiling".

I've done some searching already, and it seems that I need to use the cmdlet "Get-ChildItem", but it's giving me too much data, and I'm not clear on how to format and prune out what I don't want to get my desired results.

I would like an output similar to this:

Directory
file
file
file
Directory
file
file
file
Subdirectory
file
file
file


or maybe something like this:


+---FinGen
| \---doc
+---testVBFilter
| \---html
\---winzip


In other words, some kind of basic visual ASCII representation of the tree structure with the directory and file names and nothing else. I have seen programs that do this, but I am not sure if PowerShell can do this.

Can PowerShell do this? If so, would Get-ChildItem be the right cmdlet?
Reply

#2
You can use command `Get-ChildItem -Path <yourDir> | tree >> myfile.txt` this will output tree-like structure of a directory and write it to "myfile.txt"
Reply

#3
The following script will show the tree as a window, it can be added to any form present in the script

function tree {

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

# create Window
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Files"
$Form.Size = New-Object System.Drawing.Size(390, 390)
# create Treeview-Object
$TreeView = New-Object System.Windows.Forms.TreeView
$TreeView.Location = New-Object System.Drawing.Point(48, 12)
$TreeView.Size = New-Object System.Drawing.Size(290, 322)
$Form.Controls.Add($TreeView)

###### Add Nodes to Treeview
$rootnode = New-Object System.Windows.Forms.TreeNode
$rootnode.text = "Root"
$rootnode.name = "Root"
[void]$TreeView.Nodes.Add($rootnode)

#here i'm going to import the csv file into an array
$array=@(Get-ChildItem -Path D:\personalWorkspace\node)
Write-Host $array
foreach ( $obj in $array ) {
Write-Host $obj
$subnode = New-Object System.Windows.Forms.TreeNode
$subnode.text = $obj
[void]$rootnode.Nodes.Add($subnode)
}

# Show Form // this always needs to be at the bottom of the script!
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()

}
tree
Reply

#4
The best and clear way for me is:

PS P:\> Start-Transcript -path C:\structure.txt -Append
PS P:\> tree c:\test /F
PS P:\> Stop-Transcript
Reply

#5
In your particular case what you want is `Tree /f`. You have a comment asking how to _strip out the part at the front talking about the volume, serial number, and drive letter_. That is possible filtering the output before you send it to file.

$Path = "C:\temp"
Tree $Path /F | Select-Object -Skip 2 | Set-Content C:\temp\output.tkt

Tree's output in the above example is a `System.Array` which we can manipulate. `Select-Object -Skip 2` will remove the first 2 lines containing that data. Also, If Keith Hill was around he would also recommend the PowerShell Community Extensions(PSCX) that contain the cmdlet `Show-Tree`. Download from [here](

[To see links please register here]

) if you are curious. Lots of powerful stuff there.
Reply

#6

In `Windows`, navigate to the directory of interest

<kbd>Shift</kbd>+ right click mouse -> `Open PowerShell window here`

Get-ChildItem | tree /f > tree.log
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through