2017-08-24 17 views
1

我在E:\ MyFiles \和E:\ MyFiles \ August中有文件。我正在使用PowerShell将文件路径存储在csv文件(E:\ CSVPaths.csv)中。System.Object [] - 当文件名相同时

这是我的脚本:

$exclude = @(".html", ".tt", ".xaml", ".csproj", ".sln", ".xml", ".cmd", ".txt",".svn") 
"DirectoryPath, SourceFileName" | Out-File -FilePath "E:\CSVPaths.csv" -Encoding ASCII 
$table = (get-childitem -recurse "E:\MyFiles" -File -ErrorAction SilentlyContinue | Where-Object { $exclude -notcontains $_.Extension }).Fullname 
foreach ($row in $table) 
{ 
    $file=Get-ChildItem -recurse $row 
    $fileObject = new-object PSObject 
    $fileObject | add-member -membertype NoteProperty -name "DirectoryPath" -Value $file.DirectoryName 
    $fileObject | add-member -membertype NoteProperty -name "SourceFileName" -Value $file.Name 
    $newrow=$fileObject 
    Export-Csv "E:\CSVPaths.csv" -inputobject $newrow -append -Force 
} 

在这两个E:\ MYFILES和E:\ MYFILES \文件夹月,有同名文件(例如:Audit_Report.csv)。虽然出口到它们被存储为System.Object[]DirectoryPathSourceFileName CSV(如下所示):

DirectoryPath  SourceFileName 

System.Object[]  System.Object[] 
E:\MyFiles\August Audit_Report.csv 

请帮助解决这个问题。

+0

_shell script_?你的意思是PowreShell? – Clijsters

+0

@Clijsters:Powershell – user3189916

回答

1

AFAICS之后没有必要使用循环来获取输出。

$exclude = @(".html", ".tt", ".xaml", ".csproj", ".sln", ".xml", ".cmd", ".txt",".svn") 
"DirectoryPath, SourceFileName" | Out-File -FilePath "E:\CSVPaths.csv" -Encoding ASCII 

Get-ChildItem -recurse "E:\MyFiles" -File -ErrorAction SilentlyContinue | 
    Where-Object { $exclude -notcontains $_.Extension } | 
    Select-Object @{n="SoureFileName";e={$_.Name}},@{n="DirectoryPath";e={$_.DirectoryName}} | 
    Export-csv "E:\CSVPaths.csv" -Force -NoTypeInformation 
    # NB: The "-NoTypeInformation" is an assumption - remove if you what type info. 
+1

完美。工作很好。谢谢。 – user3189916

相关问题