将变量的内容附加到文件中

时间:2018-02-14 20:55:41

标签: xml powershell variables

我有这个工作脚本,但我认为它可以做得更干净。所以,从一点背景开始......

我正在收到另一家公司的XML文件。该文件包含一个标题,然后是所有没有任何CRLF字符的XML,然后是一个预告片。我们正在将此文件上传到z / OS主机,并且它不喜欢那个令人难以置信的长行。

我创建了这个脚本来删除标题和预告片,重新格式化XML然后重新打开标题和预告片。它可以工作,但是在脚本的某一点上我必须写一个临时文件然后再读回临时文件,这对我来说似乎效率低下。

任何人都有任何建议在下面的脚本中删除“Temp_2_DAY2FileOUT”文件:

############# CODE BEGINS #############

## Count the number of files matching the file mask provided
$x = ( Get-ChildItem "\\corp\dfs\Transfer\GAB\*DAY2FileOUT" | Measure-Object ).Count

## If the number of files from the previous command is not equal to 1 run the command with the verbose parameter to populate the log with the command results,
## then terminate with a bad exit code (indicating a failure)
If($x -NE 1){
    Get-ChildItem "\\corp\dfs\Transfer\GAB\*DAY2FileOUT" -verbose
    Exit 8}

## Read the file into a variable, named input, as a Powershell Object
$input = Get-Content "\\corp\dfs\Transfer\GAB\*DAY2FileOUT"
## Read the first record of the input variable into a new variable, named header, as a Powershell Object
$header = $input[0..0]
## Read all but the first and last record of the input variable into a new variable, named content, as a Powershell Object
$content = $input[1..($input.count - 2)]
## Read all the last record of the input variable into a new variable, named trailer, as a Powershell Object
$trailer = $input[($input.count - 1)..($input.count - 1)]

## Write the content variable out to a temporary file so that we have a file without the header and trailer records
$xml = [xml] $content
$xml.Save("\\corp\dfs\Transfer\GAB\Temp_2_DAY2FileOUT")

##  Write the header record to the final output file
$header | Out-File "\\corp\dfs\Transfer\GAB\DAY2FileOUT.xml" -Encoding ascii

##  Append the contents of the temproary file to the final output file
(Get-Content "\\corp\dfs\Transfer\GAB\Temp_2_DAY2FileOUT") | Out-File -FilePath "\\corp\dfs\Transfer\GAB\DAY2FileOUT.xml" -Encoding ascii -Append

## Append the trailer record to the final output file
$trailer | Out-File "\\corp\dfs\Transfer\GAB\DAY2FileOUT.xml" -Encoding ascii -Append

##  Delete the input file and the temporary file
#Remove-Item "\\corp\dfs\Transfer\GAB\*DAY2FileOUT"

## Terminate with a success status
Exit 0
############## CODE ENDS ##############

1 个答案:

答案 0 :(得分:0)

您可以使用Save()方法将信息存储在变量而不是文件中。还有很大的空间可以让代码更易读,更容易维护。

我已将TechSpud's helpful answer用于整合。您可以继续使用WriteTo(),而不是切换到Save()方法。它有一个覆盖来接受TextWriter而不是文件路径string

首先需要创建 StringWriter 对象,然后创建 XmlTextWriter 对象。

############# CODE BEGINS #############

# Use variables instead of hardcoding file paths. Easier to update
$Path            = "\\corp\dfs\Transfer\GAB\*DAY2FileOUT"
$formattedFile   = "\\corp\dfs\Transfer\GAB\DAY2FileOUT.xml"

# $x - no need to create a variable you're only going to use once.
# Get-ChildItem already has a Count property - Measure-Object is redundant
if((Get-ChildItem $Path).Count -ne 1){
    Get-ChildItem $Path -Verbose
    Exit 8
}

# avoid $input - see further explanation
$xmlContent = Get-Content $Path
$xmlPart    = [xml] ($xmlContent[1..($xmlContent.count - 2)])

# Thanks to TechSpud.
$xmlString = New-Object System.IO.StringWriter
$writer    = New-Object System.Xml.XmlTextwriter($xmlString)
$writer.Formatting = [System.XML.Formatting]::Indented     # see further explanation

# Save to variable instead of file
$xmlPart.WriteTo($writer)

# Put the pieces together and export the file in one go.
# .. means you are specifying a range. not required if you only want 1 element
# -1 automatically returns the last entry
$xmlContent[0] + "`n" + $xmlString.ToString() + "`n" + $xmlContent[-1] |
   Out-File $formattedFile -Encoding ascii

Exit 0
############## CODE ENDS ##############

进一步说明

修改

`n - new line
`r`n - carriage return, new line.