Batch File to check text files for a string - sql-server

im trying to create a batch file that goes thorugh each text file in a folder and looks for specific words such as "msg" "file" "size" in each line. If those words are found then it sends and me an email.
Im using SQL server to send the email, and im calling the email stored procedure from my batch file like this:
set MYDB= yourDBname
set MYUSER=youruser
set MYPASSWORD=yourpassword
set MYSERVER=yourservername
sqlcmd -S %MYSERVER% -d %MYDB% -U %MYUSER% -P %MYPASSWORD% -h -1 -s "," -W -Q "exec yourstoredprocedure"
I just need help writing the script which checks for specific words in each line in each .txt file

Just give a try for this batch file :
#echo off
Title Search String into text files
Set "ROOT=%~dp0"
set "String2Search=msg size file"
For %%a in (%String2Search%) do (
FOR /f "delims=" %%f IN ('dir /b /s "%ROOT%\*.txt"') DO (
(find /I "%%a" "%%f" >nul 2>&1) && ( Call :FoundString "%%a" "%%f" ) || ( Call :NoFound "%%a" "%%f" )
)
)
pause & exit
::*************
:FoundString
echo found %1 on file %2
goto :eof
::*************
:NoFound
echo no string like %1 found on file %2
goto :eof
::*************

Few suggestions as i cannot comment.I had done a project few years back and i had to do search files with a particular extension and read them to find particular words and do something with them.I remember few things only.I hope it helps you.
To find all txt files:
find /home/user/Downloads/etc -name '*txt'
To read a whole file and search for "msg" "file" "size":
while read -r LINE
do
grep -i "msg" | grep -i "file" | grep -i "size"
to check all in one line
Or you can do it executing one by one without "pipelining".
P.S I don't have linux installed otherwise I would have checked before posting.Sorry if not correct.

This should do the job:
#ECHO OFF
CD C:\wherever\your\.txt\files\are\at
FOR /R %%G IN ("*.txt") DO (FINDSTR /I /C:"msg" /C:"file" /C:"size" "%%G" >nul && GOTO match_found)
GOTO no_match
:match_found
ECHO Match found^!
set MYDB= yourDBname
set MYUSER=youruser
set MYPASSWORD=yourpassword
set MYSERVER=yourservername
sqlcmd -S %MYSERVER% -d %MYDB% -U %MYUSER% -P %MYPASSWORD% -h -1 -s "," -W -Q "exec yourstoredprocedure"
:no_match
ECHO No match found^!
PAUSE
If the batch file is in the same folder as the .txt files you can delete the CD line.
If the search should be case-sensitive remove the /I option.

To satisfy your question as asked, this should suffice:
#Echo Off
FindStr /IR "\<msg\> \<file\> \<size\>" "C:\Users\NT-Hero\*.txt">Nul||Exit /B
Rem Your 'match found' commands here
If you wanted to also search in sub-directories change the FindStr options to /SIR. (See FindStr /? for more options).

Related

Bat file replace character

I use bat script to backup MySQl database
SET SOURCEDIR="C:\Program Files\MariaDB\data\"
set now=%DATE:~-4%-%DATE:~3,2%-%DATE:~0,2%
for /d %%i in (%SOURCEDIR%\*) do (
if not "%%~ni"=="temp" "C:\Program Files\MariaDB\bin\mysqldump.exe" -uroot -h127.0.0.1 -c -e -q --single-transaction=TRUE %%~ni | "c:\Program Files\7-Zip\7z.exe" a -si"%%~ni_%now%.sql" "D:\backup\SQL\%now%\%%~ni.sql.7z"
)
exit
But if database have dot in name, for exapmle data.2023, then MariDB create folder data#002e2023
So SQL dump is empty.
How can I replace #002e to . in this script here *single-transaction=TRUE %%~ni | *?
%%~ni:#002e=!.!% is not working
Based upon your attempt, %%~ni:#002e=!.!%, I'd assume you need to create a variable, and delay variable expansion to use it.
Example:
#Set "SOURCEDIR=%ProgramFiles%\MariaDB\data"
#Set "now=%DATE%"
#Set "now=%now:~-4%-%now:~-7,2%-%now:~-10,2%"
#For /D %%G In ("%SOURCEDIR%\*") Do #If /I Not "%%~nxG" == "temp" (
Set "dirname=%%~nxG"
SetLocal EnableDelayedExpansion
"%ProgramFiles%\MariaDB\bin\mysqldump.exe" -uroot -h127.0.0.1 -c -e -q --single-transaction=TRUE !dirname:#002e=.!^
| "%ProgramFiles%\7-Zip\7z.exe" a -si"%%~nxG_%now%.sql" "D:\backup\SQL\%now%\%%~nxG.sql.7z"
EndLocal)

