2016-12-09 4 views
-3

テキストファイル内の行を日付順にソートし、結果を別の出力ファイルに保存する可能性はありますか?各行は日付(DD.MM.YYYY)で始まります。日付とテキストの間の区切り文字はTab(スペースなし)です。私はVBSのソリューションを好む。日付で行を並び替える(DD.MM.YYYY)

ソース

25.11.1968 Death of Upton Sinclair 
14.06.1946 Birthday Donald Trump 
25.11.2016 Death of Fidel Castro 
14.06.1969 Birthday Steffi Graf 
01.01.2017 New Year

変更と比較するため

01.01.2017 New Year 
14.06.1946 Birthday Donald Trump 
14.06.1969 Birthday Steffi Graf 
25.11.1968 Death of Upton Sinclair 
25.11.2016 Death of Fidel Castro

新しい順(ターゲット)に:月 - 日 - 年

+2

* "私はVBSの解決策が好きです。このサイトはコード作成サービスではありません。 – Tomalak

+1

http://stackoverflow.com/questions/29552725/sorting-files-by-numerical-order –

+2

よろしくお願いします。snarky comment:http://stackoverflow.com/help/how-to-askを読んでそれに応じてあなたの質問を書いてください。自分自身であなたの仕事を解決しようとしていることがあなたから期待されます(そして、それは "私は検索して何もコピーできないことを発見しました"という意味を超えています)。これまでに書いたコードと、そのコードが何をしているのか、そして何を期待しているのかを記述してください。私たちはあなたが学ぶのを手伝ってうれしいです。あまりにもコピー&ペーストスニペットを提供していない。 – Tomalak

答えて

0

ここで、それはかなりありませんあなたが望むのと逆の順序でソートしませんが、うまくいけば、これはあなたが何かを学ぶならば何かを教えるでしょう私はそうします...

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 
+0

ダウンローダーの場合、私は質問者(?)の質問にしか答えません。これは私にとっては挑戦でした。さらに、誰もが同じことを学ぶわけではないことを覚えておいてください –

+0

lisitngありがとうございますが、見栄えは良いですが、「このキーは既にこのコレクションの要素に関連付けられています」という行にエラーが表示されます(d.add iDate、fline)。 – Gurkenhobel

+0

ええと、同じ日付の2行については考えていませんでした。私はそれが問題だと確信しています。 –

関連する問題