我正在尝试创建一个脚本,该脚本提示要导出到.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
答案 0 :(得分:2)
首先递归枚举目录,然后不递归地处理每个目录的内容。请注意,由此生成的输出不是实际的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
过滤器。
答案 1 :(得分:0)
您拥有大部分。
$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,它会弄乱输出