This batch script returns incorrect value when invoked from a different directory

This batch script returns incorrect value when invoked from a different directory. I am hoping someone can help me fix this so that it always determines the correct APP_HOME directory.

The script is located in a position like:

C:\Temp\MyApplication\bin\runner.bat

And, I want to execute it from:

C:\Temp\OutsideDir\runApp.bat

When I try this, I get 'OutsideDir' but I want it to get 'bin' :

C:\Temp\OutsideDir>C:\Temp\MyApplication\bin\runner.bat
Current directory is: C:\Temp\MyApplication\bin
This folder name: OutsideDir
Function arg must match actual folder name.
This script may not be running from the expected folder.
There was an error.

And here is the script:

@ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION

TITLE Script %~n0%~x0 running from %~dp0

CALL :getparentfolder bin

ECHO My app home is: %APP_HOME%

GOTO :END

:: function to get parent folder name or APP_HOME of scripts currrent folder
:: function requires current folders name as an arg or it will fail to run
:getparentfolder dirName
SET "BIN_HOME=%~dp0"
IF "%BIN_HOME:~-1%"=="\" SET "BIN_HOME=%BIN_HOME:~0,-1%"
ECHO Current directory is: %BIN_HOME%
FOR /F "tokens=1,* delims=^\" %%I IN ("%BIN_HOME%") DO (
  SET THISFOLDER=%%~nxI
)
ECHO This folder name: %THISFOLDER%
IF "%~1"=="%THISFOLDER%" (
  SET APP_HOME=!BIN_HOME:\%THISFOLDER%=!
  ECHO APP_HOME: %APP_HOME%
) ELSE (
  ECHO Function arg must match actual expected folder name.
  ECHO This script may not be running from the expected folder.
  GOTO :ERROR
)
EXIT /B 0

:ERROR
ECHO There was an error.
PING.exe -n 10 -w 1 127.0.0.1>nul
:END
PING.exe -n 10 -w 1 127.0.0.1>nul
ECHO The script %~n0%~x0 is finished.

Answers


try this:

@echo off&setlocal
SET "BIN_HOME=%~dp0"
IF "%BIN_HOME:~-1%"=="\" SET "BIN_HOME=%BIN_HOME:~0,-1%"
ECHO Current directory is: %BIN_HOME%
FOR %%I IN ("%BIN_HOME%") DO (
  SET THISFOLDER=%%~nxI
)
ECHO This folder name: %THISFOLDER%

output is:

C:\TEMP\OutsideDir>C:\Temp\MyApplication\bin\runner.bat
Current directory is: C:\TEMP\MyApplication\bin
This folder name: bin

The scripts own home directory is %~dp0.

Your code is getting the name of the current working directory, hence it's failing because it doesn't match the argument you supply (bin).

SET "BIN_HOME=%~dp0"

Sets BIN_HOME to the path of the directory containing the script (in your case c:\temp\myapplication\bin)

FOR /F "tokens=1,* delims=^\" %%I IN ("%BIN_HOME%") DO (
   SET THISFOLDER=%%~nxI
)

Extracts tokens from the BIN_HOME variable using \ as a delimiter. You have specified you want a maximum of two tokens (tokens=1,*), so these will be allocated to the variables %%I and %%J as follows (assuming %BIN_HOME% is c:\temp\myapplication\bin:

%%I = C:   (everything up to the first backslash)
%%J = temp\myapplication\bin (the remainder of the string

Then attempts to get the file name part of th value %%I (%%~nxI). This gets the name of the current working directory (the path "C:" refers to the current working directory).

I'm not sure exactly what you're trying to achieve, why can't you simply do something like:

SET APP_HOME=%~dp0

UPDATE

I can't do that because %~dp0 refers to the current directory and not the parent of the current directory

If you want the parent of the script's directory, you can do it with something like:

SET MYDIR=%~dp0
echo My directory is %MYDIR%

CALL :GETPARENT PARENT "%MYDIR:~0,-1"

echo Parent is %PARENT%

GOTO :EOF

:GETPARENT
SET %1=%~dp2
GOTO :EOF

Need Your Help

How to perform multiple cell selection in a jtable with mouse click

java swing jtable

I have a simple JTable but how can I add cellselection so that when a user left clicks with the mouse, the cell is selected. When the user selects any other cell in any other row, that cell is also

JSF + Primefaces: Ajax CRUD-Actions in Master-Detail-View only work once per page-load

java hibernate jsf primefaces

I'm using Mojarra 2.0.3 on JBoss AS 6 with Primefaces 2.2.1 + Hibernate.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.