回复 1# 670422423
请参考Q-04和Q-05把bat文件和ini文件都保存为ANSI编码:
https://mp.weixin.qq.com/s/Koje4FufWxWBs7ioDy_LJA
不保留空行.bat- @echo off
- cd /d "%~dp0"
- setlocal enabledelayedexpansion
- set "FileOld=1.ini"
- set "FileNew=2.ini"
- set "BeginChange="
- (for /f "tokens=1,2 delims==" %%a in ('type "%FileOld%"') do (
- if "%%b" equ "" (
- set "ThisRow=%%a"
- ) else (
- set "ThisRow=%%a=%%b"
- )
- if "%%a" equ "[SERVER_DATA_2]" (
- echo,!ThisRow!
- set "BeginChange=true"
- ) else if "!BeginChange!" equ "true" (
- if "%%a" equ "SERVER_USER" (
- echo,%%a=bbs.bathome.net
- set "ChangeU=true"
- ) else if "%%a" equ "SERVER_PASSWORD" (
- echo,%%a=www.bathome.net
- set "ChangeP=true"
- ) else (
- echo,!ThisRow!
- )
- if "!ChangeU!" equ "true" (
- if "!ChangeP!" equ "true" (
- set "BeginChange=false"
- )
- )
- ) else (
- echo,!ThisRow!
- )
- ))>"%FileNew%"
复制代码 保留空行.bat- @echo off
- cd /d "%~dp0"
- setlocal enabledelayedexpansion
- set "FileOld=1.ini"
- set "FileNew=2.ini"
- set "BeginChange="
- (for /f "tokens=1,2 delims=:" %%i in ('findstr /n .* "%FileOld%"') do (
- if "%%j" equ "" (
- echo,
- ) else (
- for /f "tokens=1,2 delims==" %%a in ("%%j") do (
- if "%%b" equ "" (
- set "ThisRow=%%a"
- ) else (
- set "ThisRow=%%a=%%b"
- )
- if "%%a" equ "[SERVER_DATA_2]" (
- echo,!ThisRow!
- set "BeginChange=true"
- ) else if "!BeginChange!" equ "true" (
- if "%%a" equ "SERVER_USER" (
- echo,%%a=bbs.bathome.net
- set "ChangeU=true"
- ) else if "%%a" equ "SERVER_PASSWORD" (
- echo,%%a=www.bathome.net
- set "ChangeP=true"
- ) else (
- echo,!ThisRow!
- )
- if "!ChangeU!" equ "true" (
- if "!ChangeP!" equ "true" (
- set "BeginChange=false"
- )
- )
- ) else (
- echo,!ThisRow!
- )
- )
- )
- ))>"%FileNew%"
复制代码 批处理读取ini文件内容并修改指定的值
http://bbs.bathome.net/thread-30373-1-1.html
批处理读取ini文件指定小节的内容
http://bbs.bathome.net/thread-57942-1-1.html |