对不起伙计们......我是PowerShell的新手。如果有人帮助处理以下情况会很棒:
我在文件夹c:\ test
中有几个文件sample.x.x.1
sample.x.x.2
sample.x.x.3
sample.x.x.4
sample.x.x.5
我想在给定文件夹中找到名称中编号最大的文件的名称。在上面的示例中,5是最高编号,脚本应将输出文件名作为sample.x.x.5
返回提前致谢!
答案 0 :(得分:5)
使用数字对文件名进行排序是一个很大的问题,因为有两种方法。第一个将它们设置为按字母顺序排列。也就是说,0, 1, 11, 111, 2,...
第二个使用自然顺序。也就是0, 1, 2, 11, 111...
。这非常棘手,每三个程序员都对此感到困惑。
已经有一个good answer,我会这样说,
# Create files 1..5
for($i=1;$i -le 5; ++$i) { set-content sample.x.x.$i -Value $null }
# Tricksy! Create file .10 to confuse asciibetic/natural sorting
set-content sample.x.x.10 -Value $null
ls # Let's see the files
Directory: C:\temp\test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2015-09-28 10:29 0 sample.x.x.1
-a---- 2015-09-28 10:29 0 sample.x.x.10
-a---- 2015-09-28 10:29 0 sample.x.x.2
-a---- 2015-09-28 10:29 0 sample.x.x.3
-a---- 2015-09-28 10:29 0 sample.x.x.4
-a---- 2015-09-28 10:29 0 sample.x.x.5
# Define helper as per linked answer
$ToNatural = { [regex]::Replace($_, '\d+$', { $args[0].Value.PadLeft20) }) }
# Sort with helper and check the output is natural result
gci | sort $ToNatural -Descending | select -First 1
Directory: C:\temp\test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2015-09-28 10:29 0 sample.x.x.10
答案 1 :(得分:1)
按字母排序。
using Akka.Actor;
using Akka.Event;
namespace Foo {
public class DeadLetterAwareActor : ReceiveActor {
protected ILoggingAdapter Log = Context.GetLogger();
public DeadLetterAwareActor() {
// subscribe to DeadLetters in ActorSystem EventStream
Context.System.EventStream.Subscribe(Self, typeof(DeadLetter));
Receiving();
}
private void Receiving() {
// is it my message being delivered to DeadLetters?
Receive<DeadLetter>(msg => msg.Sender.Equals(Self), msg => {
Log.info("My message to {0} was not delivered :(", msg.Recipient);
})
}
}
}
数字排序。
PS C:\Users\Gebb> @("sample.x.x.1", "sample.x.x.5", "sample.x.x.11") | sort
sample.x.x.1
sample.x.x.11
sample.x.x.5
最大数字。
PS C:\Users\Gebb> @("sample.x.x.1", "sample.x.x.5", "sample.x.x.11") |
sort -Property @{Expression={[Int32]($_ -split '\.' | select -Last 1)}}
sample.x.x.1
sample.x.x.5
sample.x.x.11