1

我正在输出一些从文件中提取的文本行,并且当我在 $strAcct 之后输出此部分时,正在添加回车:

Add-Content "C:\TestFile-Output.txt" ($strAcct+$strPart2)

所以基本上文件中打印的是 $strAcct回车/换行$strPart2

这是我的所有代码:

#Setting Variables
$data = get-content "C:\TestFile.txt"
$strAcct= @()
$strPart1= @()
$strPart2= @()
$strLength= @()


#For each line of text in variable $data, do the following
foreach($line in $data)
{
  #reseting variables for each time the FOR loop repeats
  $strAcct= @()
  $strPart1= @()
  $strPart2= @()
  $strLength= @()


   #We're saying that if the line of text is over 180 characters, were going to split it up into two different lines so MEDITECH can accept this note files
   if ( $line.length -gt 180)
   { $strLength = $line.length
     $strAcct += $line.substring(0,22) 
     $strPart1 += $line.substring(0,180)
     $strPart2 += $line.substring(181)

     #Create first and second line in text file for the string of text that was over 180 characters
     Add-Content "C:\TestFile-Output.txt" $strPart1
     Add-Content "C:\TestFile-Output.txt" ($strAcct+$strPart2)



   } 
   #If our line of text wasn't over 180 characters, just print it as is
   Else {
   Add-Content "C:\TestFile-Output.txt" $line

   }

}
4

1 回答 1

3

$strAcct $strPart1 $strPart2都是您代码中的数组,我认为这不是您的意图。默认情况下,将字符串数组发送到文件中会将每个项目放在一个新行上(即由 CR-NL 分隔)。

如果您尝试根据代码中的启发式将长行拆分为 2 行,那么下面应该可以工作:

$data = get-content "C:\TestFile.txt"    

#For each line of text in variable $data, do the following
foreach($line in $data)
{   
   $newContent = 
     if ($line.length -gt 180)
     {
       $part1 = $line.substring(0,180)         
       $part2 = $line.substring(181)
       $acct = $line.substring(0,22)

       $part1
       $acct + $part2
     } 
     else
     {
       $line
     }

   Add-Content "C:\TestFile-Output.txt" $newContent
}
于 2012-09-21T19:22:34.707 回答