简体   繁体   English

可以在不定义特定文件名的情况下使用Import-CSV cmd?

[英]Possible to use Import-CSV cmd without defining specific filename?

So in my current scenario I am moving a .csv file to a location where I want to execute a script to parse the .csv, removing specific data, and creating a new file with only that extracted data. 因此,在当前情况下,我将.csv文件移动到要执行脚本以解析.csv的位置,删除特定数据,并仅使用提取的数据创建一个新文件。

My code is fine and its all working as I want. 我的代码很好,所有代码都可以正常运行。

However now I'm running through another set of UAT while using data files from production. 但是,现在我正在使用另一组UAT,同时使用生产中的数据文件。 These .csv files come to us each day, datestamped. 这些带有.csv文件的文件每天都会寄给我们,带有时间戳。

I've written my script initially with a text.csv, and wish to automate this script on a schedule. 我最初使用text.csv编写了脚本,并希望按计划自动执行此脚本。 However now looks I need to define the csv file each time? 但是现在看起来我每次都需要定义csv文件吗? This is not feasible. 这是不可行的。

Is it possible to use the import-csv cmdlet to have it just load the csv file it sees in the folder? 是否可以使用import-csv cmdlet使其仅加载在文件夹中看到的csv文件?

Here is my code 这是我的代码

Import-Csv "\\pg-app05\Micros\Scripted Data Extraction\Sales\" |Where 
{$_.SITEID -like "7*"} | Select 
SITEID,DATE,REVENUECENTER,OVERGROUP,UNITS,PRICE,NETT,VATRATE,VAT,GROSS | 
ConvertTo-CSV -NoTypeInformation | % {$_ -replace '"',""} |Out-File "\\pg-
app05\Micros\Scripted Data Extraction\Sales\sales_$(get-date -f 
yyyy_MM_dd)_NI.csv" -fo -en ascii

This is returning an error, even with just one CSV file in the folder. 即使文件夹中只有一个CSV文件,这也会返回错误。 SALES_27062017_090019.csv SALES_27062017_090019.csv

There should only ever be one file present here, so wondering if its possible to have powershell just load the csv file, regardless of the filenamebeing defined? 这里应该只存在一个文件,所以想知道是否有可能使powershell仅仅加载csv文件,而不管定义的文件名如何?

I've tried 我试过了

Import-Csv "\\pg-app05\Micros\Scripted Data Extraction\Sales\*.csv"

And while it gave me a generated output file, was nothing in it. 虽然它给了我一个生成的输出文件,但里面什么也没有。

I think you could do: 我认为您可以:

(Get-ChildItem "\\pg-app05\Micros\Scripted Data Extraction\Sales\*.csv").FullName | Import-Csv

Because Get-ChildItem accepts wildcard parameters for its -Path parameter. 因为Get-ChildItem-Path参数接受通配符参数。

Your attempt of: 您的尝试:

Import-Csv "\\pg-app05\Micros\Scripted Data Extraction\Sales\*.csv"

Doesn't work because the -Path parameter of Import-CSV does not accept wildcard characters. 无法使用,因为Import-CSV-Path参数不接受通配符。

If you want to make sure you only ever get 1 CSV returned you could do: 如果要确保只返回1个CSV,可以执行以下操作:

(Get-ChildItem "\\pg-app05\Micros\Scripted Data Extraction\Sales\*.csv" | Select -First 1).FullName | Import-Csv

Import-CSV can , in fact, handle importing from multiple CSVs, but does not have any wildcard processing. 实际上, Import-CSV 可以处理从多个CSV的导入,但是不进行任何通配符处理。 So, you need to generate the list of CSVs - which may include only a single CSV - some other way: 因此,您需要生成CSV列表-其中可能仅包含一个CSV-其他方式:

Import-CSV ((Get-ChildItem -Path "\\pg-app05\Micros\Scripted Data Extraction\Sales\*.csv").FullName)

should work, as should the solution proposed by @MarkWragg 应该起作用,@ MarkWragg提出的解决方案也应该起作用

Thanks for the replies guys, appreciate the help. 感谢您的答复,感谢您的帮助。

So here is my final cut that is achieving what I want 所以这是我实现自己想要的目标的最后切入点

(Get-ChildItem "\\pg-app05\Micros\Scripted Data 
Extraction\Sales\*.csv").FullName | Import-Csv |Where {$_.SITEID -like "7*"} 
| Select 
SITEID,DATE,REVENUECENTER,OVERGROUP,UNITS,PRICE,NETT,VATRATE,VAT,GROSS | 
ConvertTo-CSV -NoTypeInformation | % {$_ -replace '"',""} |Out-File "\\pg-
app05\Micros\Scripted Data Extraction\Sales\sales_$(get-date -f 
yyyy_MM_dd)_NI.csv" -fo -en ascii

I'm now getting a generated outfile that contains the parsed data I need. 我现在正在生成一个包含我需要的已解析数据的生成文件。 On with more testing! 进行更多测试!

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

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