Create Bat file from a bat file in a specified format - batch-file

Can someone please help me out?
Currently, I've got this:
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "IPv4"') do set ip=%%b
set ip=%ip:~1%
Now, I need to create a abcd.bat file (from this .bat file) that will contain a line in below format:
abcy.exe -c %ip% -i2 -u -b 45M -p5600 -t 200 -B 1.2.3.4

Here's an option:
#Set "ts=#abcy.exe -c%%ip%% -i2 -u -b 45M -p5600 -t 200 -B 1.2.3.4"
#For /F "Tokens=2Delims=:" %%A In ('IPConfig^|Find "IPv4"') Do #Set "ip=%%A"
#(Call Echo %ts%)>"abcd.bat"
I've assigned your output to a variable, %ts%, on the first line so that you can easily adjust the output text string, (just leave %%ip%%, which has a leading space, alone).

Add this to your snippet:
echo abcy.exe -c %ip% -i2 -u -b 45M -p5600 -t 200 -B 1.2.3.4 > new.bat

Related

Store curl command output in a variable in batch script

I tried many examples online, but was not able to make it work. I am new to this so please forgive my primitive question.
say I have this:
curl "www.google.com" -o /dev/null -s -w "%{http_code}\n"
when running this in a cmd window, it returns the response code.
200
which is as expected.
I would like to do something like this in a batch script, but also use the output of the curl command to set a variable.
All examples I found online propose:
set res = $(curl "www.google.com" -o /dev/null -s -w "%{http_code}\n")
echo %res%
pause
or like this
FOR /F "tokens=* USEBACKQ" %%F IN (`curl www.google.com -o /dev/null -s -w "%{http_code}\n"`) DO ( SET var=%%F )
ECHO %var%
pause
both did not work, my batch script closes immediately.
Any feedback or help is highly appreciated
thank you.
Thanks to #Compo, here is the formatted answer for other's benefit.
#echo off
For /F %%G In ('%__AppDir__%curl.exe -s -o NUL "www.google.com" -w "%%{http_code}\n"') Do Set "response=%%G"
echo response code is %response%
IF %response% == 200 (
ECHO was able to ping google
) ELSE (
ECHO unable to ping google
)
pause
As he has mentionned above #Compo in his comment :
"To get the result of the command as a variable at the Command Prompt, you'd use For /F"
#echo off
Title Store curl command output in a variable in batch script
Set "MyCommand=curl "www.google.com" -o NUL -s -w "%%{http_code}""
#for /f %%R in ('%MyCommand%') do ( Set VAR=%%R )
echo %VAR%
pause
Based upon your own answer code, there's no need to use a for-loop to get the result saved to a variable, and then compare that variable value with a known value. You can pipe the result through findstr to see if it matches your known value instead.
Example batch-file:
#%__AppDir__%curl.exe -s -o NUL "www.google.com" -w "%%{http_code}" ^
| %__AppDir__%findstr.exe /X "200" 1> NUL && (Echo Ping Succeeded
) || Echo Ping Failed
#Pause
It is only really one line, split for readability, so you could do it line this in cmd:
%__AppDir__%curl.exe -s -o NUL "www.google.com" -w "%{http_code}" | %__AppDir__%findstr.exe /X "200" 1> NUL && (Echo Ping Succeeded) || Echo Ping Failed
Or like this as a batch-file:
#(%__AppDir__%curl.exe -s -o NUL "www.google.com" -w "%%{http_code}" | %__AppDir__%findstr.exe /X "200" 1> NUL && (Echo Ping Succeeded) || Echo Ping Failed) & Pause
Other answers are good, but I would not use CURL to just test if we can reach a site:
#echo off
ping google.com -n 1 >nul 2>&1
if %errorlevel% equ 1 (
echo EHHH..NO Google is not down, we are
) else (
echo HURRAH MAN We are OK
)

How could I update values in one batch file through another batch file

In BCP approach I need to get three values(Machine Name, UserName, Password) dynamically from another batch file.
First File is look like:
MACHINE_NAME=IN-L20054
DB_UserName=sa
DB_Password=sa
pause
Second file is look like:
bcp.exe ExportDB.dbo.AddressCountry OUT "C:\Temp\AddressCountry.bcp" -S "MACHINE_NAME" -U "DB_UserName" -P "DB_Password" -n -q
bcp.exe ExportDB.dbo.CI OUT "C:\Temp\CI.bcp" -S "MACHINE_NAME" -U "DB_UserName" -P "DB_Password" -n -q
pause
The need behind this is, if we run these file in another machine, the only thing respective person need to do is to update and run first file with there Machine Name, UserName and Password
I tried solution but did not get succeed.
Could someone help me out here. ?
Strings can be passed from one batch file to another one either via arguments or via environment variables or via a text file.
1. Pass strings via arguments
Main.bat:
call "%~dp0Export.bat" "IN-L20054" "User" "Password"
call "%~dp0Export.bat" "Other Machine" "Other User" "Other Password"
Export.bat:
bcp.exe ExportDB.dbo.AddressCountry OUT "C:\Temp\AddressCountry.bcp" -S "%~1" -U "%~2" -P "%~3" -n -q
bcp.exe ExportDB.dbo.CI OUT "C:\Temp\CI.bcp" -S "%~1" -U "%~2" -P "%~3" -n -q
2. Pass strings via environment variables
Main.bat:
set "MACHINE_NAME=IN-L20054"
set "DB_UserName=User"
set "DB_Password=Password"
call "%~dp0Export.bat"
set "MACHINE_NAME=Other Machine"
set "DB_UserName=Other User"
set "DB_Password=Other Password"
call "%~dp0Export.bat"
Export.bat:
bcp.exe ExportDB.dbo.AddressCountry OUT "C:\Temp\AddressCountry.bcp" -S "%MACHINE_NAME%" -U "%DB_UserName%" -P "%DB_Password%" -n -q
bcp.exe ExportDB.dbo.CI OUT "C:\Temp\CI.bcp" -S "%MACHINE_NAME%" -U "%DB_UserName%" -P "%DB_Password%" -n -q
3. Pass strings via text file
In this example vertical bar | is used as delimiter which would be interpreted as redirection operator on not being escaped with caret character ^. It is of course possible to use any other character as delimiter like ; or , or a tab character.
(
echo IN-L20054^|User^|Password
echo Other Machine^|Other User^|Other Password
) >"%TEMP%\Export.tmp"
call "%~dp0Export.bat"
del "%TEMP%\Export.tmp"
Export.bat:
for /F "usebackq tokens=1-3 delims=|" %%I in ("%TEMP%\Export.tmp") do (
bcp.exe ExportDB.dbo.AddressCountry OUT "C:\Temp\AddressCountry.bcp" -S "%%~I" -U "%%~J" -P "%%~K" -n -q
bcp.exe ExportDB.dbo.CI OUT "C:\Temp\CI.bcp" -S "%%~I" -U "%%~J" -P "%%~K" -n -q
)
4. Both batch files merged together
for %%# in ("IN-L20054|User|Password" "Other Machine|Other User|Other Password") do (
for /F "tokens=1-3 delims=|" %%I in (%%#) do (
bcp.exe ExportDB.dbo.AddressCountry OUT "C:\Temp\AddressCountry.bcp" -S "%%~I" -U "%%~J" -P "%%~K" -n -q
bcp.exe ExportDB.dbo.CI OUT "C:\Temp\CI.bcp" -S "%%~I" -U "%%~J" -P "%%~K" -n
)
)
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
del /?
echo /?
for /?
set /?
See also the Microsoft article about Using command redirection operators.

