在批处理数据中,无法从CSV处理,单独处理

时间:2018-03-19 07:00:08

标签: powershell csv

这是我的上一个问题,我开发了一个工作脚本作为答案Powershell- separate one csv row into multiple rows based on contents of a single cell

现在我试图只处理整个文件夹而不是单个文件(所有文件的格式与我原来的问题相同),并且输出文件夹中有文件,但它们没有数据。我在其他脚本中成功完成的$files = Get-ChildItem过程,以及处理数据的方式与上一个问题相同(此处显示单个文件)

$x = Get-Content 'D:\temp\Carmel-IN.csv'
$exportlocation = 'D:\temp\Carmel-IN.csv'
$y = $x | ConvertFrom-Csv -Delimiter ','
$y | Foreach {
    $current = $_
    $current.zip -split ' ' | foreach {
        [PSCUstomObject]@{ 
            City  = $null
            State = $null
            hood  = $null
            zip   = $_
            lat   = $null
            lng   = $null
        }
    }
} | Export-Csv -Append -NoTypeInformation -Delimiter ',' -Force $exportlocation | % {
    $_ -replace '"',''
}

但是尝试使用批处理文件完成相同操作只会在目标目录中为我提供空文件。认为这是" $y = $($file.Name) "更改,我也尝试了" $y = $file.Name "" $y = $file ",结果相同。

$files = Get-ChildItem 'D:\!__DATA for searches\tempDataForECLsw\*.csv' 

# Loop through each file
foreach ($file in $files) { 
    # Imports CSV file, selects desired zip data, and then exports as CSV to the desired destination
    $y = $($file.Name) | ConvertFrom-Csv 
    $y | ForEach-Object {
        $current = $_
        $current.zip -split ' ' | foreach {
            [PSCUstomObject]@{ 
                City  = $null
                State = $null
                hood  = $null
                zip   = $_
                lat   = $null
                lng   = $null
            }
        }
    } | Export-Csv -Path "D:\!__DATA for searches\tempECL\$($file.Name)" -Append -NoTypeInformation -Delimiter ',' -Force | % {
        $_ -replace '"',''
    }
}

0 个答案:

没有答案
相关问题