Batch File Resume with Prewritten Arguments - windows

I have two batch files one batch files calls another. The one that calls the other is called install.bat, and the other controller.bat.
The contents of install.bat are as follows:
start controller.bat a.exe b.exe
Then the controller.bat file operates as such:
#echo off
:: Check if the first argument is passed
if "%~1"=="" (
echo "No first argument passed"
) else (
set first_arg=%~1
)
:: Check if the second argument is passed
if "%~2"=="" (
echo "No second argument passed"
) else (
set second_arg=%~2
if not "%~1" == "" (
set current=one
) else(
echo twob >%~dp0current.txt
)
)
call :Resume
goto %current%
goto :eof
:one
::Add script to Run key
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v %~n0 /d %~dpnx0 /f
echo two >%~dp0current.txt
echo -- Section one --
echo "update will start"
start /wait %second_arg%
echo "update done"
pause
shutdown -r -t 0
goto :eof
:two
echo three >%~dp0current.txt
echo -- Section two --
echo "updating now"
Start /wait %first_arg%
echo "Update Finish"
pause
shutdown -r -t 0
goto :eof
:twob
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v %~n0 /d %~dpnx0 /f
echo three >%~dp0current.txt
echo -- Section two --
echo "updating now"
Start /wait %first_arg%
echo "Update Finish"
pause
shutdown -r -t 0
goto :eof
:three
::Remove script from Run key
reg delete HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v %~n0 /f
del %~dp0current.txt
echo -- Section three --
pause
goto :eof
:resume
if exist %~dp0current.txt (
set /p current=<%~dp0current.txt
) else (
set current=one
)
The problem it is not working well it jumps into twob and then straight to three. It is supposed to show the intermediate steps to a user to show the updates are occurring.
I am expecting it to be able to read if one argument is supplied let's say the second and not the first and still do the jumps to right labels. Right now, the labels are a bit mixed up when I supply both args %~1 and %~2.

Related

If statement in batch file - user chooses option

