Reputation: 3542
I have a pretty simple batch file that is basically checking to see if an enviornment variable is set, and then attempting to set it. Its not setting the variable correctly, and I am pretty new to batch files in general so I had some general questions about the script I am writing. Any insight would be appreciated. Here are the questions I have.
Thanks!
1@echo off
2
3if not defined JAVA_OPTS (
4 set JAVA_OPTS=-Xmx1024m -Dderby.stream.error.field=org.openbel.framework.common.enums.DatabaseType.NULL_OUTPUT_STREAM
5) else (
6 set JAVA_OPTS=%JAVA_OPTS% -Dderby.stream.error.field=org.openbel.framework.common.enums.DatabaseType.NULL_OUTPUT_STREAM
7)
8
9set DIR=%CD%
10echo "DIR/CD : %DIR%"
11set DIR=%~dp0
12echo "DIR/~dp0 : %DIR%"
13
14if not defined BELFRAMEWORK_HOME (
15 if "%DIR%"=="whistle-1.0.0" (
16 set BELFRAMEWORK_HOME=%DIR%\..\
17 echo "DIR == whistle, BELFRAMEWORK_HOME is now: %BELFRAMEWORK_HOME%"
18 ) else (
19 set BELFRAMEWORK_HOME=%DIR%\..\
20 echo "DIR != whistle, BELFRAMEWORK_HOME is now: %BELFRAMEWORK_HOME%"
21 )
22)
23
24set WHISTLE_HOME=%~dp0
25java %JAVA_OPTS% -jar %WHISTLE_HOME%\whistle-1.0.0.jar %*
Upvotes: 0
Views: 181
Reputation: 130809
Oooh, 4 questions for the price of one :-)
1) I believe you want something like C:\folder\xxxxWHISTLExxxx\
to match, but not something like C:\xxxxWHISTLExxxx\folder\
. If that is the case then you will want to use FINDSTR with a regular expression to make sure the last directory is what matches WHISTLE. The search must be case insensitive.
You don't want the FINDSTR output. You just want to know if the string is found. It returns 1 if not found (error) and 0 if found (success).
echo %DIR% | findstr /irc:"whistle[^\\]*[\\]*$" >nul && (
rem Code for success (string found)
)
rem Code for failure (string not found)
)
2) If you want the trailing backslash, then you can use
for %%A in ("%DIR%\.") do set "BELFRAMEWORK_HOME=%%~dpA"
If you don't want the trailing backslash, then use
for %%A in ("%DIR%\..") do set "BELFRAMEWORK_HOME=%%~fA"
3) You are experiencing a classic stumbling block for new batch users. The %BELFRAMEWORK_HOME% is expanded when the line is parsed, and the entire IF statement, including the parenthesized code blocks, is parsed as one statement. So the expanded value is the value that existed before the IF statement is executed.
You can solve the problem by using delayed expansion.
@echo off
setlocal enableDelayedExpansion
set var=BEFORE
(
set var=AFTER
echo within block normal expansion = %var%
echo within block delayed expansion = !var!
)
echo after block normal expansion = %var%
The result of the above is
within block normal expansion = BEFORE
within block delayed expansion = AFTER
after block normal expansion = AFTER
4) Here are my favorite batch scripting web sites
Upvotes: 2