【问题标题】:Concat files by passing file name and printing file name in the final output - PowerShell通过在最终输出中传递文件名和打印文件名来连接文件 - PowerShell
【发布时间】:2016-11-09 20:45:07
【问题描述】:

我有一个文件列表,我想通过将文件名作为参数将它们组合到一个文件中。另外,在最后的输出中,我想在合并之前和之后添加一些硬编码文本。例如:在一个文件夹中,我有 5 个标记为 1.txt、2.txt、3.txt、4.txt 和 5.txt 的文件。 我想要的是 1.txt、3.txt 和 5.txt 的内容,文件内容类似于

'1.txt 开始'

然后是1.txt的内容

'1.txt 结束'

'3.txt 开始'

然后是3.txt的内容

'3.txt 结束'

'5.txt 开始'

然后是5.txt的内容

'5.txt 结束'

我是 powershell 世界的新手,任何帮助都会非常有帮助。

注意:在任何给定时间,我都可以合并 n 个文件。在我的问题中,我只提供了一个输出示例。

【问题讨论】:

标签: powershell batch-file


【解决方案1】:

我建议你一个有点笨拙的解决方案,但我认为它会更容易理解:)

$FolderWithFiles = 'C:\Users\YourUser\Desktop\FolderWithFiles\' #You can have many files here
$FilesToMerge = '1.txt', '2.txt', '5.txt'  #List only the ones you need
$OutputFile = 'C:\Users\YourUser\Desktop\FolderWithFiles\Output4.txt' #Set the output file. No need to be existing file'
$i = 0

$FileCollection = Get-ChildItem $FolderWithFiles

foreach($file in  $FileCollection) #Loop trough all files
{
    $i++ #I use it to get the current number of the file

    $CurrentFileName = $file.Name
    $CurrentFilePath = $file.FullName 

    #Check if the files are the one you need
    if($FilesToMerge -contains $CurrentFileName){

        #get their content
       $CurrentContent = Get-Content $CurrentFilePath

       #Create new content
       $NewFileContent = "`r`nFile " + $i + " Starts`r`n" + $CurrentContent + "`r`nFile " + $i + " Ended`r`n " 

       #Append it to a text file 
       $NewFileContent | Out-File -LiteralPath $OutputFile -Append
    }
}

希望对你有帮助。

【讨论】:

  • 我喜欢这种方法,但是在将 #create new content 更改为 #Create new content $NewFileContent = "rPrint " + $CurrentFileName + " Startsrn" + $CurrentContent + "@ 987654324@Print " + Print + " Endsrn " ,它会抛出错误。我想要的是“打印文件名开始”,然后是文件内容,然后是“打印文件名结束”。请帮忙解决
  • 您在行中有错误 - 您使用“打印”而不是 $CurrentFileName 作为结尾。将整行替换为:$NewFileContent = "'r'nPrint " + $CurrentFileName + " Starts'r'n" + $CurrentContent + "'r'nPrint " + $CurrentFileName + " Ends'r'n " - 这对我有用。 :) 但不要使用撇号,而是使用转义符号
  • 是否可以破坏文件的内容。到目前为止,整个内容都在一行中。而且我确实有包含大量内容的文件??
  • 这是您需要的$CurrentContent = Get-Content $CurrentFilePath | out-string 只需添加| out-string :)
【解决方案2】:

我会尝试这样的:

Function Merge-Files {
    [CmdLetBinding()]
    Param (
        [ValidateScript({Test-Path $_ -Type Leaf})]
        [Parameter(Mandatory)]
        [String]$File1,
        [ValidateScript({Test-Path $_ -Type Leaf})]
        [Parameter(Mandatory)]
        [String]$File3,
        [ValidateScript({Test-Path $_ -Type Leaf})]
        [Parameter(Mandatory)]
        [String]$File5,
        [Parameter(Mandatory)]
        [String]$Destination
    )

    $ContentFile1 = Get-Content -LiteralPath $File1
    Write-Verbose "Saved content of '$File1' as '$ContentFile1'"

    $ContentFile3 = Get-Content -LiteralPath $File3
    Write-Verbose "Saved content of '$File3' as '$ContentFile3'"

    $ContentFile5 = Get-Content -LiteralPath $File5
    Write-Verbose "Saved content of '$File5' as '$ContentFile5'"

    $NewContent = @"
'1.txt Starts'
$ContentFile1
'1.txt Ends'

'3.txt Starts'
$ContentFile3
'3.txt Ends'

'5.txt Starts'
$ContentFile5
'5.txt Ends'
"@ # Needs to be against the margin

    $NewContent | Out-File -LiteralPath $Destination -Force| Out-Null
    Write-Verbose "New file '$Destination' saved with content '$NewContent'"

}

$Params = @{
    File1 = 'C:\1.txt'
    File3 = 'C:\3.txt'
    File5 = 'C:\5.txt'
    Destination = 'C:\NewFile.txt'
}

Merge-Files @Params -Verbose
# Same as writing: Merge-Files -File1 'C:\1.txt' -File3 'C:\3.txt' ..

这里使用的一些技术是:

【讨论】:

  • 我可以合并 n 个文件,在这个 sn-p 中,它被硬编码为 3 个。
猜你喜欢
  • 1970-01-01
  • 2021-02-27
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
  • 2021-09-02
  • 2015-08-31
相关资源
最近更新 更多