通过在最终输出-PowerShell中传递文件名和打印文件名来压缩文件
我有一个文件列表,我想通过将文件名作为参数传递,将它们合并到一个文件中。另外,在最终输出中,我想在合并前后添加一些硬编码文本。例如:在一个文件夹中,我有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世界的新手,任何帮助都会非常有用通过在最终输出-PowerShell中传递文件名和打印文件名来压缩文件,powershell,batch-file,Powershell,Batch File,我有一个文件列表,我想通过将文件名作为参数传递,将它们合并到一个文件中。另外,在最终输出中,我想在合并前后添加一些硬编码文本。例如:在一个文件夹中,我有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结束
注意:在任何给定的时间,我都可以合并n个文件。在我的问题中,我只提供了一个输出示例。我会尝试以下方法:
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' ..
这里使用的一些技术有:
- 在参数中
- 用于寻址参数
希望有帮助。我喜欢这种方法,但在将“创建新内容”更改为“创建新内容$NewFileContent=“
r
Print”+$CurrentFileName+”开始r
n”+$CurrentContent+“r
Print+”结束r
n”之后,它抛出了错误。我想要的是“打印文件名开始”,然后是文件内容,然后是“打印文件名结束”。如果行中有错误,请提供帮助-您使用“Print”而不是$CurrentFileName作为结尾。将整行替换为:$NewFileContent=“'r'nPrint”+$CurrentFileName+“Starts'r'n”+$CurrentContent+“'r'nPrint”+$CurrentFileName+“Ends'r'n”
-这对我很有用。:)但是使用转义符号代替撇号可能会破坏文件的内容。。到目前为止,整个内容都在一行中。我确实有大量内容的文件??以下是您需要的$CurrentContent=Get content$CurrentFilePath | out string
只需添加| out string
:)我可以合并n个文件,在这个代码段中,它被硬编码为仅3个。当您需要解决方案时,为什么要在问题上加上标签?
$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
}
}