当前位置: 移动技术网 > IT编程>数据库>Mysql > bat批处理脚本替换文件里的字符串

bat批处理脚本替换文件里的字符串

2020年07月30日  | 移动技术网IT编程  | 我要评论

项目中有一个需求,就是MySQL可以被安装到用户指定的任意目录,但my.ini中有basedir和datadir需要配置到指定的路径,所以,需要写一个bat脚本来替换这个路径,此脚本可保留文件中的空行,代码如下,可作为参考:

@echo off
echo ***** 设置MySQL路径 *****
set appPath=%~dp0
set strFilePath=%appPath%\my-default.ini
set dstFilePath=%appPath%\my.ini
set source_dir=D:\MySQL\
set dest_dir=%appPath%

setlocal EnableDelayedExpansion
for /f "tokens=1* delims=:" %%i in ('findstr /n .* "%strFilePath%"') do (
	set "line=%%j"
	if not "!line!"=="" set "line=!line:%source_dir%=%dest_dir%!"
	echo.!line!>>%dstFilePath%
)

%dest_dir%bin\mysqld.exe --install MySQL
net start MySQL

最后两行是将mysql加入windows系统服务,并启动系统服务的代码。如果要停止服务,然后从系统服务删除,代码如下:

net stop MySQL
sc delete MySQL

本文地址:https://blog.csdn.net/canlynetsky/article/details/107628829

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网