Batch script: update database with new sql files - mysql

So I am trying to create a batch file which I can execute in order to update multiple databases with sql files. I already created a first part which is executing the sql files in all the databases. It is not very elaborate but it does the job. My problem is that all the files are always executed.
What I would like to have is a check between the execution date/time of the batch script and the last modified date/time of every sql scripts. Thanks to this, I can only execute the sql scripts which were modified/created after the execution of the batch script. I tried quite hard but didn't manage to script this functionality. I'm not really good with batch scripts ^^
Thanks !
The Script
#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

Try this:
#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%
Remove the echo once you have tested it and made sure it is working.

Related

BATCH - Remove some characters from filename

I've files with this name:
FirstPart_SecondPart_ThirdPart.zip_FourthPart_FifthPartX.csv
where X is a one-digit number.
It should be renamed via batch scripting as:
FirstPart_SecondPart_ThirdPart.zip_FifthPartX.csv
So I'd like to remove the FourthPart. Please note that all the parts ALWAYS HAVE the same lenght. FirstPart is always 7 digits, SecondPart is always 9 digits...etc...
Here is what I've tried:
ren \*.zip_*FifthPart?.csv *.zip_FifthPart?.csv
But it does not work.
Please any help?
Within the appropriate directory
…at the Command prompt:
For /F "EOL=_Tokens=1-4*Delims=_" %A In ('Dir/B/A-D "*_*_*_*_*.csv"') Do #Ren "%A_%B_%C_%D_%E" "%A_%B_%C_%E"
…in a batch file:
#For /F "EOL=_Tokens=1-4*Delims=_" %%A In ('Dir/B/A-D "*_*_*_*_*.csv"'
) Do #Ren "%%A_%%B_%%C_%%D_%%E" "%%A_%%B_%%C_%%E"
Because you are already certain of the file name format and character numbers I feel that the best approach would be to utilise Where instead of Dir.
For example, in a batch file:
FirstPart is always 7 digits, SecondPart is always 9 digits, ThirdPart is always 6 digits, FourthPart is always 5 digits and FifthPart is always 8 digits.
#For /F "EOL=_Tokens=1-4*Delims=_" %%A In (
'Where .:"???????_?????????_??????_?????_????????.csv"'
) Do #Ren "%%A_%%B_%%C_%%D_%%E" "%%A_%%B_%%C_%%E"
You can use a for /F loop to split and rebuild the file names:
for /F "delims= eol=|" %%F in ('dir /B /A:-D "*_*_*_*_*.csv"') do (
for /F "tokens=1-4* delims=_ eol=_" %%A in ("%%F") do (
ren "%%F" "%%A_%%B_%%C_%%E"
)
)
Given that none of the parts contain underscores (_) on their own and none of them are empty, a single loop is sufficient:
for /F "tokens=1-4* delims=_ eol=_" %%A in ('dir /B /A:-D "*_*_*_*_*.csv"') do (
ren "%%A_%%B_%%C_%%D_%%E" "%%A_%%B_%%C_%%E"
)
Here is an approach with an additional filter for file names using findstr in order to exclude files that do not match the name specifications:
for /F "tokens=1-4* delims=_ eol=_" %%A in ('
dir /B /A:-D "*_*_*_*_*.csv" ^| findstr /I "^[^_][^_]*_[^_][^_]*_[^_][^_]*_[^_][^_]*_[^_].*\.csv$"
') do (
ren "%%A_%%B_%%C_%%D_%%E" "%%A_%%B_%%C_%%E"
)
Two possibilities to solve this:
(1) As all parts always have the same length, you can just use substrings to cut the FourthPart_ part:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /F "tokens=*" %%G IN ('DIR /B *.csv') DO (
SET new_filename=%%G
SET new_filename=!new_filename:~0,35!!new_filename:~46!
ECHO REN "%%G" "!new_filename!"
)
(2) Alternatively, if FourthPart_ is a static string, you might also get away with removing the FourthPart_ using search & replace:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /F "tokens=*" %%G IN ('DIR /B *.csv') DO (
SET new_filename=%%G
SET new_filename=!new_filename:FourthPart_=!
ECHO REN "%%G" "!new_filename!"
)
These batch files will only output the commands to be issued. Remove the ECHO once you've inspected the output and you're confident it does what you want.

batch - modify each csv files in subfolders

