Powershell如何在export-csv

时间:2016-04-26 07:19:41

标签: parsing powershell export-to-csv text-mining

我的脚本通过文本挖掘文件构造一个对象,然后将其输出为LookupTable.csv。 .CSV的第一列包含从.TXT文件读取的第二行。第二列包含文件的原始名称。

每个.TXT文件的第二行是一个数字ID,当前被误解为数字。如何强制PowerShell将值传递到.CSV?或者为什么.CSV会错误解释这些值?

例如,值“-696706651--001”显示为“-696706651--1”,即使.TXT中的第二行是“-696706651--001”。似乎这主要/仅在我尝试提取的唯一值在其前面有“ - ”时发生,即“700111651--001”在我的输出中正确显示为“700111651--001”.CSV。< / p>

SCRIPT

# Construct an out-array to use for data export
$OutArray = @()

# Import CSV containing location of TXT files to search (the 2nd line is retrieved from each file).
$Files = Import-CSV 'C:\Data\PROJECTS\SearchDocs.csv' -Header ("TxtName","PdfName")

ForEach($file in $Files)
{
    # Look at second line of the text file and pass it to newName
    $newName = Get-Content -Path $file.TxtName | Select-Object -Index 1

    # suffix .pdf to newName
    $newName2 = $newName + ".pdf"

    # Rename the file using the second line found in the file
    Rename-Item $file.PdfName $newName2

    #Construct an object for the export
    $myobj = "" | Select "fileID","Title"

    #fill the object
    $myobj.fileID = $newName   # This value does not always match the text read
    $myobj.Title = $file.PdfName

    #Add the object to the out-array
    $outarray += $myobj

    #Wipe the object just to be sure
    $myobj = $null
}

#After the loop, export the array to CSV
$outarray | export-csv "C:\Data\PROJECTS\LookupTable.csv"

0 个答案:

没有答案