Saving cURL and jq output to a variable in batch script

I use this command in order to retrieve a result of a build in Jenkins:
curl -s http://<jenkins_url>/job/<job_name>/lastCompletedBuild/api/json | jq ."result"
Based on that result I need to decide an action to be performed using a batch command,
How do I save the output of the command as a variable?
You should be able to use a For /F command in a batch file to return the output of a command to a variable:
For /F "Delims=" %%A In ('"curl -s http://<jenkins_url>/job/<job_name>/lastCompletedBuild/api/json | jq ."result""') Do Set "test=%%~A"
If /I "%test%"=="failed" ...DoSomething
If /I "%test%"=="success" ...DoSomethingElse
If /I "%test%"=="unstable" ...DoAnotherThing
However there may be no need to set a variable at all, because you can work with the returned metavariable directly:
For /F "Delims=" %%A In ('
"curl -s http://<jenkins_url>/job/<job_name>/lastCompletedBuild/api/json | jq ."result""
') Do (
If /I "%%~A"=="failed" ...DoSomething
If /I "%%~A"=="success" ...DoSomethingElse
If /I "%%~A"=="unstable" ...DoAnotherThing
)

Set filename as variable in BATCH

Am new to this so hope someone can assist a novice. Cannot locate the answer so far. Essentially I am trying to create a watch folder which picks up new .MXF files, runs a specific command line application and renames the new file with the original filename plus _new.
#echo off
:loop
if exist "C:\Input\*.mxf" (
for %%a in ("C:\Input\*.mxf") do (
C:\BMX\bmxtranswrap -o C:\Output\NEW.mxf -t as11op1a -y 09:59:50:00 --afd 10 --pass-anc all -anc-max 2048 "%%a"
ping -n 5 localhost >nul
del "%%a"
)
)
ping -n 5 localhost >nul
goto :loop
I originally tried using %~n1 command to pass through the name which works in a CMD script I have.
C:\BMX\bmxtranswrap -o C:\Output\%~n1_NEW.mxf -t as11op1a -y 09:59:50:00 --afd 10 --pass-anc all -anc-max 2048 "%%a"
This doesn't work however. If there is a command I am missing to pass through the source filename then please let me know.
Many thanks.
You need to extract the filename without the extension in order to use it as a variable in the new name.
To get the filename you can use:
%%~ni
This should work:
#echo off
if exist "C:\Input\*.mxf" (
FOR %%i IN ("c:\Input\*.mxf") DO (
REM ECHO %%~ni - This is the filename without the extensoion for the new file
C:\BMX\bmxtranswrap -o C:\Output\%%~ni_new.mxf -t as11op1a -y 09:59:50:00 --afd 10 --pass-anc all -anc-max 2048 %%i
ping -n 5 localhost >nul
del %%i
)
)
NOTE:
I've tested the above code without the bmxtranswrap line and the new filenames were displayed as expected: test_new.mxf and test2_new.mxf

Batch to compare strings from 1st file to remove complete line on 2nd file