I try to make a batch script which update header line of each .csv file in folder (and subfolders).
Since I don't want to really modify source files, I create new files with new headers, same content and new extension ".csv.modified"
The script works fine when I have only one .csv (I just remove the /s) but ignore content of other files when > 1.
Note: I have many subfolders and some of them contains whitespaces.
Any idea ?
#echo off
cls
setlocal enabledelayedexpansion
set HEADERS=header1,header2
for /f "delims=" %%i in ('dir /b /s *.csv') do (
set filename=%%~i
echo !filename!
echo.
set cpt=1
set new_filename=!filename!.modified
#copy nul "!new_filename!"
echo creating !new_filename!
echo %HEADERS%>"!new_filename!"
for /f %%a in (%%~i) do (
set line=%%a
if !cpt! gtr 2 (
echo Y
echo !line!>>"!new_filename!"
) else (
echo N
)
echo !cpt! %%a
set /a cpt=!cpt!+1
)
)
endlocal
This should be all you need to achieve that:
#SET "HEADERS=header1,header2"
#FOR /F "DELIMS=" %%A IN ('DIR/B/S/A-D-S-L *.csv') DO #((ECHO %HEADERS%
MORE +1 "%%~A")>"%%~A.modified")

How to copy files using Set_Monitor function in BATCH file CMD

I need some help with cmd .bat code. I have a machine which makes different tests. Tests results goes to pc in specific folder. Each test is named automatically as stated in a program. Tests are written in .csv format. Problem is, that each time i make a new test, the older test file is owerwritten. At least i have made bat file, which sets monitor at that folder specific file and copy, rename and write it in specific place. I need that with all my test files, now i have just with one..
This is what i have:
#Echo Off
if not DEFINED IS_MINIMIZED set IS_MINIMIZED=1 && start "" /min "%~dpnx0" %* && exit
Set _Delay=3
Set _Monitor="c:\Users\Topas\Desktop\Export\Tacktestorezultatai.csv"
Set _Base=%temp%\BaselineState.dir
Set _Chck=%temp%\ChkState.dir
Set _OS=6
Ver|Findstr /I /c:\Users\Topas\Desktop\Export\Tacktestorezultatai.csv>Nul
If %Errorlevel%==0 Set _OS=5 & Set /A _Delay=_Delay*1000
:_StartMon
Call :_SetBaseline "%_Base%" "%_Monitor%"
:_MonLoop
If %_OS%==5 (Ping 1.0.0.0 -n 1 -w %_Delay%>Nul) Else Timeout %_Delay%>Nul
Call :_SetBaseline "%_Chck%" "%_Monitor%"
FC /A /L "%_Base%" "%_Chck%">Nul
If %ErrorLevel%==0 Goto _MonLoop
::
echo %DATE%
echo %TIME%
set datetimef=%date:~-4%_%time:~0,2%_%time:~3,2%_%time:~6,2%
echo %datetimef%
xcopy /s/z "c:\Users\Topas\Desktop\Export\" "c:\Users\Topas\Desktop\galas"
setlocal EnableDelayedExpansion
for %%f in (Tacktestorezultatai.csv) do (
set i=0
for /F "delims=" %%l in (%%f) do (
set /A i+=1
set line!i!=%%l
)
echo %%f, >> bendras.csv
echo !line2!>> bendras.csv
echo !line3!>> bendras.csv
)
ren "c:\Users\Topas\Desktop\galas\" "Tacktestorezultatai_%datetimef%.csv"
::
Echo.Change Detected
Goto :_StartMon
:::::::::::::::::::::::::::::::::::::::::::::::::::
:: Subroutine
:::::::::::::::::::::::::::::::::::::::::::::::::::
:_SetBaseline
If Exist "%temp%\tempfmstate.dir" Del "%temp%\tempfmstate.dir"
For /F "Tokens=* Delims=" %%I In ('Dir /S "%~2"') Do (
Set _Last=%%I
>>"%temp%\tempfmstate.dir" Echo.%%I
)
>"%~1" Findstr /V /C:"%_Last%" "%temp%\tempfmstate.dir"
Goto :EOF
exit
Tacktestorezultatai.csv is one of the files, which are exported after making a test. There are more files, for example 90laipsniurezultatai.csv ; Trintiestestorezultatai.csv . I want to make the same to these files.

Merge CSV Files (Without Duplicates) in Batch

