我正在PowerShell中编写脚本,该脚本将在特定时间范围内从共享文件夹中获取文件(在这种情况下,该时间介于今天的上午8:00和昨天的上午6:00之间-根据文件,则在此时间段内大约有14个文件)。我要获取其他4000+ .csv文件中的这些特定文件的工作是获取其“文件名”,并与该特定时间范围规则进行比较。
文件名示例:“2019.09.17-06.00.OutPutAXIS”
重点是获取第一个文件的第一行并将其用作报告的新标题,然后,根据该标题添加其他.csv文件中的所有数据。
E.g。
HEADER (Gotten from the first file)
1 FILE DATA (Gotten from the first file)
2 FILE DATA (Gotten from the second file)
3 FILE DATA (Gotten from the third file)
and so on...
这里的问题是大约需要50分钟才能完成。我已经读过有关cgi确实很慢的文章,但是我找不到其他可以解决问题的替代方法。
尝试几次并站在我这边搜索:
Powershell Get-ChildItem most recent file in directory
Merging multiple CSV files into one using PowerShell
https://www.reddit.com/r/PowerShell/comments/51q9s5/getchilditem_ist_very_slow_when_dealing_with_a/
https://devblogs.microsoft.com/powershell/why-is-get-childitem-so-slow/
$Today_Date = [DateTime]::Today.AddHours(8).ToString("yyyy.MM.dd-hh.mm")
$Yesterday_Date =
[DateTime]::Today.addDays(-1).AddHours(6).ToString("yyyy.MM.dd-hh.mm")
[array]$Name_Files = [System.IO.Directory]("$IN_FILE_PATH\*.csv") | Where-
Object {$_.LastWriteTime -gt (Get-Date).AddDays(-2)} | Select-Object -
ExpandProperty BaseName
该函数将遵守时间框架的文件彼此分开,然后添加到将要合并的文件夹中。#>
foreach ($name in [array]$Name_Files -replace ".OutPutAXIS", "") {
if (($name -ge $Yesterday_Date) -AND ($name -le $Today_Date)) {
Move-Item -path "$IN_FILE_PATH\$Name.csv" -destination "$MERGING_FILE_PATH"
///
}
try {
if ($getFirstLine = $true) {
get-childItem "$MERGING_FILE_PATH\*.csv" | ForEach {
$filePath = $_
$lines = $lines = Get-Content $filePath
$linesToWrite = switch ($getFirstLine) {
$true { $lines }
$false { $lines | Select -Skip 1 }
}
#Import all the information and tranfer to the new workbook.
$Report_name = $((get-date).ToString("yyyy.MM.dd-hh.mm"))
$getFirstLine = $false
Add-Content "$OUT_FILE_PATH\Report $Report_Name.csv" $linesToWrite
}
}
$LogDate = (Get-Date).ToString("dd-MMM-yy hh:mm:ss")
$Log += $LogDate + " - SUCCESS: Successfully retrieved all data from the .csv files and merged to create the report."
}#try end
catch {
$LogDate = (Get-Date).ToString("dd-MMM-yy hh:mm:ss")
$Log += $LogDate + " - FAILED: Error while trying to process the report. Could not retrieve all the data and/or merge the .csv files." + $_.Exception.ToString
}#catch end
它正在工作,但是我需要性能要快得多。我是PowerShell的新手,所以我找不到更好的方法。如果花了大约5分钟,那就太好了。
如果CGI对您来说很慢,请改用-Filter参数。在哪里对象过滤将根据给定条件评估目录中的每个项目,而-Filter则不会。