【问题标题】:Sort lines by date (DD.MM.YYYY)按日期对行进行排序 (DD.MM.YYYY)
【发布时间】:2016-12-09 10:42:13
【问题描述】:

是否可以按日期对文本文件中的行进行排序并将结果保存到另一个输出文件?每行都以日期 (DD.MM.YYYY) 开头。日期和文本之间的分隔符是制表符(没有空格)。我更喜欢 VBS 中的解决方案。

来源

25.11.1968 厄普顿·辛克莱逝世
14.06.1946 生日唐纳德特朗普
25.11.2016 菲德尔·卡斯特罗逝世
14.06.1969 生日 Steffi Graf
01.01.2017 新年

到新订单(target)

01.01.2017 新年
14.06.1946 生日唐纳德特朗普
14.06.1969 生日 Steffi Graf
25.11.1968 厄普顿·辛克莱逝世
25.11.2016 菲德尔·卡斯特罗逝世

顺序更改比较:月-日-年

【问题讨论】:

  • “我更喜欢 VBS 中的解决方案” - 那你应该写一个。本网站不是代码编写服务。
  • 好的,不那么刻薄的评论:请通读stackoverflow.com/help/how-to-ask 并相应地重写您的问题。您希望您尝试自己解决您的任务(这意味着“我已经搜索并没有找到任何我可以复制的东西”)。您应该包括到目前为止编写的代码,以及代码正在做什么以及您期望什么的描述。我们很乐意帮助您学习。不太乐意提供复制粘贴 sn-ps。
  • @Noodleslink 看起来很合适——不要被冗长的代码示例吓到——只要读到有命令行解决方案的底部即可。为面条点赞。再加上 Tomalak 的尖刻评论和不那么尖刻的评论。
  • 感谢@all,特别感谢 Noodles 的提示。我仍然是 VBS 的初学者,英语不是我的母语(见我的昵称)。所以期待一些有用的帮助来满足我的期望......我测试了 Script.vbs stackoverflow.com/questions/29552725/…,但参数 d 不起作用(从指定的列中提取时间或日期。寻找第一次约会)。我用欧洲日期 (DD-MM-YYYY) (DD.MM.YYYY) 和美国日期 (MM.DD.YYYY) 试了一下。其他论点工作正常。不懂块 ElseIf LCase(Arg(1)) = "d" then

标签: date vbscript text-files


【解决方案1】:

在这里,它并不漂亮,也没有按照你想要的顺序排序,但如果你像我一样学习的话,希望这能教你一些东西......

Option Explicit
Const fsoForReading = 1
Const fsoForWriting = 2

inputFile = "input.txt"
outputFile = "output.txt"

dim inputFile, outputFile, lineCount, file, fline, lDate, lYear, lMonth, lDay, iDate,output
dim a, d, i

dim objFSO, objFile

Set objFSO = CreateObject("Scripting.FileSystemObject")
set a = CreateObject("System.Collections.ArrayList")
set d = CreateObject("Scripting.Dictionary")

'open up the file for reading
Set objFile = objFSO.GetFile(inputFile)
Set file = objFile.OpenAsTextStream(fsoForReading,-2)
Do Until file.AtEndOfStream
    fline = file.ReadLine               'get the line
    lDate = left(fline,10)              'get the date portion at the beginning (the first 10 characters)
    lYear = split(lDate,".")(2)     'parse the year from the date
    lMonth = split(lDate,".")(1)        'parse the month
    lDay= split(lDate,".")(0)           'parse the day

    iDate = lYear + lMonth + lDay       'put together the "index date".  The date needs to be formatted as yyyymmdd so it is sortable

    a.add iDate                             'add the index date to an array for easier sorting
    d.add iDate, fline                  'add the index date and line contents to a dictionary object

Loop                                            'go to the next line in the file

a.Sort()                                        'sort the array of dates
for each i in a                         'loop through the array of dates
    output = output + d(i) + vbCrlf 'add the appropriate dictionary object to the output
Next

call writeFile(output)                  'write the file
file.Close


WScript.Quit 0

sub writeFile(fc)
    dim fso
   'fn = fn + ".hl7"
    Set fso = CreateObject("Scripting.FileSystemObject")
    if (fso.FileExists(outputFile)) then
        Set objFile = fso.OpenTextFile(outputFile,8,True)
        objFile.writeline fc
        objFile.Close  
    else
        Set objFile = fso.CreateTextFile(outputFile,True)
        objFile.writeline fc
        objFile.Close  
    end if
end sub

【讨论】:

  • 对于反对者,我不会只为提问者回答问题 (?)。这对我来说是一个很好的挑战。另外,请记住,并非每个人都学得一样。
  • 感谢 lisitng,它看起来不错,但在“此键已与此集合的元素关联”行中产生错误 (d.add iDate, fline)。
  • 嗯,没想到两条线的日期相同。我确定这就是问题所在。
  • 尝试更改索引以包含 iDate 并且整行这样排序仍然有效,但键应该是唯一的。
  • 我必须更改此iDate = lMonth + lDay + lYear 以获得所需的排序。要拥有唯一的日期,我只需附加一个随机数
猜你喜欢
  • 1970-01-01
  • 2020-10-30
  • 2015-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-03
相关资源
最近更新 更多