New to batch/sql files. Thanks for you help.
What im trying to do:
user picks option 1, runs my sql file.
user picks 2, exits program.
user enters nothing or invalid option, ECHO "invalid option".
Problem: anything I type will exit my program. What am I doing wrong?
ECHO 1 - Show Report
ECHO 2 - Exit
SET choice=""
SET /P input=Your choice:
IF "%input%"=="1" (
GOTO :sql file
) ELSE (
IF "%input%"=="2" (
GOTO :Exit
)
) ELSE (
IF "%input%"=="" (
ECHO Invalid option chosen.
)
)
PAUSE
Just use the choice command, its instructions are available by entering choice /? at the Command Prompt.
#ECHO OFF
ECHO 1 - Show Report
ECHO 2 - Exit
CHOICE /C 12 /M "Your choice"
IF ERRORLEVEL 2 GOTO :EOF
REM This line is your SQL report code
PAUSE
If you still wanted to use, what in your case I believe is the wrong Set /P input method, then something like this should be relatively robust, given that your end user is free to enter whatever they like as input, mischievous or harmful intent included:
#ECHO OFF
ECHO 1 - Show Report
ECHO 2 - Exit
:GETInput
SET "input="
SET /P "input=Your choice: "
2>&1 SET input | FINDSTR /XR "input=[12]" 1>NUL || (
ECHO Invalid option chosen
GOTO GETInput
)
IF %input% EQU 2 GOTO :EOF
REM This line is your SQL report code
PAUSE
There is one extract closing bracket [Line #12] inside nested if..else. Also, as far as i know, your label :sql file should not contain space and you must declare it inside the batch file (Same for label :Exit)
I did few changes in your code to make it work
#echo off
:Main
cls
ECHO 1 - Show Report
ECHO 2 - Exit
SET choice=""
SET /P input=Your choice:
IF "%input%"=="1" (
GOTO :SQLReport
) ELSE IF "%input%"=="2" (
GOTO :EOF
) ELSE (
ECHO.
ECHO Invalid option chosen.
pause
GOTO :Main
)
:SQLReport
ECHO.
ECHO Running SQL Report...
GOTO :EOF
:EOF

Windows batch file to block multiple hosts (using list)

I want to add multiple lines to the hosts file if they're not existing. My code works great but I want to do it for a list of 30 entries and thats where I don't know how to handle it. I thought about a list and a for-loop but I don't know how.
#ECHO OFF
ECHO Checking administrator right.
OPENFILES >NUL 2>&1
IF %ERRORLEVEL% EQU 0 (
ECHO Administrator right detected.
ECHO.
ECHO Hosts file : %WINDIR%\system32\drivers\etc\hosts
ECHO.
SETLOCAL ENABLEDELAYEDEXPANSION
SET BLOCKLINE=127.0.0.1 example.com
ECHO Checking : !BLOCKLINE!
FIND /C /I "!BLOCKLINE!" "%WINDIR%\system32\drivers\etc\hosts" >NUL 2>NUL
IF !ERRORLEVEL! NEQ 0 (
ECHO Line not found, adding to the hosts file.
ECHO !BLOCKLINE!>>%WINDIR%\system32\drivers\etc\hosts
) ELSE (
ECHO Line found.
)
ECHO.
ENDLOCAL
ECHO Patching is completed.
ECHO Check hosts file if you want to see the result.
ECHO.
) ELSE (
ECHO Administrator right not detected.
ECHO You need admin right to use this hosts patch!
ECHO.
)
PAUSE
A basic for loop will suffice.
Define the lines to test in the loop set
For %%L in (
"127.0.0.1 example1"
"127.0.0.30 example30"
) Do (
ECHO Checking : %%~L
FIND /C /I "%%~L" "%WINDIR%\system32\drivers\etc\hosts" >NUL 2>NUL
IF !ERRORLEVEL! NEQ 0 (
ECHO Line not found, adding to the hosts file.
>>"%WINDIR%\system32\drivers\etc\hosts" ECHO %%~L
) ELSE (
ECHO Line found.
)
)

cmd : getting results and if statement

Hi I am having problems with one of my first CMD scripts, any suggestions on how to go about this loop with grabbing the results outputted the command window with the if statements?
#echo off
:BEGIN
T:
cd js
cd ddf
cd invent
for %%s in ("invstk" "labmas") do (
Call Isutl -r %%s
if result contains "%ISUTL=20: Cannot open index file" goto SendEmail else if the result = "Indexing Complete" continue to next file in
list, if result not contains "%ISUTL=20: Cannot open index file" or "Indexing Complete" then call Isutl -r on that file again
)
:SendEmail
:DONESCAN
Exit
See if this does what you need:
#echo off
:BEGIN
cd /d "t:\js\ddf\invent"
for %%s in ("invstk" "labmas") do call :routine "%%s"
del file.log 2>nul
pause
goto :EOF
:routine
Call Isutl -r "%~1" >file.log 2>&1
find /i "ISUTL=20: Cannot open index file" <file.log >nul && goto SendEmail
find /i "Indexing Complete" <file.log >nul && goto :EOF
goto :routine
:SendEmail
rem send email
goto :EOF

Execute SQL from batch file

I'm new to batch files scripting.
All I want is creating a batch file that calling SQL file and store results in text file .
Can anyone help me, your help is highly appreciated,
This is the first time I need to create such files.
using a batch file:
save and run this:
#echo off
sqlplus -s -l user/pass#yourdb #yoursql.sql>your_log.log
p.s. be sure to have the last line of your sql script as exit; or the batch file will hang.
sqlcmd -S sqlservername -i yoursqlfile.sql -U username -P password -o outputfile.txt
I created a more advanced launcher, try it out (you can find the latest version at http://www.unix.com/windows-and-dos-issues-and-discussions/256021-windowss-batch-launcher-oracle-sql-linux-sh-scripts-available-here.html)
Here's the code anyway:
launcher.cmd
#ECHO OFF
rem Script Launcher by Fr3dY v1.4
rem ##############################
rem Version History:
rem 1.4 - Misc. fixes
rem 1.3 - Merged with 'server launcher', now accepts both SQL and SHELL SCRIPTS
rem 1.2 - Interactive prompt to show the file on screen
rem 1.1 - No need to add 'quit;' or 'exit;' in the .sql file anymore
rem Fixed sqlplus waiting for username/password if the first attempt was unsuccessful
rem Log file is generated automatically, including date and time in name
rem Misc. fixes
rem 1.0 - Initial Version
:MAIN
::Path of PLINK
set PLINK="C:\Program Files (x86)\PuTTY\plink.exe"
::List with TNS NAMES
set dbservers=launcher-databases.txt
::List with LINUX SERVERS
set linuxservers=launcher-servers.txt
set dt=%DATE:~6,4%_%DATE:~3,2%_%DATE:~0,2%__%TIME:~0,2%_%TIME:~3,2%_%TIME:~6,2%
set dt=%dt: =0%
echo Choose launcher mode:
echo 1) Database scripts (.sql files)
echo 2) Shell scripts (.sh files)
set /p launchermode="Insert value: "
echo.
if %launchermode%==1 (
set extension=sql
set servers=%dbservers%
set mode=DB
) else (
if %launchermode%==2 (
set extension=sh
set servers=%linuxservers%
set mode=OS
) else (echo "Incorrect value, exiting..." & goto :END)
)
if exist %servers% (
goto :LISTFILES
) else echo FILE %servers% NOT FOUND, ABORTING & goto :END
:LISTFILES
echo Listing *.%extension% files...
echo.
dir /b *.%extension%
echo.
set /p file=Name of the file to be launched (without extension)?
if exist %file%.%extension% (
goto :CONFIRMSHOW
) else echo FILE %file%.%extension% NOT FOUND, ABORTING & goto :END
:CONFIRMSHOW
echo.
set /p confirm=Show the script %file%.%extension% on screen now?
if %confirm%==y (
goto :SHOWSCRIPT
) else goto :CONFIRMEXEC
echo.
:SHOWSCRIPT
echo.
echo Content of %file%.%extension%
echo ######################
type %file%.%extension%
echo.
echo ######################
echo.
:CONFIRMEXEC
set /p confirm=Are you sure you want to execute this script?
if %confirm%==y (
set /p user=%mode% username?
goto :HInput
) else echo ABORTED & goto :END
echo.
echo Output saved to %file%_%dt%.log
echo.
Goto :END
:HInput
::Hidden.cmd
::Tom Lavedas, 02/05/2013, 02/20/2013
::Carlos, 02/22/2013
::https://groups.google.com/forum/#!topic/alt.msdos.batch.nt/f7mb_f99lYI
::Version 3.0
SetLocal DisableDelayedExpansion
echo.
Echo Enter password:
Set "Line="
Rem Save 0x08 character in BS variable
For /F %%# In (
'"Prompt;$H&For %%# in (1) Do Rem"'
) Do Set "BS=%%#"
:HILoop
Set "Key="
For /F "delims=" %%# In (
'Xcopy /L /W "%~f0" "%~f0" 2^>Nul'
) Do If Not Defined Key Set "Key=%%#"
Set "Key=%Key:~-1%"
SetLocal EnableDelayedExpansion
If Not Defined Key echo. & Goto :HIEnd
If %BS%==^%Key% (Set /P "=%BS% %BS%" <Nul
Set "Key="
If Defined Line Set "Line=!Line:~0,-1!"
) Else Set /P "=*" <Nul
If Not Defined Line (EndLocal &Set "Line=%Key%"
) Else For /F delims^=^ eol^= %%# In (
"!Line!") Do EndLocal &Set "Line=%%#%Key%"
Goto :HILoop
:HIEnd
if %launchermode%==1 (
goto :EXECDB
) else goto :EXECLINUX
:EXECDB
FOR /f %%A IN (%servers%) DO CALL ECHO DATABASE: %%A & ECHO DATABASE: %%A >> %file%_%dt%.log & sqlplus -S -L %user%/!Line!#%%A < %file%.%extension% >> %file%_%dt%.log
goto :END
:EXECLINUX
FOR /f %%A IN (%servers%) DO CALL ECHO SERVER: %%A & ECHO SERVER: %%A >> %file%_%dt%.log & echo y | %PLINK% %user%#%%A -pw !Line! "exit" & %PLINK% %user%#%%A -pw !Line! -batch -m %file%.%extension% >> %file%_%dt%.log & echo. >> %file%_%dt%.log
goto :END
:END
pause

