code

PowerShell을 사용하여 파일 및 디렉터리 목록 생성

starcafe 2023. 8. 11. 22:28
반응형

PowerShell을 사용하여 파일 및 디렉터리 목록 생성

몇 개의 디렉터리를 만들고 여러 개의 파일을 함께 복사하여 기술 문서를 "컴파일"하기 위해 PowerShell 스크립트를 작성하고 있습니다.Readme 파일의 일부로 파일과 디렉터리의 매니페스트를 생성하고 PowerShell에서 이미 "컴파일" 작업을 수행하고 있기 때문에 PowerShell에서 이 작업을 수행하고 싶습니다.

이미 검색을 좀 해봤는데, "Get-ChildItem"이라는 cmdlet을 사용해야 할 것 같은데, 너무 많은 데이터가 제공되고 원하는 결과를 얻기 위해 포맷하고 잘라내는 방법이 명확하지 않습니다.

다음과 같은 출력을 원합니다.

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

또는 이런 것일 수도 있습니다.

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

즉, 디렉토리와 파일 이름을 사용하여 트리 구조를 기본적으로 시각적으로 ASCII로 표현하는 것입니다.이 기능을 수행하는 프로그램을 본 적이 있지만 PowerShell에서 이 기능을 수행할 수 있는지는 잘 모르겠습니다.

PowerShell에서 이를 수행할 수 있습니까?그렇다면 Get-ChildItem이 올바른 cmdlet일까요?

당신의 특별한 경우에 당신이 원하는 것은Tree /f볼륨, 일련 번호 및 드라이브 문자에 대해 이야기하면서 앞부분에 있는 부품을 제거하는 방법을 묻는 댓글이 있습니다.파일로 보내기 전에 출력을 필터링할 수 있습니다.

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

위의 예에서 트리의 출력은 다음과 같습니다.System.Array우리가 조작할 수 있습니다.Select-Object -Skip 2해당 데이터를 포함하는 처음 두 줄을 제거합니다.또한 Keith Hill이 주변에 있었다면 cmdlet이 포함된 PowerShell Community Extensions(PSCX)도 추천할 것입니다.Show-Tree궁금하면 여기서 다운로드하세요.거기에는 강력한 것들이 많이 있습니다.

다음 스크립트는 트리를 창으로 표시하며 스크립트에 있는 모든 양식에 추가할 수 있습니다.

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

Windows관심 디렉토리로 이동합니다.

Shift마우스 오른쪽 버튼 클릭 ->Open PowerShell window here

Get-ChildItem | tree /f > tree.log

나에게 가장 좋고 명확한 방법은 다음과 같습니다.

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

명령을 사용할 수 있습니다.Get-ChildItem -Path <yourDir> | tree >> myfile.txt이렇게 하면 디렉터리의 트리와 같은 구조가 출력되어 "myfile"에 기록됩니다.txt"

언급URL : https://stackoverflow.com/questions/27447014/use-powershell-to-generate-a-list-of-files-and-directories

반응형