【问题标题】:Powershell to loop over csv's and place them into a new folder using its original namePowershell遍历csv并使用其原始名称将它们放入一个新文件夹中
【发布时间】:2020-01-30 17:14:53
【问题描述】:

我已经写出了下面的powershell:

$Headers = @(
 "Purchase Order"
  "SKU"
  "Markdown"
  "Landed Cost"
  "Original Price"
  "Current Sale Price"
  "Free Stock"
  "OPO"
  "ID Style"
  "Supplier Style No")


$Location = "\\dc1\shared\BI\HisRms"
$Destination = "C:\Users\jonathon.kindred\Desktop\RMM\"
$files = Get-ChildItem $location -Recurse -Filter "*.csv"


Foreach ($file in $files) { Select $Headers | Export-Csv Join-Path "C:\Users\jonathon.kindred\Desktop\RMM\" $_.FullName -Force -NoTypeInformation}

我遇到的问题是它在所有文件上都失败了,因为我没有硬编码文件路径以使 .csv 结尾。它是怎么做到的,所以我加入路径 $Destination 和原始文件名?

【问题讨论】:

  • Join-Path $Destination $file.Name.

标签: powershell for-loop filenames export-csv


【解决方案1】:

在您的 foreach 循环中,您可以使用以下内容:

Export-Csv -Path (Join-Path $Destination $file.Name) -NoType

Get-ChildItem 在查询文件时返回 FileInfo 对象。 $files 在您的代码中包含这些对象。每个对象都包含属性FullNameBaseNameNameFullName 包含文件名、文件路径和扩展名。 BaseName 包含不带路径和扩展名的文件名。 Name 包含带扩展名的文件名,但不包括路径。

【讨论】:

  • 谢谢,这已经奏效,但它没有选择我的标题,只是复制空白文件,有什么想法吗?
  • 输出到 CSV 需要一个具有属性的对象。你可以做"" | select $headers | Export-CSV ....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-25
  • 2018-10-11
  • 1970-01-01
  • 1970-01-01
  • 2017-02-19
  • 2019-05-10
相关资源
最近更新 更多