Syntax of the command is incorrect in batch?

The cmd.exe keeps giving me a The syntax of the command is incorrect error when I run the following codes. Whats wrong with it?
code:
#echo off
setlocal enabledelayedexpansion
CALL ant clean build compile
REM loop thru/Run ALL .properties file names from "ExecutionSDKTest_10.2.2" folder
:labelNextTest
FOR %%G in (C:\ExecutionSDKTest_10.2.2\*.properties) DO (
pause
Set fileName=%%~nxG
rem fileName==ENG-822.properties
set testName=!fileName:~0,7!
rem testName==ENG-822
set testNum=!fileName:~4,3!
REM testNum==822
ECHO Please Create: !testName!_Ref.properties.txt in C:\ExecutionSDKTest_10.2.2\Logs
echo Running:!fileName!
java -jar Test.jar !fileName! > Logs\!fileName!.log
set logPath="C:/ExecutionSDKTest_10.2.2/Logs/!fileName!.log"
set refLogPath="C:/ExecutionSDKTest_10.2.2/Logs/!testName!_Ref.properties.txt"
REM if Ref exists check Line by line
if EXIST !refLogPath!(
Call Perl TestProp1RefcheckLines.pl !fileName! !testName!
) else (
rem if ref does NOT exist check one important line
rem returns case number to check for
perl.exe c:\ExecutionSDKTest_10.2.2\TestProp1noRefCases.pl !testNum!
set lineCase=!ERRORLEVEL!
echo linecase is !lineCase!
rem set LineCase to one, two, three or four, depending on case number
FOR /F "tokens=*" %%B in (!logPath!) Do (
set logLine=%%B
rem check line >> : "...job status:..."
if /i "!logLine:~11,33!"=="... STATUS REPORT: ...Job status:"(
if "!lineCase!" =="1"(
if /i "!logline:~32!" == "job status: job finished_error" goto labelPass
)
if "!lineCase!"=="2" (
if /i "!logLine:~32!" == "Job status: job QUEUED" goto labelPass
)
if "!lineCase!"=="4"(
if /i "!logLine:~32!" == "job Execution Finished Successfully" goto labelPass
)
) else (
if /i "!logLine:~11,33!"=="Error initializing the connection" goto labelPass
if /i "!logLine!" == "Exception in thread ""main"" java.lang.IllegalArgumentException: timeout value is negative" goto labelPass
)
)
goto labelFail
)
)
pause
REM Test Passed or Failed
:labelFail
echo !TestName! FAILED due to the above incorrect line
goto labelNextTest
:labelPass
Echo !TestName! PASSED
goto labelNextTest
You're missing a few crucial spaces. In these lines:
if EXIST !refLogPath!(
if /i "!logLine:~11,33!"=="... STATUS REPORT: ...Job status:"(
if "!lineCase!" =="1"(
if "!lineCase!"=="4"(
there must be a space placed before the parenthesis ( !
Yes, batch scripts are very hard to debug. But really, the script could look prettier (indented properly, etc...).

Resources