我想创建一个ps1,它将文件夹中的所有.csv拉出来,拉出2列(由标题标识)下的所有值,然后为每个只有这两列的文件吐出新的.csv。
我可以让它在下面的单个文件上工作,但是当我添加一个通配符时,它说功能对多个文件不起作用。我尝试了其他方法,但我没有正式的CS背景(所以下面的“脚本”可能看起来很简陋);我接近但到目前为止没有任何工作。
$inputpath = 'C:/Users/AAA/AAA/AAA/AAA/'
$inputfile = 'BBB.csv'
$outputpath = $inputpath
$outputfile = $inputfile.basename + 'edited' + '.csv'
Import-Csv $inputpath$inputfile |
select COLUMNtitle1, COLUMNtitle2 |
Export-Csv -Path $outputpath$outputfile -NoTypeInformation
echo
答案 0 :(得分:2)
由于您未将Get-ChildItem
用于$inputfile
,因此没有basename
属性。在加入路径时使用Join-Path
更安全,更好地避免+
连接字符串。
$inputfolder = 'C:\Users\AAA\AAA\AAA\AAA\'
Get-ChildItem $inputfolder -include *.csv | % {
$outputpath = Join-Path $inputfolder "$($_.basename)edited.csv"
Import-Csv $_.fullname | select COLUMNtitle1,COLUMNtitle2 | Export-Csv -Path $outputpath -NoTypeInformation
}