2015-11-02 65 views
1

我試圖用當前時間修改文件中日誌記錄的日期和時間,並將其保存在同一個文件中,但不知何故,我無法更新它們。 DateTime正在動態變化,我想要按順序更改每個記錄的DateTime。在文件內容中動態修改DateTime

正如你看到的下面,日期時間用秒遞增一起毫秒變化 - 有什麼辦法,我可以修改根據當前系統時間遞增的文件嗎?我嘗試了以下方法 - 我知道這是不正確的,但試着看看是否有效。任何意見或建議要解決,請幫助。

環境:Windows 2008 & Windows 2003的

$PATH = "D:\Log\focusServer\focusServer.txt" 
$content = "2015-10-27 15:50:21,900 [ListenerThread0] WARN focus.Core.Server.States.InvalidIPRangeState - Begin 'InvalidIPRangeState' for application '|did:N/A^ep:10.160.210.222:40534^iprg:N/A|' 
2015-10-27 15:50:21,900 [ListenerThread0] INFO focus.Core.Server.Connection.DcmpConnection - |did:N/A^ep:01.60.210.222:40534^iprg:N/A|: Connection Established 
2015-10-27 15:50:49,993 [12 ] INFO focus.Core.Server.Connection.DcmpConnection - |did:N/A^ep:01.60.213.172:39158^iprg:N/A|: Connection Closed. Reason: Socket closed by remote party (0-byte packet received) 
2015-10-27 15:50:49,994 [ListenerThread0] WARN focus.Core.Server.States.InvalidIPRangeState - Begin 'InvalidIPRangeState' for application '|did:N/A^ep:01.60.213.172:39158^iprg:N/A|' 
2015-10-27 15:50:49,994 [ListenerThread0] INFO focus.Core.Server.Connection.DcmpConnection - |did:N/A^ep:01.60.213.172:39158^iprg:N/A|: Connection Established" 

$toReplace = "2015-10-27" 
$updateContent = [DateTime]::Now.Add(0).AddHours(0).AddMinutes(0).addseconds(0).toString() 
$convertDate = ([datetime]::ParseExact($updateContent,"dd/MM/yyyy HH:mm:ss",$null)) 
$convertDate.year.toString() + "-" + $convertDate.month.toString() +"-"+ 
$convertDate.day.toString() + " " + $convertDate.Hour.toString() + ":" +$convertDate.Minute.toString() + ":" + $convertDate.Second.toString() 

Add-Content -Value $content -Path $PATH 

(Get-Content $PATH) | 
Foreach-Object {$_ -replace $toReplace,$updateContent} | 
Out-File $PATH 
+0

你可以展示一旦完成後你會希望它看起來像什麼嗎?我可以看到你所要求的多種解釋。你只是更換日期而不是時間? – Matt

+0

您的解析確切格式化字符串與您的示例數據不匹配。注意數據中的破折號和格式字符串中的斜槓。 – Matt

+0

最初,我試圖只用日期來做到這一點,但似乎是我需要改變時間。我試圖改變每個記錄順序的日期和時間,並保存在像同一個文件: 2015年10月27日15:50:21900 [ListenerThread0] WARN焦點..... 2015年10月27日15: 50:22,500 [ListenerThread0] INFO focus ..... 2015-10-27 15:50:22,900 [ListenerThread0] WARN關注..... 2015-10-27 15:50:23,400 [ListenerThread0] INFO focus ..... – user1911509

回答

0

好吧,你應該能夠改變,你需要在此基礎上的。我們所做的是編輯文件中的每一行。解析每行的數據....然後操縱日期並將其寫回相同的文件。

在我的示例中,我們將日期移至今天,同時保持原始時間戳的相同時間。

# file formatting. Note the space on the end is on purpose. 
$formatting = "yyyy-MM-dd HH:mm:ss,fff " 
$currentDate = Get-Date 

(Get-Content $PATH) | Foreach-Object { 
    # Split to get the date on the first occurrence of [. 
    $parsed = $_.split("[",2) 
    # Convert to datetime 
    $date = [datetime]::ParseExact($parsed[0], $formatting, $null) 
    # Get the new time by adding the current date and the time from the parsed data 
    $manipulated = $currentDate.Date + $date.TimeOfDay 
    # Convert new time to the old format from file and append remainder of line from file. 
    $manipulated.ToString($formatting) + "[" + $parsed[1] 
} | Set-Content $PATH 

採樣數據輸出基於11月1日,2015年(今天)

2015-11-01 15:50:21,900 [ListenerThread0] WARN focus.Core.Server.States.InvalidIPRangeState - Begin 'InvalidIPRangeState' for application '|did:N/A^ep:10.160.210.222:40534^iprg:N/A|' 
2015-11-01 15:50:21,900 [ListenerThread0] INFO focus.Core.Server.Connection.DcmpConnection - |did:N/A^ep:01.60.210.222:40534^iprg:N/A|: Connection Established 
2015-11-01 15:50:49,993 [12 ] INFO focus.Core.Server.Connection.DcmpConnection - |did:N/A^ep:01.60.213.172:39158^iprg:N/A|: Connection Closed. Reason: Socket closed by remote party (0-byte packet received) 
2015-11-01 15:50:49,994 [ListenerThread0] WARN focus.Core.Server.States.InvalidIPRangeState - Begin 'InvalidIPRangeState' for application '|did:N/A^ep:01.60.213.172:39158^iprg:N/A|' 
2015-11-01 15:50:49,994 [ListenerThread0] INFO focus.Core.Server.Connection.DcmpConnection - |did:N/A^ep:01.60.213.172:39158^iprg:N/A|: Connection Established 

我看到你正在使用正則表達式。你可能有你的替代字符串中的控制字符有問題。我的方法不太容易出錯,併爲日期操作打開了更簡單的方法。

+0

非常抱歉,非常感謝。示例數據正是我正在尋找的,但更多的是動態的。該腳本完美地工作,但是當我第二次運行腳本時,我得到了異常。 我猜是因爲數據已經存在於當前日期以及當我刪除數據並運行它時 - 我完美地工作。有沒有辦法,我可以每次運行代碼時動態更改DateTime。 – user1911509

+0

我已經想通了 - 添加刪除該內容,作爲第一步 - 所以每次運行前 - 它清除的內容和插入所述內容與當前日期修改。請讓我知道這是否是一種好方法。 – user1911509

+0

今天再次嘗試代碼時,它只改變日期,但不改變時間。像小時,分鐘和秒鐘不會改變 - 它們保持不變,在我們的變量中我們聲明瞭這些。 – user1911509