I have a requirement to read the lines of a txt file with a specific word "Release" to an array and get the first four characters of the last line in the array. Below is the code which I have used. My inputs will be textfilepath. Entries in the text file are as below.
1234 Debug Build1Rel Build2Dbg
1234 Release Build1Dbg Build2Dbg
1235 Release Build1Rel Build2Dbg
1235 Debug Build1Dbg Build2Dbg
1236 Release Build1Rel Build2Dbg
1236 Debug Build1Dbg Build2Dbg
Output I need is the first four characters (1236) in last line of Release. Thanks all for your prompt support. But now I need the same code in powershell.
Following code helped me to read the first four characters and the characters after third tab of last line and write the details to a XML file.
#Required parameters to set
param(
[Parameter(Position=0,Mandatory=$true)] [string]$ControlFilePath,
[Parameter(Position=1,Mandatory=$true)] [string]$MatchPattern,
[Parameter(Position=2,Mandatory=$true)] [string]$OutControlFile
)
If (Test-path $OutControlFile) {
Remove-Item $OutControlFile
}
$LastLine= get-content $ControlFilePath -ReadCount 1000 | foreach { $_ -match "$MatchPattern" } | Select-Object -Last 1
$BuildNumberDigit=$LastLine.Substring(0,4)
$BuildNumberFormat=($LastLine -split "`t")[2].substring(0)
# Create The Document
$XmlWriter = New-Object System.XMl.XmlTextWriter($OutControlFile,$Null)
# Set The Formatting
$xmlWriter.Formatting = 'Indented'
$xmlWriter.Indentation = 1
$XmlWriter.IndentChar = "`t"
# Write the XML Decleration
$xmlWriter.WriteStartDocument()
# Write the Document
$xmlWriter.WriteStartElement("BuildVersions")
$xmlWriter.WriteStartElement("property")
$xmlWriter.WriteAttributeString("name","BuildNumber")
$xmlWriter.WriteAttributeString("value","$BuildNumberDigit ")
$xmlWriter.WriteEndElement() # Closing Property
$xmlWriter.WriteStartElement("property")
$xmlWriter.WriteAttributeString("name","BuildNumberFormat")
$xmlWriter.WriteAttributeString("value","$BuildNumberFormat")
$xmlWriter.WriteEndElement() # Closing Property
$xmlWriter.WriteEndElement() # Closing BuildVersions
# End the XML Document
$xmlWriter.WriteEndDocument() #Closing Documents
# Finish The Document
$xmlWriter.Finalize
$xmlWriter.Close()
ControlFilepath ==> Path to the txt file where the code in question exists\FileName
MatchPattern ==> Pattern to search for in the txt file
OutControlFile ==> Path to the XML file\Filename
So the output XML file looks like
<BuildVersions>
<property name="BuildNumberDigit" value="1236 " />
<property name="BuildNumberFormat" value="Build1Rel" />
</BuildVersions>
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加