简体   繁体   English

CSV文件生成新名称-调用Powershell

[英]CSV file generates new name - call powershell

I want to call a CSV file from a Powershell script, which I know how to do. 我想从Powershell脚本中调用CSV文件,我知道该怎么做。 However the problem is, I need to do this with a new CSV file name everyday ( a new CSV is generated everyday). 但是问题是,我每天都需要使用一个新的CSV文件名来执行此操作(每天都会生成一个新的CSV)。 Is there anyway to do this without having to change the name of the file on the PS script everyday? 无论如何,不​​必每天都更改PS脚本上文件的名称吗?

$csv = Import-Csv '$D\path to csv'

$headers = $csv | Get-Member -MemberType NoteProperty | select -Expand Name

  foreach ($header in $headers) {
  foreach ($data in $csv.$header) {

   }

 }

Thanks, 谢谢,

You can use Get-Childitem to get files/directories in a directory. 您可以使用Get-Childitem获取目录中的文件/目录。 You can combine it with -Filter to get only CSVs, Sort-Object to sort by LastWriteTime (most recent CSV at the top) and Select-Object so you only get the most recent file. 您可以将其与-Filter组合使用,以仅获取CSV,可以将Sort-ObjectLastWriteTime (在顶部为最新CSV)和Select-Object因此只能获取最新的文件。

$myCsv = Get-ChildItem '$D\path to csv' -Filter *.csv |
            Sort-Object LastWriteTime -Descending |
            Select-Object -First 1

$csv = Import-Csv $myCsv

$headers = $csv | Get-Member -MemberType NoteProperty | select -Expand Name

  foreach ($header in $headers) {
  foreach ($data in $csv.$header) {

   }

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM