mysql - 批处理脚本: update database with new sql files

标签 mysql batch-file

所以我尝试创建一个可以执行的批处理文件,以便使用 sql 文件更新多个数据库。我已经创建了第一部分,它在所有数据库中执行 sql 文件。它不是很复杂,但它可以完成工作。我的问题是所有文件总是被执行。

我想要的是检查批处理脚本的执行日期/时间和每个sql脚本的最后修改日期/时间。因此,我只能执行在执行批处理脚本后修改/创建的sql脚本。我非常努力地尝试,但未能编写此功能的脚本。我不太擅长批处理脚本 ^^

谢谢!

脚本

@echo off 

set "mySQLexe=C:\wamp\bin\mysql\mysql5.5.24\bin\mysql.exe"
for /f "tokens=*" %%A in (databases.txt) do ( 
  for %%G in (*.sql) do ( 
      %mySQLexe% -hlocalhost -uroot %%A < %%G 
  )
) 
REM Save execution of batch file 
@echo %date% > executiontime.txt
@echo %time% >> executiontime.txt

最佳答案

试试这个:

@echo off
setlocal enabledelayedexpansion
set "mySQLexe=C:\wamp\bin\mysql\mysql5.5.24\bin\mysql.exe"
for /f "tokens=*" %%A in (databases.txt) do ( 
  for /f "tokens=*" %%G in ('dir /b *.sql') do ( 
    Call :FileModTime "%%G" sTime
    Call :FileModTime "%%~dpf0" bTime
    if  !sTime! GTR !bTime! (
      echo %mySQLexe% -hlocalhost -uroot %%A < %%G 
    )  
  )
) 
exit /b 

:FileModTime  File  [TimeVar]
:: Created by dbenham from dostips    ::
:: Converts current time to epox time ::
setlocal disableDelayedExpansion
for %%F in ("%~1") do set "file=%%~fF"
set "time="
for /f "skip=1 delims=,. tokens=2" %%A in (
  'wmic datafile where "name='%file:\=\\%'" get lastModified /format:csv 2^>nul'
) do set "ts=%%A"
set "ts=%ts%"
if defined ts (
  set /a "yy=10000%ts:~0,4% %% 10000, mm=100%ts:~4,2% %% 100, dd=100%ts:~6,2% %% 100"
  set /a "dd=dd-2472663+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4"
  set /a "ss=(((1%ts:~8,2%*60)+1%ts:~10,2%)*60)+1%ts:~12,2%-366100-%ts:~21,1%((1%ts:~22,3%*60)-60000)"
  set /a "ts=ss+dd*86400"
)
endlocal & if "%~2" neq "" (set "%~2=%ts%") else echo(%ts%

测试完并确保其正常工作后,将其移除。

关于mysql - 批处理脚本: update database with new sql files,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28613814/

相关文章:

php - 关于从数据库中获取 Joomla 文章

php - 使用php从多个表mysql中删除

c# 没有向 PHP 发送 json 请求

batch-file - BAT文件: Open new cmd window and execute a command in there

windows - 文件名通配 Windows 与 Unix

php - 重写 URL,用查询字符串中的标题替换 ID

php - UTF-8贯穿始终

java - 如何在 Windows 批处理文件中获取 Java 程序的退出状态

file - 如何使用批处理文件写入文本文件?

Powershell Putty 连接和自动化任务