Powershell导入-基于时间戳的CSV选择

Powershell导入-基于时间戳的CSV选择,powershell,powershell-3.0,Powershell,Powershell 3.0,关于powershell导入CSV选项的另一个问题 文件夹里有两个文件 文件1(最近的) 文件2(第二个最新版本) 我用下面的命令选择了最近的文件 $Filerecentdir = "Dir path" $Filter = "*.csv" $Filerecent = Get-ChildItem -Path $Filerecentdir -Filter $Filter | Sort-Object LastAccessTime -Descending | Select-Object -First 1

关于powershell导入CSV选项的另一个问题 文件夹里有两个文件 文件1(最近的) 文件2(第二个最新版本)

我用下面的命令选择了最近的文件

$Filerecentdir = "Dir path"
$Filter = "*.csv"
$Filerecent = Get-ChildItem -Path $Filerecentdir -Filter $Filter | Sort-Object LastAccessTime -Descending | Select-Object -First 1
如何选择文件2(第二个最近的文件)?所有这些文件都包含时间戳
谢谢你的帮助

您可以使用
-Skip
参数:

Select-Object -Skip 1 -First 1
扩展的
考虑使用简化代码。请记住,您将获得文件的数组,按
LastAccessTime
的降序排序;您始终可以索引到该数组中

$location = @{
    "Path"   = "D:\Path\To\Files"
    "Filter" = "*.csv"
}

$files = Get-ChildItem -Path @location |
                Sort-Object LastAccessTime -Descending

$files | Out-Host

$firstFile     = $files | Select-Object -First 1
$firstFileAlso = $files[0]

$secondFile     = $files | Select-Object -First 1 -Skip 1
$secondFileAlso = $files[1]

获取第二个最新的CSV文件:

Get-ChildItem *.csv |
  Sort-Object LastWriteTime -Descending |
  Select-Object -Index 1

(第0个索引将是最新的文件。)

另一个只是为了好玩:

$Filerecent = (Get-ChildItem -Path $Filerecentdir -Filter $Filter | Sort-Object LastAccessTime -Descending)[1]

当我使用索引0和1运行时,我得到的是第一个和第三个文件,而不是第二个$Filerecentdir=“D:\tcTouchPoints\候选者\u Import\inbound”$Filter=“.csv”$Filerecent=get ChildItem-Path$Filerecentdir-Filter$Filter |排序对象LastAccessTime-Descending |选择对象-索引0$FileA=$Filerecent.name$Filesecondrecentdir=“D:\TCCTouchpoints\Candidate\u Import\inbound”$Filtersecond=“.csv“$Filesecondrecent=Get ChildItem-Path$Filesecondrecentdir-Filter$Filtersecond | Sort Object LastAccessTime-Descending | Select Object-Index 1$FileC=$Filesecondrecent.Name如果这是一个问题,您需要用您尝试过的内容更新您的问题,并告诉您遇到的具体问题。正确的格式至关重要。我得到的是第一个和第三个文件,而不是第二个$Filerecentdir=“D:\TCCTouchpoints\候选者\u Import\inbound”$Filter=“.csv”$Filerecent=get ChildItem-Path$Filerecentdir-Filter$Filter |排序对象LastAccessTime-Descending |选择对象-first 1$FileA=$Filerecent.name$Filesecondrecentdir=“D:\TCCTouchpoints\Candidate\u Import\inbound”$Filtersecond=“.csv“$Filesecondrecent=Get ChildItem-Path$Filesecondrecentdir-Filter$Filtersecond |排序对象LastAccessTime-降序|选择对象-跳过1-第一个1$FileC=$Filesecondrecent.name。是否正确?@Divya您需要更改您的过滤器以包含
*
-很惊讶它返回了任何东西!假设是,它应该返回第一个和第二个。您不需要为
Filesecondrecent
创建一组单独的变量。请参阅更新的示例以了解简化。
$files | Out主机
部分将打印列表,因此您可以检查是否获得第一个和第二个。请记住,
$files
将已经是一个排序列表,因为您使用了
排序对象。您的意思是第二个文件也指向文件[0]。然后再次加载第一个文件$Filerecentdir=“D:\TCCTouchpoints\Candidate\u Import\inbound“$Filter=“*.csv”$FileArray=Get ChildItem-Path$Filerecentdir-Filter$Filter | Sort Object LastAccessTime-Descending$Filerecent=$FileArray |选择对象-first 1$FileA=Import csv$FileArray[0]$Filesecondrecent=$FileArray |选择对象-第一个1-跳过1$FileC=导入CSV$FileArray[1]。有了这个,它仍然在加载第三个文件FileC@Divya不,这是一个错误更正。不知道你为什么会得到第三个文件。是的,要么是第一个文件,要么是第三个文件。它没有考虑第二个:)让我仔细看看,然后再回来。谢谢你的帮助!这是给我的错误在第3行字符:109+。。。e-在表达式或语句中降序[0]+~~~意外标记“[0]”。第3行字符:110+-降序)[0]+~在“[”之后缺少类型名称。在第9行,在表达式或语句中字符数:127+…e-降序)[1]+~~意外标记“[1]”。在第9行,字符数:128+…-降序)[1]+~在“…”之后缺少类型名称['.第3行的实际代码行是什么?$Filerecent=(Get ChildItem-Path$Filerecentdir-Filter$Filter | Sort Object LastAccessTime-Descending)[1]我看不出有任何语法错误。我会检查它周围的行。