本文介绍了获取Powershell中管道的最后一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

这可能很奇怪,但是请和我在一起.我只想将管道结果的最后一个元素分配给变量.我当然知道如何用常规"代码来做到这一点,但是由于这必须是一成不变的.

This might be weird, but stay with me.I want to get only the last element of a piped result to be assigned to a varaiable.I know how I would do this in "regular" code of course, but since this must be a one-liner.

更具体地说,我对从FTP请求ListDirectoryDetails获取结果时获得文件扩展名感兴趣.

More specifically, I'm interested in getting the file extension when getting the result from an FTP request ListDirectoryDetails.

由于这是在字符串扩展中完成的,所以我找不到正确的代码.

Since this is done within a string expansion, I can't figure out the proper code.

目前,我正在接受最近的3 hars,但这真是令人讨厌.

Currently I'm getting the last 3 hars, but that is real nasty.

新对象PSObject-属性@ {
LastWriteTime = [DateTime] :: ParseExact($ tempDate,"MMM dd HH:mm",[System.Globalization.CultureInfo] :: InvariantCulture)
类型= $(if([int] $ tempSize -eq 0){目录"}否则{$ tempName.SubString($ tempName.length-3,3)})
名称= $ tempName
大小= [int] $ tempSize
}

New-Object PSObject -Property @{
LastWriteTime = [DateTime]::ParseExact($tempDate, "MMM dd HH:mm",[System.Globalization.CultureInfo]::InvariantCulture)
Type = $(if([int]$tempSize -eq 0) { "Directory" } else { $tempName.SubString($tempName.length-3,3) })
Name = $tempName
Size = [int]$tempSize
}

我的想法是做类似的事情

My idea was doing something similar to

    $ tempName.Split(.")| ? {$ _ -eq $ input [$ input.Length-1]} 

    $tempName.Split(".") | ? {$_ -eq $input[$input.Length-1]}

也就是说,遍历所有对象,但是只能取出我要查看的元素是输入数组的最后一个元素.

that is, iterate over all, but only take out where the element I'm looking at is the last one of the input-array.

我想念什么?

推荐答案

几种方法可以做到这一点:

A few ways to do this:

$name = 'c:\temp\aaa.bbb.ccc'

# way 1
$name.Split('.') | Select-Object -Last 1

# way 2
[System.IO.Path]::GetExtension($name)

# or if the dot is not needed
[System.IO.Path]::GetExtension($name).TrimStart('.')

这篇关于获取Powershell中管道的最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 12:14