powershell - 将第八行和第九行添加到所有 *.txt 文件

标签 powershell batch-file cmd

我在 C:\myfolder*.txt 中有超过 100 个 txt 文件 当我从“C:\myfolder”运行此脚本时,我可以将第八行和第九行添加到 somename.txt

@echo off
powershell "$f=(Get-Content somename.txt);$f[8]='heretext1';$f | set-content somename.txt"
powershell "$f=(Get-Content somename.txt);$f[9]='heretext2';$f | set-content somename.txt"

但是我如何将第八行和第九行添加到路径 C:\myfolder*.txt 中的所有 *.txt 文件中 有人可以解释一下该怎么做吗...


对不起我的英语,对不起,如果我没有解释我的问题。我现在就试试:

我使用“*.uci”文件,而不是*.txt 文件。我写 txt 是因为大多数人不知道 uci 扩展名。这些 *.uci 文件是使用 uci 协议(protocol)的国际象棋引擎的设置。 因此,当您使用 chessbase 程序时,您有很多国际象棋引擎,每个引擎都会创建其“enginename.uci”文件。 如果您想将 PC 上使用的核心数量从 1 更改为 16,您需要通过在 *.uci 文件中添加以下信息来手动执行此操作,如下所示:

[OPTIONS] 
Threads=1

这就是为什么最好通过一键添加这两行来进行小批量或 ps1 更改所有引擎的设置

最佳答案

也许像这样的 PowerShell 脚本适合您的任务:

Get-ChildItem -Path 'C:\myfolder' -Filter '*.txt' | ForEach-Object {
$LineIndex = 0
$FileContent = Switch -File $_.FullName {Default {
$LineIndex++
If ($LineIndex -Eq 8) {@'
heretext1
heretext2
'@}
$_}}
Set-Content -Path $_.FullName -Value $FileContent}

关于powershell - 将第八行和第九行添加到所有 *.txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74089391/

相关文章:

php - 从 powershell 传递到批处理时丢失部分 url

powershell - 导入证书 - 访问被拒绝(以管理员身份运行时)

variables - 变量内的 Windows 批处理变量

batch-file - 在 Windows 上通过 Cygwin 运行 shell 脚本

windows - Windows中读取文件时批量设置值和清除值

powershell - 警告 : Unable to find module repositories

windows - 如何复制整个目录结构?

postgresql - 如何将 PSQL 命令行拆分为多行?

java - 如何在Windows 8上执行 "jps''命令?

batch-file - 批处理 : Execute command with quotes in for loop with piping to find