Houdini
Houdini

Reputation: 3542

Scripting in Windows Batch

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.

  1. In line 15, what if I dont know the full name of the directory? i.e. what if I just knew 'whistle' was a part of it, and wanted to check if it was == to DIR?
  2. In lines 16 and 19, I am trying to say if I am in 'whistle-1.0.0', I know that the BELFRAMEWORK_HOME variable should be set to 1 directory above it. I try to do this with '%DIR%..\' but this seems to have no effect. How can I set BELFRAMEWORK_HOME to 1 directory above 'whistle-1.0.0'?
  3. In lines 17 and 20, the BELFRAMEWORK_HOME variable is not printed. I do not understand why, because it is of the same form as lines 10 and 12, which echo the variable as expected. Why doesnt BELFRAMEWORK_HOME echo?
  4. Last thing, are there any good online resources for learning more about this type of scripting?

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

Answers (1)

dbenham
dbenham

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

Related Questions