0

I have a batch script placed in the /project/Scripts directory, but it should be executed in /project directory. My task is to design the script in the way it can be executed from /project/script directory, but it behaves as it were placed in /project directory.

How can I do this magic? I'm not experience in batch magic at all.

The script contains a multiline mvn command and I don't want to pollute the root of the mvn project.

AndrasCsanyi
  • 3,943
  • 8
  • 45
  • 77
  • Would `CD ..` at the beginning of batch file be ok? – Renat Jul 09 '19 at 14:25
  • Not really, because you have cd back to the Scripts directory. – AndrasCsanyi Jul 09 '19 at 14:27
  • have you looked into `pushd` and `popd`? otherwise you could just set the path as a variable and use it as needed – mael' Jul 09 '19 at 14:32
  • 5
    1. The Windows path separator is `\ `but not `/`. 2. `%~dp0.` points to the parent directory of the script, `%~dp0..` goes one level up. 3. Use `setlocal` at the beginning of the script (and `endlocal` at its end), then you can do [`cd`](https://ss64.com/nt/cd.html)`/D "%~dp0.."` to change to the target directory temporarily (alternatively use [`pushd`](https://ss64.com/nt/pushd.html) and [`popd`](https://ss64.com/nt/popd.html))... – aschipfl Jul 09 '19 at 14:34
  • @SayusiAndo I recommend reading [this answer](https://stackoverflow.com/a/38676582/3074564) for details about the commands __SETLOCAL__ and __ENDLOCAL__ as well as __PUSHD__ and __POPD__. And read also [What does %~dp0 mean, and how does it work?](https://stackoverflow.com/questions/5034076/) which references drive and path of argument 0 which is the drive and path of current executed batch file always ending with a backslash and so `cd /D "%~dp0..\"` changes the current directory to parent directory of the batch file independent on what is the current directory on starting the batch file. – Mofi Jul 09 '19 at 17:46
  • @SayusiAndo See also [What is the reason for batch file path referenced with %~dp0 sometimes changes on changing directory?](https://stackoverflow.com/questions/12141482/) and read the Microsoft documentation about [Naming Files, Paths, and Namespaces](https://learn.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file). – Mofi Jul 09 '19 at 17:50

0 Answers0