I need a batch file to pick each string (1 string = 1 line) in a FileA and search for it in FileB and remove the complete line from this FileB that has this string.
I tried several combinations using:
findstr /vixg:"FileA" "FileB" >"File.new"
type FileA.txt |findstr /v I%%
Do you guys have a solution for that?
Thanks
try something like this:
for /f "tokens=*" %%i in (fileA.txt) do ( type fileB.txt | find "%%~i" /v > fileB.txt )
I hope that these statemets can help you:
#echo off
set /p A="Insert first file path: "
:: FileA
set /p B="Insert second file path: "
:: FileB
for /f "delims=ΒΆ" %%g in (%B%) do (
findstr /c:"%%g" %A% > nul || echo %%g >> TEMP.txt
)
del /P %B%
ren TEMP.txt %B%
pause
However, with these statements it is possible to compare entire lines rather than only parts of a line, but I hope this is anyway what are you looking for.
Likely the shortest solution is to use grep, the right tool for this particular job. Use the -f option to read the patterns from FileA instead of from the command line, and the -v option to negate matches (i.e. print any line that's not matching):
grep -v -f FileA FileB

Using a .bat to extract text between two words

I have a media server and I'm attempting to automate ripping my collection of movies and any future movies using MakeMKV. Ripping and moving is working without a hitch. The problem I'm running into is occasionally MakeMKV doesn't assign a title to the MKVs and I end up with title00.mkv which Media Center Master obviously cannot even begin to try to match to any metadata.
MakeMKV does offer the ability to get the information from the disc which I have print to info.txt which looks like this.
MSG:1005,0,1,"MakeMKV v1.8.10 win(x64-release) started","%1 started","MakeMKV v1.8.10 win(x64-release)"
DRV:0,2,999,1,"HD-DVD-ROM HL-DT-ST BD-RE GGW-H20L YL05","FARFROMHOME_16X9","\\Device\\CdRom0"
DRV:1,256,999,0,"","",""
DRV:2,256,999,0,"","",""
DRV:3,256,999,0,"","",""
DRV:4,256,999,0,"","",""
DRV:5,256,999,0,"","",""
DRV:6,256,999,0,"","",""
DRV:7,256,999,0,"","",""
DRV:8,256,999,0,"","",""
DRV:9,256,999,0,"","",""
DRV:10,256,999,0,"","",""
DRV:11,256,999,0,"","",""
DRV:12,256,999,0,"","",""
DRV:13,256,999,0,"","",""
DRV:14,256,999,0,"","",""
DRV:15,256,999,0,"","",""
FARFROMHOME_16X9 is the label for the disc.
How can I extract this and rename my .mkv when makemkv has finished?
Here is my BAT so far (my first attempt at a .bat):
makemkvcon64 -r info disc > info.txt
makemkvcon64 --minlength=3600 mkv disc:0 all C:\Users\HTPC\MakeMKV_Temp\
START /WAIT makemkvcon64.exe
cd "c:\Program Files (x86)\FreeEject\"
FreeEject d:
move C:\Users\HTPC\MakeMKV_Temp\*.mkv C:\Users\HTPC\Movies\
Renaming the file before the move would be ideal.
Although you posted an ample description of your problem and data, you have not explained what exactly you want as result, so I must guess a couple points. The lines below extract the sixth comma-separated token from the second line of info.txt file:
for /F "skip=1 tokens=6 delims=," %%a in (info.txt) do set "discLabel=%%~a" & goto continue
:continue
echo The label of the disk is: %discLabel%
This is the output of previous lines when they are executed on your example data:
The label of the disk is: FARFROMHOME_16X9
You also have not indicated the name of the file you want to rename (before move it). Below is a possible solution to your problem that should be adjusted when previous unclear points be defined:
makemkvcon64 -r info disc > info.txt
for /F "skip=1 tokens=6 delims=," %%a in (info.txt) do set "discLabel=%%~a" & goto continue
:continue
makemkvcon64 --minlength=3600 mkv disc:0 all C:\Users\HTPC1\MakeMKV_Temp\
START /WAIT makemkvcon64.exe
cd "c:\Program Files (x86)\FreeEject\"
FreeEject d:
ren C:\Users\HTPC1\MakeMKV_Temp\TheNameHere.mkv %discLabel%.mkv
move C:\Users\HTPC1\MakeMKV_Temp\*.mkv C:\Users\HTPC1\Movies\
I've been wrangling with the same thing and created something that works fairly well.
MakeMKV uses it's own output FileName like you indicated "title00.mkv" or something similar.
I've created two(2) batch files to perform my home movie automation.
The first is "RipWorkflow_DVD.bat", which handles all the heavy lifting to call MakeMKV first to rip the content to my drive. Also in this file is a call to Handbrake to convert the MKV to MP4 (my stupid "smart" tv won't play MKV, but likes MP4)
The second batch file "OneStepDVDRip.bat" calls the first, but with parameters I choose for
a) location of the MKV
b) the output name of my MP4
c) the minLength of a track to locate and rip to MKV
Here is the code for "OneStepDVDRip.bat"
call "C:\Users\Todd\Desktop\RipWorkflow_DVD.bat" "Z:\Video\TrueStory" "E:\My Videos\Movies\Drama\True Story (2015).mp4" 3600
Here is the code for "RipWorkflow_DVD.bat"
#echo off
cls
setlocal ENABLEDELAYEDEXPANSION
SET input=%1
SET output=%2
SET minlength=%3
echo
echo %input%
echo %output%
echo
if not exist %input% ( mkdir "%input%" )
call "C:\Program Files (x86)\MakeMKV\makemkvcon64.exe" --minlength=%minlength% --decrypt mkv disc:0 0 "%input%
timeout /t 5 /nobreak
for %%F in (%input%\*.mkv) do (
echo %%~dpnxF
echo %output%
call "C:\Program Files\Handbrake\HandbrakeCLI.exe" -v1 -i %%~dpnxF --main-feature -o %output% -f mp4 --markers -e x264 -b 2000 -a 1 -E faac -6 dpl2 -R 44.1 -B 128 -D 1.75 --subtitle-forced -x ref=2:bframes=2:subme=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0
timeout /t 5 /nobreak
DEL %%F /Q
)
endlocal

Resources