1

I am new to Batch script, just started understanding. I couldn't find right syntax for navigate up one directory.

current path=C:\Users\myname\gr\firstClone\gr\src\gui\prod

I want to move up to 'C:\Users\myname\gr\firstClone\gr\src\gui' so I can go to build folder where my build file resides

I have this in my batch file

for %%i in (.) do set CURRENT_PATH=%%~fi
set PROJECT_REL=%CURRENT_PATH%
set PROJECT_ROOT=%CURRENT_PATH%\..
set JAVA_HOME=%GCTI_JDK_1_8_0%
if ("%JAVA_HOME%")==() goto invalid_java_home

With below script i try to navigate up one folder

expected: C:\Users\myname\gr\firstClone\gr\src\gui\build\build_brgui.xml

Actual: "C:\Users\myname\gr\firstClone\gr\src\gui\prod\..\build\build_brgui.xml" The system cannot find the path specified.

but I get above error as navigation one level up is not happening.

set BUILDFILE="%PROJECT_REL%\..\build\build_brgui.xml"

My OS: Windows 10

Appreciate your help on this.

Priya
  • 143
  • 4
  • 17
  • 2
    have you tried `for %%i in (..) do ...`? Or `for %%i in (.) do (set "Project_rel=%%~fi"&set "Project_root=%%~dpi"` (attention: trailing backslash)? – Stephan Jul 29 '21 at 08:28
  • 1
    `for %%i (..\build\build_brgui.xml) do set "BUILDFILE=%%~fi"` defines the environment variable `BUILDFILE` with the full path of parent directory of current directory concatenated with folder name `build` and file name `build_brgui.xml` independent if there is really the file `%UserProfile%\gr\firstClone\gr\src\gui\build\build_brgui.xml` on current directory being `%UserProfile%\gr\firstClone\gr\src\gui\prod`. It would be also possible to use `for %%i ("%~dp0..\build\build_brgui.xml") do set "BUILDFILE=%%~fi"` to define the variable with path relative to the batch file path. – Mofi Jul 29 '21 at 09:34
  • I recommend to read [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) It explains in full details the difference between `set "BUILDFILE=%%~fi"` (recommended) and `set BUILDFILE="%%~fi"`, especially on using the string value assigned to an environment variable later with concatenation of other strings or environment variable values. – Mofi Jul 29 '21 at 09:37
  • 1
    To get the full path to the current working directory use `%CD%`. Anyway, `C:\Users\myname\gr\firstClone\gr\src\gui\build\build_brgui.xml` and `C:\Users\myname\gr\firstClone\gr\src\gui\prod\..\build\build_brgui.xml` are equivalent. What line of code is actually throwing the error? You should in general use the syntax `set "VAR=Value"` so assign a variable, so the quotes are not part of the value, which is advantageous in case you want to concatenate strings. And `if ("%JAVA_HOME%")==() goto invalid_java_home` is wrong, it should read `if "%JAVA_HOME%"=="" goto invalid_java_home`… – aschipfl Jul 29 '21 at 12:58

0 Answers0