Batch File Help: determining version to decide action - windows

As our office upgrades to Window 7, I have been tasked to update the loginscript to work with Windows 7. The creators of said script are long gone, and I am not a batch file expert.
What I am trying to do is determine the OS. As I do some network administration duties, I need to be able to log on to a server without running the login script whereas I will need to the login script to run if I log into a Windows XP or Windows 7 computer.
I found I couldn't use the VER command as Windows 7 and Windows Server 2008 return the exact same results.
This is what I have:
if exist %loginscriptdir%\sysinfo.txt goto setver
if not exist %loginscriptdir%\sysinfo.txt wmic os get name /value > %loginscriptdir%\sysinfo.txt
type %loginscriptdir%\sysinfo.txt > %loginscriptdir%\sysinfo1.txt
:setver
set WinVer=Unknown
set errorlevel=0
If %WinVer% == "Unknown" (
findstr /c:"Windows XP Professional" %loginscriptdir%\sysinfo1.txt
if %errorlevel%==1 set WinVer=XP
) else (
findstr /c:"Windows 7 Enterprise" %loginscriptdir%\sysinfo1.txt
if %errorlevel%==1 set WinVer=Win7
)
set result=false
if %WinVer% == "XP" set result=true
if %WinVer% == "Win7" set result=true
if "%result%" == "false" (
goto skipicon1
Throughout the script, I wrote in breaks to find the values. Example:
REM -----
ECHO "%WinVer%"
ECHO "%result%"
ECHO "%errorlevel%"
ECHO Press any key to continue 4.
pause>null
REM -----
The fourth break comes at the end of the script I pasted above. These are the results:
"Unknown"
"false"
"0"
Press any key to continue 4.

Here you go. This is the best way I've found to get the OS accurately from win2kpro-winserver2k10. It also tells if it's 32/64 bit and what sp is installed but you don't have to. Just check %cap% in this example.
#echo off
setlocal
call :GetOS cap bit sp
echo %cap%%bit% (%sp%)
exit /b
:GetOS caption servicepack
setlocal
set arc=%PROCESSOR_ARCHITECTURE%
set key="HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
for /f "tokens=3*" %%a in (
'reg query %key%^|findstr /i ProductName') do set cap=%%a %%b
for /f "tokens=3*" %%a in (
'reg query %key%^|findstr /i CSDVersion') do set sp=%%a %%b
endlocal & set %1=%cap% & set %2=%arc% & set %3=%sp%
exit /b

The issue with the code is the expansion of the %errorlevel% value. Since it is contained within a scope of parentheses, the value will not be updated till after the scope ends. Meaning that %errorlevel% will always equal its value when the scope began. To fix this you would have to use delayed expansion. setlocal enabledelayedexpansion and !errorlevel!. Here is a StackOverflow post about delayed expansion: Enable and Disable Delayed Expansion, what does it do?
You may just want to use the version detection method shown at ss64.com http://ss64.com/nt/ver.html
Here is the example from ss64.com but simplified:
#echo off
setlocal
:: Get windows Version
for /f "tokens=4,5,6 delims=[.] " %%A in ('ver') do (
set "Major=%%~A"
set "Minor=%%~B"
set "Build=%%~C"
)
if "%Major%.%Minor%"=="5.1" goto WinXP
if "%Major%.%Minor%"=="6.1" goto Win7
echo Unsupported Version Detected "%Major%.%Minor%"
goto End
:WinXP
echo Windows XP Detected
goto End
:Win7
echo Windows 7 or Server 2008 Detected
goto End
:End
endlocal
exit /b 0

Related

Creating a shop. Won't subtract money correctly

I expect to be able to buy items in the shop and have it do the correct subtraction. In the code below, you start out with 10 gold pieces, (gp), but whenever option 2 or 4, to spend 5gp or 1gp, is entered, it takes away all 10gp. I know that it's because it doesn't go past the first if %input%== 1 but I don't know how to fix it, I have tried almost everything, including if/else statements, but I may not have been doing them properly.
:shop
cls
echo You see a middle aged man behind the counter
echo of the shop as well as a younger man sweeping the floors.
echo "Hello young travelers. Welcome, is there anything
echo I can help you find?"
:purchase
echo --------------------------------------------------------
echo %name%
echo Gold: %gp%
echo --------------------------------------------------------
echo.
echo 1) Battleaxe 10gp Stats: 1d8(S) Versatile(1d10)
echo 2) Mace 5gp Stats: 1d6(B)
echo 3) L.Crossbow 20gp Stats: 1d8(P) Range 80/320
echo 4) 5 Bolts 1gp Equip with Crossbow
echo 5) Go Back
echo.
set /p input=Enter:
if %input%== 5 goto main
if %input%== 1
if %gp% LSS 10 goto nofunds
set /a gp= %gp% - 10
goto shopcont
if %input%== 2
if %gp% LSS 5 goto nofunds
set /a gp= %gp% - 5
goto shopcont
if %input%== 3
if %gp% LSS 20 goto nofunds
set /a gp= %gp% - 20
goto shopcont
if %input%== 4
if %gp% LSS 1 goto nofunds
set /a gp= %gp% - 1
goto shopcont
goto shop
:nofunds
cls
echo You don't have enough gold to purchase that item.
pause >nul
goto shop
:shopcont
cls
echo Would you like to purchase anything else?
goto purchase
I am still new at this so examples and explanations would be wonderful!
Please do not tell me to use choice.exe instead of Set /P, unless it will fix the actual issue.
In the below example, I have used Set /P under :purchase to satisfy your ill advised stipulation to not use choice.exe, (which I used under :shopcont instead).
:shop
ClS
Echo You see a middle aged man behind the shop counter, as well as a
Echo younger man sweeping the floor.
Echo(
Echo "Welcome young travellers, is there anything I can help you with?"
:purchase
Set "input="
Set "invalid=true"
Echo(
Echo ------------------------------------------------------------------
Echo(%name%
Echo Gold: %gp%
Echo ------------------------------------------------------------------
Echo(
Echo 1. Battleaxe 10gp [Stats: 1d8(S) Versatile(1d10)]
Echo 2. Mace 5gp [Stats: 1d6(B)]
Echo 3. L.Crossbow 20gp [Stats: 1d8(P) Range 80/320]
Echo 4. 5 Bolts 1gp [Equip with Crossbow]
Echo 5. Go Back
Echo(
Set /P "input=Enter: "
For /L %%A In (1,1,5) Do If "%%~A" == "%input:"=%" Set "invalid="
If Defined invalid ClS & GoTo purchase
If %input% Equ 5 GoTo main
If %input% Equ 4 If %gp% GEq 1 Set /A gp -=1 & GoTo shopcont
If %input% Equ 3 If %gp% GEq 20 Set /A gp -=20 & GoTo shopcont
If %input% Equ 2 If %gp% GEq 5 Set /A gp -=5 & GoTo shopcont
If %input% Equ 1 If %gp% GEq 10 Set /A gp -=10 & GoTo shopcont
Echo You do not have enough gold to purchase that item.
:shopcont
"%__AppDir__%choice.exe" /M "Would you like to purchase anything else"
If "%ErrorLevel%"=="1" ClS & GoTo purchase
Please note that I have tried to replicate that which you posted in your question, this assumes that %gp% and %name% are already defined prior to this code section and that the label :main exists elsewhere in your unposted code.
You asked for examples and explanations, but those are readily available under each command's usage information and via web searches, so I will not be pointlessly including such things.
The usage of command set /P is not recommended for a simple choice menu. A simple typing mistake by user of batch file can easily result in a syntax error on further processing of the batch file detected by Windows command processor resulting in an unexpected exit of batch file execution. A user playing this batch file game by double clicking on it will not be happy on typing for example by mistake " instead of 2 and suddenly the console window is closed because cmd.exe exited batch file processing because of a serious syntax error caused by " and not good coded batch file.
See also:
How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?
Safe number comparison in Windows batch file
However, the main problem is the used syntax on all IF commands. The syntax of command IF can be seen by opening a command prompt, running if /? and reading the output help. if %input%== 1 without a command or a command block starting with ( and ending with matching ) to execute on condition is true on same line results in a syntax error on batch file execution. This can be seen on debugging the batch file.
The indentations have no meaning for cmd.exe regarding to process flow. Windows command processor is not Python. Windows command processor executes one command line respectively command block after the other independent on how many leading spaces or tabs are used to indent the command lines.
See also: How does the Windows Command Interpreter (CMD.EXE) parse scripts?
An arithmetic expression is the string after set /A evaluated by cmd.exe on execution of the batch file. The help output on running set /? explains that within an arithmetic expression it is possible to reference the value of an environment variable by writing just its name without % or ! around variable name. That has two advantages:
If the environment variable does not exist at all, Windows command processor uses value 0 for not existing environment variable. Using %NotExistingVariable% in an arithmetic expression results in a syntax error because of this string is replaced by nothing which usually results in a missing operand error on evaluation of the arithmetic expression.
Environment variables can be modified with arithmetic expressions multiple times in a command block without usage of delayed expansion.
For that reason set /a gp= %gp% - 10 is not a recommended syntax to decrement the environment variable gp by 10. Better is using set /A gp=gp - 10 and best set /A gp-=10.
The DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/ explains that echo. can fail to print an empty line into console window and that echo/ or echo( is better for this task.
A minimal, complete, and verifiable example for this task is following batch file:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "gp=50"
set "name=B1u3Soul"
:Shop
cls
echo You see a middle aged man behind the counter
echo of the shop as well as a younger man sweeping the floors.
echo "Hello young travelers. Welcome, is there anything
echo I can help you find?"
:Purchase
echo --------------------------------------------------------
echo %name%
echo Gold: %gp%
echo --------------------------------------------------------
echo/
echo 1) Battleaxe 10gp Stats: 1d8(S) Versatile(1d10)
echo 2) Mace 5gp Stats: 1d6(B)
echo 3) L.Crossbow 20gp Stats: 1d8(P) Range 80/320
echo 4) 5 Bolts 1gp Equip with Crossbow
echo 5) Go Back
echo/
%SystemRoot%\System32\choice.exe /C 12345 /N /M "Enter: "
if errorlevel 5 goto Main
if errorlevel 4 set "GoldAmount=1" & goto GoldInPurse
if errorlevel 3 set "GoldAmount=20" & goto GoldInPurse
if errorlevel 2 set "GoldAmount=5" & goto GoldInPurse
set "GoldAmount=10"
:GoldInPurse
if %gp% LSS %GoldAmount% goto NoFunds
set /A gp-=GoldAmount
echo/
%SystemRoot%\System32\choice.exe /C YN /N /M "Would you like to purchase anything else [Y/N]? "
cls
if errorlevel 2 goto Main
goto Purchase
:NoFunds
echo/
echo You don't have enough gold to purchase that item.
pause >nul
goto Shop
:Main
endlocal
See also single line with multiple commands using Windows batch file for an explanation of operator & as used in this batch file.
It would be of course possible to use just choice instead of %SystemRoot%\System32\choice.exe. But the usage of full qualified file name (drive + path + file name + file extension) makes the batch file independent on environment defined outside the batch file. For this batch file it does not matter how PATH and PATHEXT is defined on starting the batch file. It depends only on environment variable SystemRoot defined by Windows and which is not modified by applications or users in general.
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.
choice /?
cls /?
echo /?
endlocal /?
goto /?
if /?
pause /?
set /?
setlocal /?
See also:
What are the ERRORLEVEL values set by internal cmd.exe commands?
Which cmd.exe internal commands clear the ERRORLEVEL to 0 upon success?
Why is no string output with 'echo %var%' after using 'set var = text' on command line?
Microsoft article about Using command redirection operators

In a CMD batch file, can I determine if it was run from powershell?

I have a Windows batch file whose purpose is to set some environment variables, e.g.
=== MyFile.cmd ===
SET MyEnvVariable=MyValue
Users can run this prior to doing work that needs the environment variable, e.g.:
C:\> MyFile.cmd
C:\> echo "%MyEnvVariable%" <-- outputs "MyValue"
C:\> ... do work that needs the environment variable
This is roughly equivalent to the "Developer command prompt" shortcuts installed by Visual Studio, which set environment variables needed to run VS utilities.
However if a user happens to have a Powershell prompt open, the environment variable is of course not propagated back to Powershell:
PS C:\> MyFile.cmd
PS C:\> Write-Output "${env:MyEnvVariable}" # Outputs an empty string
This can be confusing for users who switch between CMD and PowerShell.
Is there a way I can detect in my batch file MyFile.cmd that it was called from PowerShell, so that I can, for example, display a warning to the user? This needs to be done without any 3rd party utility.
Your own answer is robust and while it is generally slow due to needing to run a PowerShell process, it can be made significantly faster by optimizing the PowerShell command used to determine the calling shell:
#echo off
setlocal
CALL :GETPARENT PARENT
IF /I "%PARENT%" == "powershell" GOTO :ISPOWERSHELL
IF /I "%PARENT%" == "pwsh" GOTO :ISPOWERSHELL
endlocal
echo Not running from Powershell
SET MyEnvVariable=MyValue
GOTO :EOF
:GETPARENT
SET "PSCMD=$ppid=$pid;while($i++ -lt 3 -and ($ppid=(Get-CimInstance Win32_Process -Filter ('ProcessID='+$ppid)).ParentProcessId)) {}; (Get-Process -EA Ignore -ID $ppid).Name"
for /f "tokens=*" %%i in ('powershell -noprofile -command "%PSCMD%"') do SET %1=%%i
GOTO :EOF
:ISPOWERSHELL
echo. >&2
echo ERROR: This batch file may not be run from a PowerShell prompt >&2
echo. >&2
exit /b 1
On my machine, this runs about 3 - 4 times faster (YMMV) - but still takes almost 1 second.
Note that I've added a check for process name pwsh as well, so as to make the solution work with PowerShell Core too.
Much faster alternative - though less robust:
The solution below relies on the following assumption, which is true in a default installation:
Only a system environment variable named PSModulePath is persistently defined in the registry (not also a user-specific one).
The solution relies on detecting the presence of a user-specific path in PSModulePath, which PowerShell automatically adds when it starts.
#echo off
echo %PSModulePath% | findstr %USERPROFILE% >NUL
IF %ERRORLEVEL% EQU 0 goto :ISPOWERSHELL
echo Not running from Powershell
SET MyEnvVariable=MyValue
GOTO :EOF
:ISPOWERSHELL
echo. >&2
echo ERROR: This batch file may not be run from a PowerShell prompt >&2
echo. >&2
exit /b 1
Alternative approach for launching a new cmd.exe console window on demand:
Building on the previous approach, the following variant simply re-invokes the batch file in a new cmd.exe window on detecting that it is being run from PowerShell.
This is not only more convenient for the user, it also mitigates the problem of the solutions above yielding false positives: When run from an interactive cmd.exe session that was launched from PowerShell, the above solutions will refuse to run, even though they should, as PetSerAl points out.
While the solution below also doesn't detect this case per se, it still opens a useable - albeit new - window with the environment variables set.
#echo off
REM # Unless already being reinvoked via cmd.exe, see if the batch
REM # file is being run from PowerShell.
IF NOT %1.==_isNew. echo %PSModulePath% | findstr %USERPROFILE% >NUL
REM # If so, RE-INVOKE this batch file in a NEW cmd.exe console WINDOW.
IF NOT %1.==_isNew. IF %ERRORLEVEL% EQU 0 start "With Environment" "%~f0" _isNew & goto :EOF
echo Running from cmd.exe, setting environment variables...
REM # Set environment variables.
SET MyEnvVariable=MyValue
REM # If the batch file had to be reinvoked because it was run from PowerShell,
REM # but you want the user to retain the PowerShell experience,
REM # restart PowerShell now, after definining the env. variables.
IF %1.==_isNew. powershell.exe
GOTO :EOF
After setting all environment variables, note how the last IF statement, also re-invokes PowerShell, but in the same new window, based on the assumption that the calling user prefers working in PowerShell.
The new PowerShell session will then see newly defined environment variables, though note that you'll need two successive exit calls to close the window.
As Joe Cocker used to say "I get by with a little help from my friends".
In this case from Lieven Keersmaekers, whose comments led me to the following solution:
#echo off
setlocal
CALL :GETPARENT PARENT
IF /I "%PARENT%" == "powershell.exe" GOTO :ISPOWERSHELL
endlocal
echo Not running from Powershell
SET MyEnvVariable=MyValue
GOTO :EOF
:GETPARENT
SET CMD=$processes = gwmi win32_process; $me = $processes ^| where {$_.ProcessId -eq $pid}; $parent = $processes ^| where {$_.ProcessId -eq $me.ParentProcessId} ; $grandParent = $processes ^| where {$_.ProcessId -eq $parent.ParentProcessId}; $greatGrandParent = $processes ^| where {$_.ProcessId -eq $grandParent.ParentProcessId}; Write-Output $greatGrandParent.Name
for /f "tokens=*" %%i in ('powershell -command "%CMD%"') do SET %1=%%i
GOTO :EOF
:ISPOWERSHELL
echo.
echo ERROR: This batch file may not be run from a PowerShell prompt
echo.
cmd /c "exit 1"
GOTO :EOF
I did something like this for Chocolatey's RefreshEnv.cmd script: Make refreshenv.bat error if powershell.exe is being used.
My solution didn't end being used, for unrelated reasons, but it's available in this repo: beatcracker/detect-batch-subshell. Here is copy of it, just in case.
Script that will only run if called directly from interactive command processor session
Script will detect if it's run from non-interactive session (cmd.exe /c detect-batch-subshell.cmd) and show approriate error message.
Non-interactive shell includes PowerShell/PowerShell ISE, Explorer, etc... Basically anything that will try to execute script by running it in the separate cmd.exe instance.
Hovewer, dropping into the cmd.exe session from PowerShell/PowerShell ISE and executing script there will work.
Dependencies
wmic.exe - comes with Windows XP Professional and up.
Example:
Open cmd.exe
Type detect-batch-subshell.cmd
Output:
> detect-batch-subshell.cmd
Running interactively in cmd.exe session.
Example:
Open powershell.exe
Type detect-batch-subshell.cmd
Output:
PS > detect-batch-subshell.cmd
detect-batch-subshell.cmd only works if run directly from cmd.exe!
Code
detect-batch-subshell.cmd
#echo off
setlocal EnableDelayedExpansion
:: Dequote path to command processor and this script path
set ScriptPath=%~0
set CmdPath=%COMSPEC:"=%
:: Get command processor filename and filename with extension
for %%c in (!CmdPath!) do (
set CmdExeName=%%~nxc
set CmdName=%%~nc
)
:: Get this process' PID
:: Adapted from: http://www.dostips.com/forum/viewtopic.php?p=22675#p22675
set "uid="
for /l %%i in (1 1 128) do (
set /a "bit=!random!&1"
set "uid=!uid!!bit!"
)
for /f "tokens=2 delims==" %%i in (
'wmic Process WHERE "Name='!CmdExeName!' AND CommandLine LIKE '%%!uid!%%'" GET ParentProcessID /value'
) do (
rem Get commandline of parent
for /f "tokens=1,2,*" %%j in (
'wmic Process WHERE "Handle='%%i'" GET CommandLine /value'
) do (
rem Strip extra CR's from wmic output
rem http://www.dostips.com/forum/viewtopic.php?t=4266
for /f "delims=" %%x in ("%%l") do (
rem Dequote path to batch file, if any (3rd argument)
set ParentScriptPath=%%x
set ParentScriptPath=!ParentScriptPath:"=!
)
rem Get parent process path
for /f "tokens=2 delims==" %%y in ("%%j") do (
rem Dequote parent path
set ParentPath=%%y
set ParentPath=!ParentPath:"=!
rem Handle different invocations: C:\Windows\system32\cmd.exe , cmd.exe , cmd
for %%p in (!CmdPath! !CmdExeName! !CmdName!) do (
if !ParentPath!==%%p set IsCmdParent=1
)
rem Check if we're running in cmd.exe with /c switch and this script path as argument
if !IsCmdParent!==1 if %%k==/c if "!ParentScriptPath!"=="%ScriptPath%" set IsExternal=1
)
)
)
if !IsExternal!==1 (
echo %~nx0 only works if run directly from !CmdExeName!^^!
exit 1
) else (
echo Running interactively in !CmdExeName! session.
)
endlocal
Like the answer from beatcracker I think it would be better to not take assumptions about the external shell that can be used to launch the batch script, for instance, the issue can also arise when running the batch file through the bash shell.
Because it exclusively uses the native facilities of CMD and has no dependency on any external tool or the WMI, the execution time is very fast.
#echo off
call :IsInvokedInternally && (
echo Script is launched from an interactive CMD shell or from another batch script.
) || (
echo Script is invoked by an external App. [PowerShell, BASH, Explorer, CMD /C, ...]
)
exit /b
:IsInvokedInternally
setlocal EnableDelayedExpansion
:: Getting substrings from the special variable CMDCMDLINE,
:: will modify the actual Command Line value of the CMD Process!
:: So it should be saved in to another variable before applying substring operations.
:: Removing consecutive double quotes eg. %systemRoot%\system32\cmd.exe /c ""script.bat""
set "SavedCmdLine=!cmdcmdline!"
set "SavedCmdLine=!SavedCmdLine:""="!"
set /a "DoLoop=1, IsExternal=0"
set "IsCommand="
for %%A in (!SavedCmdLine!) do if defined DoLoop (
if not defined IsCommand (
REM Searching for /C switch, everything after that, is CMD commands
if /i "%%A"=="/C" (
set "IsCommand=1"
) else if /i "%%A"=="/K" (
REM Invoking the script with /K switch creates an interactive CMD session
REM So it will be considered an internal invocatoin
set "DoLoop="
)
) else (
REM Only check the first command token to see if it references this script
set "DoLoop="
REM Turning delayed expansion off to prevent corruption of file paths
REM which may contain the Exclamation Point (!)
REM It is safe to do a SETLOCAL here because the we have disabled the Loop,
REM and the routine will be terminated afterwards.
setlocal DisableDelayedExpansion
if /i "%%~fA"=="%~f0" (
set "IsExternal=1"
) else if /i "%%~fA"=="%~dpn0" (
set "IsExternal=1"
)
)
)
:: A non-zero ErrorLevel means the script is not launched from within CMD.
exit /b %IsExternal%
It checks the command line that used to launch the CMD shell to tell if script have been launched from within CMD or by an external app using the command line signature /C script.bat which is typically used by non CMD shells to launch batch scripts.
If for any reason the external launch detection needs to bypasses, for instance when manually launching the script with additional commands to take advantage the defined variables, it can done by prepending # to the path of the script in CMD command line:
cmd /c #MyScript.bat & AdditionalCommands

I am using a Batch File to search a list of functions for an exact match, wondering if its possible to do a keyword search instead

I am using a Batch File to search a list of functions for an exact match, wondering if its possible to do a key word search, so right now i have to type "open cmd"
(variations on spacing and capital letters are accounted for)
Id like to switch it over to a system that can look for "cmd" and perform the action so "hey, open cmd please" would yield the same result as the old system
Old system:
setlocal
:: /STARTUP
set speech=start scripts\nircmd.exe speak text
cls
:begin
set TALK=TypeSomething
SET /P TALK=
set TALK=%TALK:?=%
call :%TALK: =% 2>NUL
if %errorlevel% equ 0 goto begin
exit /B 0
:unknown
echo Old function no longer supported
:opencmd
:BOSopencmd
:cmd
echo Command Prompt has now been opened in a new window, sir.
%speech% "Command Prompt has now been opened in a new window, sir."
start scripts\cmd.bat
exit /B 0
It is based of a chat bot i tried to make in middle school so the %speech% is not an important item, i can add that and the echo later. I just need a system that works like the old one if possible. The other i can have any number of functions with
:cmd
start cmd
Exit /B 0
or
:reddit
start http://www.reddit.com/
exit /B 0
at these need to be able to stack. I can transition to having scripts for each function in a separate batch files if needed. Ive tried trying findstr but it wasn't giving the desired results. Ive exhausted my knowledge on what i might be able to do but I've come up short lol, If you are having trouble understanding what i'm asking don't hesitate to let me know
I learn by taking things apart so partial code is appreciated but will not be much help until after I've figured out what does what .
Here's a sample of how you might approach it using ECHO, FINDSTR, and CALL (This is a modified example from the original per your request to be able to process multiple keywords):
#echo off
set TST_FNDFLG=FALSE
set TST_USRANS=
set /P TST_USRANS=Enter keywords:
if "%TST_USRANS%" == "" goto ENDIT
echo %TST_USRANS% | findstr /i "CMD" >NUL 2>&1
if ERRORLEVEL 1 goto TRYRED
call :DOCMD
:TRYRED
echo %TST_USRANS% | findstr /i "REDDIT" >NUL 2>&1
if ERRORLEVEL 1 goto TRYGOO
call :DORED
:TRYGOO
echo %TST_USRANS% | findstr /i "GOOGLE" >NUL 2>&1
if ERRORLEVEL 1 goto TRYEND
call :DOGOO
goto TRYEND
:DOCMD
if [%TST_FNDFLG%] == [FALSE] echo.
echo CMD was found in "%TST_USRANS%"
set TST_FNDFLG=TRUE
goto :EOF
:DORED
if [%TST_FNDFLG%] == [FALSE] echo.
echo REDDIT was found in "%TST_USRANS%"
set TST_FNDFLG=TRUE
goto :EOF
:DOGOO
if [%TST_FNDFLG%] == [FALSE] echo.
echo GOOGLE was found in "%TST_USRANS%"
set TST_FNDFLG=TRUE
goto :EOF
:TRYEND
echo.
if [%TST_FNDFLG%] == [TRUE] echo No more keywords found
if [%TST_FNDFLG%] == [FALSE] echo Did not find any known keywords
goto ENDIT
:ENDIT
echo.
set TST_USRANS=
set TST_FNDFLG=

Why I'm not being able to declare a variable inside a 'if exist' syntax in Batch (Windows) file?

In the following script few things won't work right and i'm having a hard time trying to figure the why, First it gives a 'Null' value to the variable (The .txt file is not empty), second it returns to me that some funcitons are not expected at that moment (Such as'goto' was not expected) so it maybe something with the syntax?
#echo off
set name1=sample1
set name2=sample2
set name3=sample3
set ccount=0
del mes.txt
:WFC
ping localhost -n 2 >nul
if exist mes.txt (
SetLocal EnableDelayedExpansion
set /p cname=<mes.txt
set /a ccount=%ccount%+1
if %cname%==!name%ccount%! goto AllowedList
if %ccount%==20 goto Crasher
goto WFC
I tryed calling echo %cname% in the end of the script and appareantly the var have a "nil" value, plus I also turned the #echo on and haven't found nothing useful to understand the problem.
The weirdest part is that if I take the variable declaration out of the 'if exist statemant', the whole script it works marvelously as in:
#echo off
set name1=sample1
set name2=sample2
set name3=sample3
set ccount=0
del mes.txt
:WFC
ping localhost -n 2 >nul
set /p cname=<mes.txt
if exist mes.txt (
SetLocal EnableDelayedExpansion
set /a ccount=%ccount%+1
if %cname%==!name%ccount%! goto AllowedList
if %ccount%==20 goto Crasher
goto WFC
So, this could be satisfying enough but it's not, there's a second software updating the mes.txt value all the time and I'm assuming that there will be some(not much, but some) cases in the second script where the var cname will get null value and still it will compare a null value with name1, completing excluding sample1 of the allowed lists. Can anyone solve this problem or at least explain the why?
Thankfully to the user MC ND, who awnsered me in the comments, I've managed to solve it, turns out that it was a really simple thing, his awnser was:
Your problem is delayed expansion (the lack of it) when reading %cname% and %ccount% (read here) – MC ND 24 mins ago
What I did to the script to make it work was:
#echo off
set name1=sample1
set name2=sample2
set name3=sample3
set ccount=0
del mes.txt
:WFC
ping localhost -n 2 >nul
set /p cname=<mes.txt
if exist mes.txt (
SetLocal EnableDelayedExpansion
set /a ccount=%ccount%+1
if !cname!==!name%ccount%! goto AllowedList
if !ccount!==20 goto Crasher
goto WFC
My guess is that it should be something more like this
:WFC
Timeout 1 1>Nul
If Not Exist mes.txt GoTo :WFC
SetLocal EnableDelayedExpansion
Set/P cname=<mes.txt
Set/A ccount+=1
If %ccount% Equ 20 GoTo :Crasher
If "!name%ccount%!" Equ "%cname%" GoTo :AllowedList
GoTo :WFC

identify if java cannot be executed in windows batch script

I wrote the below batch script which asks for a JAVA_HOME path if its not present in environment, and then it verifies the java version. but before identifying java version it should also check whether java is present in the path (can be executed) or not. Please help me figure out if java -version can be executed or not and display proper message
#echo off
setlocal
set VERSION5="1.5"
IF "%JAVA_HOME%" == "" (
echo Enter path to JAVA_HOME:
set /p JAVA_HOME=
goto check_java_version
) ELSE (
echo Using %JAVA_HOME%
goto check_java_version
)
:check_java_version
for /f "tokens=3" %%g in ('%JAVA_HOME%\jre\bin\java -version 2^>^&1 ^| findstr /i "version"') do (
set JAVAVER=%%g
)
set JAVAVER=%JAVAVER:"=%
set JAVAVER=%JAVAVER:java version =%
for /f "delims=. tokens=1-3" %%v in ("%JAVAVER%") do (
set VER=%%w
)
if not "%VER%" GEQ "5" goto wrong_version
set JAVA_BIN=%JAVA_HOME%\jre\bin
goto correct_java_version
:correct_java_version
REM echo JAVA Version is ok.
set JAVA_LIB=%cd%/lib
%JAVA_BIN%/java -cp %JAVA_LIB%/csm-runtime-1.0.jar;%JAVA_LIB%/groovy-all-1.8.1.jar;%JAVA_LIB%/commons-beanutils-1.8.3.jar;%JAVA_LIB%/csm-dbutil-1.0.jar;%JAVA_LIB%/commons-exec-1.1.jar;%JAVA_LIB%/log4j-1.2.8.jar;%JAVA_LIB%/commons-cli-1.2.jar -Dlog4j.configuration=com/ABC/csm/log4j.xml -Dendorsed_plugins_dir=./plugins com.ABC.csm.CSMMain %*
goto end_java_version
:wrong_version
echo Current JDK Version %VER%
echo Expected JDK %VERSION5% or greater. Please fix your SSATOP and try again.
goto end_java_version
:no_java
echo No JDK found in %JAVA_HOME%.
goto wrong_version
:end_java_version
endlocal
1 of the examples of invalid condition would be, instead of providing JAVA_HOME i.e., e:\csm\java I gave it as e:\csm\java\jre\bin which in this case should display proper error message that please provide a JAVA_HOME path
To check the existence of a program in the PATH, windows batch provides the ~%PATH: option of the SET command. See HELP CALL or HELP FOR.
Use this piece of code as a starting point.
:ProgInPath
set PROG=%~$PATH:1
goto :eof
and use it like this
call :ProgInPath java.exe
IF "%PROG%" == "" (
echo Java.exe not found
) else (
echo. %PROG%
)
in this example, if java.exe is in the PATH, it echoes its complete filespec.
I used the below snippet and it solved my problem
:check_java_existence
IF EXIST %JAVA_HOME%\jre\bin\java.exe (
echo java exists
) ELSE (
echo java does not exists
)

Resources