簡體   English   中英

導出目錄文件名

[英]Exporting directory file names

我正在嘗試創建一個腳本,該腳本提示輸入要導出到.csv的路徑,該路徑顯示文件夾的名稱和文件夾內容的名稱。 像這樣,但沒有模式和長度。

我想保持每個文件夾之間的空隙。

示例-(無模式/長度)

Directory: C:\Users\khalifam\Desktop\TestFolder1


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       24/07/2019     15:50                TestFolder2
d-----       24/07/2019     15:50                TestFolder3


    Directory: C:\Users\khalifam\Desktop\TestFolder1\TestFolder2


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       30/05/2019     11:05           1696 EC2Key1.pem


    Directory: C:\Users\khalifam\Desktop\TestFolder1\TestFolder3


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       31/05/2019     16:16          22027 Dropbox-f

到目前為止,我的腳本是:

$FilePathLocation = Read-Host -Prompt 'Please enter the the path of the fold you wish to export'

Set-Location $FilePathLocation

gci -Recurse | select DirectoryName, FullName | FT

電流輸出:

Please enter the the path of the fold you wish to export: C:\Users\khalifam\Desktop\TestFolder1

DirectoryName                                     FullName                                                                            Root
-------------                                     --------                                                                            ----
                                                  C:\Users\khalifam\Desktop\TestFolder1\TestFolder2                                   C:\
                                                  C:\Users\khalifam\Desktop\TestFolder1\TestFolder3                                   C:\
C:\Users\khalifam\Desktop\TestFolder1\TestFolder2 C:\Users\khalifam\Desktop\TestFolder1\TestFolder2\EC2Key1.pem
C:\Users\khalifam\Desktop\TestFolder1\TestFolder3 C:\Users\khalifam\Desktop\TestFolder1\TestFolder3\Dropbox-CCEN-Course.docx

首先遞歸枚舉目錄,然后不遞歸地處理每個目錄的內容。 請注意,由此產生的輸出不是實際的CSV。

Get-ChildItem $FilePathLocation -Directory -Recurse | ForEach-Object {
    "{0}`n" -f $_.FullName
    Get-ChildItem $_.FullName |
        Select-Object Name, LastWriteTime |
        Format-Table |
        Out-String
} | Set-Content 'output.txt'

另請注意,這需要PowerShell v3或更高版本。 如果您使用的是舊版本,則需要刪除參數-Directory並使用Where-Object過濾器。

您擁有大部分。

$FilePathLocation = Read-Host -Prompt 'Please enter the the path of the fold you wish to export'

Get-ChildItem $FilePathLocation -Recurse | select DirectoryName, FullName | Export-Csv C:\PathStuff.csv

如果要導出到csv,則不能使用Format-Table,它會使輸出混亂

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM