Windows Batch Script for 2 Cases - mysql

I use the following .bat script
set varSearch="C:\Users\User1\Desktop\Test-folder\*.crypt8"
for /f %%i in ('dir %varSearch% /B ') do set varSearch= %%i
WhatsAppViewer.exe -decrypt8 %myName% key exp.db
sqlite3.exe exp.db<command.txt
cd C:\xampp\mysql\bin
mysql -u admin -p1234 < query.txt
The basic function is to find a file thats ending with .crypt8, decrypt it, save as csv and import to mysql. Its working correctly
But i need some extra features
Case1
The folder contains more than 1 file, and every file has to be processed, but only once
Case 2
Everyday at least one file gets added. It would be superb if the .bat could be scheduled as a task, and run every night and just process the new added files.
Does anybody has a solution for this?

Case 2
The forfiles command processes groups of files based on date. This does files made today only.
forfiles /d 0 /m *.crypt8 /c "cmd /c echo #fname in #path"
Case 1
Your code has errors, it may work but not under all conditions.
The easist way is to put the sequence of commands in a batchfile for a file (%1) which is passed on command line, and use forfiles to call it.

Related

How to make general_log(mysql) file into daily copy?

Need help on this.
I would like to make my general_log file for mysql from single huge file into a daily log file.
I created a batch file to rename and create anew general log file,
but it still write into the renamed file.
Are there a config in mysql or am I missing a command here, so that the general log will not write on the renamed file / or just create a new general log / or will just write to the new empty file? thanks in advance
btw, batch file is below:
#echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set
dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set stamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
ren C:\wamp\logs\generalmysql.log "generalmysql - %stamp%.log"
#echo off
echo.>"C:\wamp\logs\generalmysql.log"
Screenshot
solved my issue by flushing the logs
call "C:\wamp\bin\mysql\mysql5.7.14\bin\mysqladmin.exe" -u'root' -h localhost flush-logs

How do your check for existing .pid before starting wamp?

I'm looking for a way to be sure it is not already running on some other machine before starting it.
The best way I've figured out is to check for an existing .pid in the MySQL folder.
The easiest thing would be to modify the shortcut but I don't think that's possible afaik.
So I need to do a couple of things.
figure out where MySQL is stored
check for the existence of a .pid file and if found the file throw an error stating so with the name of the file MINUS the extension to give them a hint what machine is be running it.
if not exist then go ahead and start wamp like it normally would with the shortcut.
DONE.
If necessary we can ask the user ONCE where wamp is stored then it's simply at %wamp%\bin\mysql
I tried this script but it doesn't do it properly, it's opening 2 cmd windows and just throwing a pause.
Thanks
Bear
#echo off
cd "C:\google_drive\server\wamp64"
forfiles /p bin\mysql\ /m *.pid /s /c "cmd /c goto error"
start wampmanager.exe
exit
:error
echo I'm sorry, Mysql is already running so I can't start WAMP for you. Shutdown WAMP on #FNAME before running again."
pause
You can check the existence of a file with:
if exist "C:\google_drive\server\wamp64\bin\mysql\*.pid
But per your description, I guess, there may be more than one .pid file, so there is a better choice:
setlocal enabledelayedexpansion
set "running="
for %%a in ("C:\google_drive\server\wamp64\bin\mysql\*.pid") do set "running=!running! %%~na,"
if "%running%" neq "" (
echo I'm sorry, Mysql is already running so I can't start WAMP for you.
echo Shutdown WAMP on: %running% before running again.
pause
goto :eof
)
start wampmanager.exe

Execute batch file with arguments

I'm working on a project that require me to use window task scheduler to execute mysql query, this is the batch file content:
mysql -ufoo -pbar -D %1 < %2
when I tested the batch file via cmd:
task_sheduler.bat dbName pathToSqlFile
I get:
mysql -ufoo -pbar -D dbName 0<pathToSqlFile
I just want to say that its working, my question is what is about the extra space and the 0, where did they came from?
The extra space is between the dbName and 0
I'm using Windows 7 Ultimate Service Pack 1 (x64)
I assume your Batch file does NOT have an #echo off command, so you refer to the echo of commands that appear in the screen when a Batch file is executed. The display of these commands frequently include additional characters that cmd.exe inserts to display exactly the executed commands.
In the case of redirections, <input is a short form of Stdin redirection, and the number of Stdin is zero, so the real redirection is 0<input. The same happens with >output, that is echoed as 1>output. cmd.exe also remove multiple spaces from the original code and insert needed ones in order to clearly show the executed commands.
If you want not to see these command expantions, just insert an #echo off command at beginning of your Batch file.
cmd prefixes all redirection commands by the default handle if none is provided. The handles are defined here. 0<file thus means that we want file to be redirected to standard input. The extra space is there to prevent a command like hi.exe<myfile from being wrongly interpreted as hi.exe0 < myfile

Delete all instances of acad.lsp on C:\ except for

I'm using the following code to delete all instances of Acad.lsp found on my C:\ drive but I want to make one exception, which is C:\Autocad 2010\Support.
How can I achieve this?
del "C:\ICT\acad.lsp" /q /a /s
From a command line you could use the batch file for command (I'm assuming that you're using a Windows command prompt or similar here). This is a powerful command that will let you loop through a set of "things" - with the right options, these "things" may be files.
The following command, when run in the C:\ICT directory, should do what you want:
for /F "usebackq" %a in (`dir /s /b acad.lsp ^| find /v "C:\Autocad 2010\Support"`) do #echo %a
Note that I'm using #echo here so that you can test that the results are as you expect before you change the #echo to del.
If you wanted to put this into a batch file, you should change %a to %%a.
A little explanation on what's happening:
for /F "usebackq" %a in (...) runs the command that is between the back-quotes, and runs the command following the do on each item that results. The command in my example above does a recursive dir for the file acad.lsp, and puts that through the find command to remove the one you want to keep. The remaining files are the ones that you will want to delete.

MySQL load command: is there a wildcard available for the datafile?

I have the following file dumped daily into one of our online directories:
dat-part2-489359-43535-toward.txt
The numbers change each day randomly.
I have the following code to try and LOAD the file:
mysql_query("LOAD DATA LOCAL INFILE 'dat-part2-%-toward.txt'
REPLACE INTO TABLE my_table
FIELDS TERMINATED BY ',' ENCLOSED BY ''
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES") or die(mysql_error());
And of course no luck. What's the best way to do this?
assuming this is a scheduled job, why not check the directory for the most recent file that matches your filename template. Store the name of said file in a variable and then sub the variable into your query. Check out glob()
I would do it via a shell script or windows cmd file. This assumes you created the file with mysqldump or some program that creates a valid sql script.
You can run dir /B in windows commend to get a directory of files. So do a dir /B > input.txt then use Windows scripting to read the input file and for each line, pipe the file in using the mysql client.
# echo off set infile= %%1
for /f "eol= tokens=*
delims= usebackq" %%i in (%infile%)
do ( mysql -u userName --password=pwd < %%i )
It's been a long time since I wrote any windows scripts, but that should give you an idea of an approach.