powershell - 在目标目录的每个子目录中创建 CSV 文件

标签 powershell powershell-2.0 powershell-3.0 powershell-4.0

我的文件夹中有如下文件:

D:\TESzz\Background\BG_Flash-Zootopia-HD-Desktop-Wallpapers.jpg
D:\TESzz\Background\BG_NimmHBD.jpg
D:\TESzz\Background\BG_Note5.jpg
D:\TESzz\Icons\150x150.jpg
D:\TESzz\Icons\Bathroom-gender-sign.png
D:\TESzz\Icons\brocoli.png
D:\TESzz\Icons\Carrot_Clipart_PNG_Image.png
D:\TESzz\Icons\File.txt
D:\TESzz\Icons\garlic.png
D:\TESzz\Icons\ICONS.txt
D:\TESzz\Icons\NoppNimm-1.jpg
D:\TESzz\Icons\NoppNimmIcon.jpg
D:\TESzz\Icons\NoppNimmIcon.png
D:\TESzz\Icons\NoppProfie.jpg
D:\TESzz\Icons\NoppProfileSerious.jpg
D:\TESzz\Icons\pork.png
D:\TESzz\Icons\Profile.jpg
D:\TESzz\Icons\Questionmark.png
D:\TESzz\Icons\sugar.png
D:\TESzz\Icons\Tree.png
D:\TESzz\Icons\wheel.png

我想将文件列表导出到“D:\TeSzz”下的每个子文件夹,如下所示

D:\TESzz\Icons\Icons.csv
D:\TESzz\Background\Background.csv

我的代码如下。但它会创建“FileList.csv”而不是“Icons.csv”或“Background.csv”。 :(

Get-ChildItem -path "D:\TESzz\" -directory | ForEach-Object {Get-ChildItem -file "$($_.fullname)" | Export-Csv "$(Join-path $_.fullname 'FileList.csv')"}

有人可以帮我解决这个问题吗?

最佳答案

我会为当前目录引入一个变量。您没有得到预期的结果,因为您始终将输出命名为 FileList.csv

Get-ChildItem -path "D:\TESzz\" -directory | 
  ForEach-Object {
    $directory = $_
    Get-ChildItem -file $directory.FullName | 
        Export-Csv (Join-path $directory.FullName "$($directory.BaseName).csv") -Force
}

在上面,请注意您有 FileList 的地方,现在是 $($directory.BaseName),因此每个目录都不同。

关于powershell - 在目标目录的每个子目录中创建 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39160105/

相关文章:

powershell - 在 Powershell 中创建 Outlook 规则时出错

powershell - 如何使用模块 list 导出 PowerShell 模块别名?

Powershell 4.0 Transcript 未捕获 Write-Host 语句的输出

powershell - PowerShell进度栏不会消失

google-chrome - 在Windows上清除C​​hrome或Firefox缓存的脚本

powershell - 有什么简单的方法可以检查 powershell 脚本的向后兼容性吗?

powershell - 使用 get-childItem 进行 PSRemoting 性能开销

Azure 自动化调用-Sqlcmd : Login failed for user '<token-identified principal>'

Powershell 使用选择字符串访问文件 - 访问被拒绝

powershell - 停止打印 foreach 循环 powershell 中元素的数量