I want to merge two similar CSV files using batch. I have found a file that was working perfectly and now is not. This may have been due to renaming the CSV files that were used.
I want what is demonstrated below:
File 1:
name1,group1,data1
name2,group2,data2
name3,group3,data3
File 2:
name1,group1,data1,time1
name2,group2,data2,time2
Merged file:
name1,group1,data1,time1
name2,group2,data2,time2
name3,group3,data3
(Note that the fourth column was not filled in by name3 and was subsequently not on file 2.)
The following code was modified from: http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_27694997.html.
#echo off
set "tmpfile=%temp%\importlist.tmp"
set "csvfile=importlist.csv"
copy nul "%tmpfile%" >nul
echo.
echo Processing all CSV files...
set "header="
for %%a in (%1) do (
if not "%%a"=="%csvfile%" (
set /p =Processing %%a...<nul
for /f "tokens=1* usebackq delims=," %%b in ("%%a") do (
if /i "%%b"=="Keyword" (
if not defined header (
set /p =Found header...<nul
set "header=%%b,%%c"
)
) else (
title [%%a] - %%b,%%c
findstr /b /c:"%%b" /i "%tmpfile%">nul || echo %%b,%%c>>"%tmpfile%"
)
)
echo OK
)
)
echo Finished processing all CSV files
echo.
echo Creating %csvfile%
echo %header%>"%csvfile%"
set /p =Sorting data...<nul
sort "%tmpfile%">>"%csvfile%"
echo OK
del "%tmpfile%"
echo Finished!
title Command Prompt
exit /b
The problem is that when executed it just creates a sorted CSV with all the data from the first file and not the second.
I have attempted to get it working by putting quotation marks around the parameter (%1 - "directory*.csv") to no avail.
you might try this
#echo off &setlocal disabledelayedexpansion
for /f "delims=" %%a in (file1.csv) do set "#%%~a=7"
for /f "tokens=1-4delims=," %%a in (file2.csv) do (
set "lx1=#%%~a,%%~b,%%~c"
setlocal enabledelayedexpansion
if defined !lx1! (
endlocal
set "#%%~a,%%~b,%%~c="
) else (
endlocal
)
set "#%%~a,%%~b,%%~c,%%~d=4"
)
(for /f "delims==#" %%a in ('set #') do echo %%~a)>merge.csv
type merge.csv
It doesn't work, if you have = or # in your data.

How to put data into mysql database from .txt file using batch script?

I have a database on my localhost, and I need to put data into MySQL database using batch script. Is this possible? Those values are in .txt files
But this doesn't work.
Here is my whole script:
#echo on
setlocal ENABLEDELAYEDEXPANSION
set vidx=0
for /f "tokens=*" %%A in (C:\Users\Diana\Desktop\names.txt) do (
SET /A vidx=!vidx! + 1
snmpwalk -v 2c -c root %%A .1.3.6.1.2.1.25.3.3.1.2 > C:\Users\Diana\Desktop\cpu.txt
snmpwalk -v 2c -c root %%A .1.3.6.1.2.1.25.5.1.1.2 > C:\Users\Diana\Desktop\ramvid.txt
snmpwalk -v 2c -c root %%A .1.3.6.1.2.1.25.2.2 > C:\Users\Diana\Desktop\ram.txt
)
for /f "tokens=4" %%B IN (C:\Users\Diana\Desktop\ram.txt) DO echo %%B >> C:\Users\Diana\Desktop\ramfiltruotas.txt
for /f "tokens=4" %%B IN (C:\Users\Diana\Desktop\cpu.txt) DO echo %%B >> C:\Users\Diana\Desktop\cpufiltruotas.txt
for /f "tokens=4" %%a in (C:\Users\Diana\Desktop\ramvid.txt) do set /a used+=%%a
echo !used! > C:\Users\Diana\Desktop\naujas.txt
for /f "tokens=4" %%c in (C:\Users\Diana\Desktop\ram.txt) do set /a total=%%c
set /a ramai=(%used%*100)/%total%
echo %ramai% > C:\Users\Diana\Desktop\naujas2.txt
for /f "tokens=4" %%d in (C:\Users\Diana\Desktop\cpu.txt) do set /a e+=%%d
set /a cpu=(%e%/4)
echo %cpu% > C:\Users\Diana\Desktop\naujas3.txt
mysql --host=localhost --user=root --password= --database=database <
INSERT INTO server (cpu, ram)
WHERE ('server_name' LIKE '192.168.95.139'
VALUES (%cpu%,%ramai%)
I just woke up and didn't have coffee yet, but... the way I see it the where clause in your insert statement makes no sense. Try:
INSERT INTO server (cpu, ram , server_name )
VALUES (%cpu%,%ramai%,'192.168.95.139')
instead. This assumes that %cpu% and %ramai% will be replaced with the correct values and that cpu and ram are numeric types in the database. If these are string types, add single quotes, eg ... VALUES('%cpu%','%ramai%','192.168.23.139')
if you want to update existing rows instead of inserting new ones, it would be something like
UPDATE server
SET cpu=%cpu%, ram=%ramai%
WHERE server_name = '192.168.95.139'