conditional print in a batch file

I have a very simple batch file that runs a SQL query file and copies the results to a printer. The problem is it prints even if the results of the query is 0 rows. How can I only print if there is data?
cd stock
sqlcmd -i testquery.sql -S localhost -U User -P password -o testresults.txt
copy testresults.txt \\printserver\share
sqlcmd -i ems_update.sql -S localhost -U User -P password
del c:\stock\testresults.txt
exit
Ok, this is what I have now:
cd c:\stock
sqlcmd -i testquery.sql -S localhost -U User -P password -o testresults.txt
#find /c /i "0 rows" "C:\stock\testresults.txt" > NUL
if %ERRORLEVEL% EQU 1 (
pause
) else (
copy c:\stock\testresults.txt \\printserver\share)
pause
sqlcmd -i ems_update.sql -S localhost -U User -P password
I cannot get the copy command to run now. What am I missing??
thanks
Take a file that you know has 0 data.for /f "delims=" %%A in ('type nodata.txt') do set nodata=%%AThenfor /f "delims=" %%A in ('type testresults.txt') do set test_result=%%AThenif not %nodata%==%test_result% copy testresults.txt \printserver\shareThis will only work if the same exact no data file is produced, like no timestamps.or
use find which returns %errorlevel%==1 if it does not find a phrase in file and 0 if it does.
Just an idea - you can check if size of the file is 0. In Bash it would be something like this:
if [ ! -s ./testresults.txt ] ; then
rm ./testresults.txt
# or do something else
else
copy testresults.txt \\printserver\share
fi

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

generate backup file using date and time as filename

How do I do this properly. I'm trying to name the sql file that is being produced by mysqldump into the current date and time. I've already some research in this site and found a code in here: How to get current datetime on Windows command line, in a suitable format for using in a filename?
Tried to mixed it up with my current code and I came up with this one. The file is named into the current date and time but its only a 1kb file and does not produce a .sql file.
It is supposed to be a 7 kb sql file.
#For /f "tokens=2-4 delims=/ " %%a in ('date /t') do #(set mydate=%%c-%%a-%%b)
#For /f "tokens=1-2 delims=/:" %%a in ('time /t') do #(set mytime=%%a%%b)
#echo mydate= %mydate%
#echo mytime= %mytime%
mysqldump -u root -p --add-drop-table --create-options --password= onstor >c:\%mydate%_%mytime%.sql
UPDATE
I don't think there's a problem with the mysqldump command since it works well when I do it this way. The code below just uses the date as its filename.
#For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do #(
Set Month=%%A
Set Day=%%B
Set Year=%%C
)
#echo DAY = %Day%
#echo Month = %Month%
#echo Year = %Year%
mysqldump -u root --add-drop-table --create-options --password= onstor >c:\%Day%-%Month%-%Year%.sql
Please help, thanks.
On Linux, simply put $(date +%Y-%m-%d-%H.%M.%S) to show date and time in the file name, so it looks like:
mysqldump -u <user> -p <database> | bzip2 -c > <backup>$(date +%Y-%m-%d-%H.%M.%S).sql.bz2
(This command also compresses the file using bzip2)
I think the syntax of your mysqldump command is wrong;
mysqldump -u root -p --add-drop-table --create-options --password= onstor
You use both -p and --pasword=, you should only use one option. And there is a space before the password.
Just try to run the mysqldump command on the commandline to see error messages. Alternatively add 2>&1 at the end of the command in the batchfile. Then you would also see error messages in the output file.
mysqldump -u root --add-drop-table --create-options --password=onstor >c:\%mydate%_%mytime%.sql 2>&1
For those who want to get this to work in crontab, in that case it's a bit different:
30 01 * * * /opt/bitnami/mysql/bin/mysqldump -u root --password=MySecretPass database_name | gzip > /path/backup/`date "+\%d-\%m-\%y_\%H:\%M"`.gz

Resources