Board logo

标题: [文本处理] [分享]批处理在文本第三行添加指定内容 [打印本页]

作者: Batcher    时间: 2021-11-6 19:39     标题: [分享]批处理在文本第三行添加指定内容

【问题描述】

Settings.ini内容如下:
  1. [/Script/Engine.GameUserSettings]
  2. bUseDesiredScreenHeight=False
  3. [ScalabilityGroups]
  4. sg.ResolutionQuality=100.000000
  5. sg.ViewDistanceQuality=0
  6. sg.AntiAliasingQuality=0
  7. sg.ShadowQuality=0
  8. sg.PostProcessQuality=0
  9. sg.TextureQuality=0
  10. sg.EffectsQuality=0
  11. sg.FoliageQuality=0
复制代码
需要在第二行下面添加一行:
GraphicsAPI=DX11_Enhanced
作者: Batcher    时间: 2021-11-6 19:40

方案1.bat
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. cd /d "%~dp0"
  4. set "FileOld=Settings.ini"
  5. set "FileNew=Settings_New.ini"
  6. set "n=0"
  7. (for /f "delims=" %%i in ('type "%FileOld%"') do (
  8.     echo,%%i
  9.     set /a n+=1
  10.     if !n! equ 2 (
  11.         goto :next
  12.     )
  13. ))>"%FileNew%"
  14. :next
  15. >>"%FileNew%" echo GraphicsAPI=DX11_Enhanced
  16. >>"%FileNew%" more +2 "%FileOld%"
复制代码

作者: Batcher    时间: 2021-11-6 19:43

方案2.bat
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. cd /d "%~dp0"
  4. set "FileOld=Settings.ini"
  5. set "FileNew=Settings_New.ini"
  6. set "n=0"
  7. (for /f "tokens=1* delims=:" %%i in ('type "%FileOld%" ^| findstr /n .*') do (
  8.     echo,%%j
  9.     set /a n+=1
  10.     if !n! equ 2 (
  11.         echo,GraphicsAPI=DX11_Enhanced
  12.     )
  13. ))>"%FileNew%"
复制代码

作者: Batcher    时间: 2021-11-6 20:20

方案3.bat
  1. @echo off
  2. cd /d "%~dp0"
  3. set "FileOld=Settings.ini"
  4. set "FileNew=Settings_New.ini"
  5. (for /f "tokens=1* delims=:" %%i in ('type "%FileOld%" ^| findstr /n .*') do (
  6.     echo,%%j
  7.     if %%i equ 2 (
  8.         echo,GraphicsAPI=DX11_Enhanced
  9.     )
  10. ))>"%FileNew%"
复制代码

作者: Batcher    时间: 2021-11-6 20:24

方案4 使用命令行工具 sed.exe
http://bcn.bathome.net/s/tool/index.html?key=sed
  1. sed "3iGraphicsAPI=DX11_Enhanced" "Settings.ini" > "Settings_New.ini"
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2