动态修改文件内容中的DateTime
dynamic modfication of DateTime in the file content
我试图用当前时间修改文件中日志记录的日期和时间并将其保存在同一个文件中,但不知何故我无法更新它们。
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
好的,您应该可以根据此更改您需要的内容。我们所做的是编辑文件中的每一行。从每一行中解析出数据....然后操作所述日期并将其写回同一个文件。
在我的示例中,我们将日期移动到今天,同时保持与原始时间戳相同的时间。
# 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
示例数据输出基于 2015 年 11 月 1 日(今天)
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
我看到你在使用正则表达式。您 可能 对替换字符串中的控制字符有疑问。我的方法不太容易出错,并为日期操作开辟了更简单的方法。
我试图用当前时间修改文件中日志记录的日期和时间并将其保存在同一个文件中,但不知何故我无法更新它们。 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
好的,您应该可以根据此更改您需要的内容。我们所做的是编辑文件中的每一行。从每一行中解析出数据....然后操作所述日期并将其写回同一个文件。
在我的示例中,我们将日期移动到今天,同时保持与原始时间戳相同的时间。
# 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
示例数据输出基于 2015 年 11 月 1 日(今天)
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
我看到你在使用正则表达式。您 可能 对替换字符串中的控制字符有疑问。我的方法不太容易出错,并为日期操作开辟了更简单的方法。