开发者

Writing list of ChildItem for a list of paths to a CSV file

What I am attempting to do is have an end CSV file like this:

path , get-childItem

path , get-childItem

path , get-childItem

etc.

I am very new to powershell so as of right now the issue I am having isn't with errors but rather actually knowing what to do

Here is my code thus far:

$data = gc "c:\Documents and Settings\user\Desktop\paths.txt"
ForEach($line in $data) {
$subName = Get-ChildItem "$line"
write-host $line","$subName
#Export-Csv -Path "C:\Documents and Settings\reidli\Desktop\mytest.csv" -Force "true" -InputObject $output 

So I am reading a list of paths from paths.txt where each line is a different path $subName is the list of Children i want and is correct and when I write-host the output开发者_如何学编程 is in fact: path,children commentented out is where I attempted to export csv, but this wont work as it would over-write the existing csv for each path

Can someone please help me take each line in the foreach loop and create a .csv file with each path and children?


just don't use export-csv at all. CSV is plain comma separated text file. which makes it easy to export:

$data = gc "c:\Documents and Settings\user\Desktop\paths.txt"
Remove-Item "C:\Documents and Settings\reidli\Desktop\mytest.csv" -ea 0
ForEach($line in $data) {
  $subName = Get-ChildItem "$line"
  write-host $line","$subName
  "$line,$subName" | Add-Content "C:\Documents and Settings\reidli\Desktop\mytest.csv" 
}

btw. do you really want the sub files space separated? it is what you get when you convert Get-ChildItem to string


Try this:

$data | select $_ , @{L="subname";E=@{(gci $_)}} | Export-Csv "C:\Documents and Settings\reidli\Desktop\mytest.csv" -force
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