0

I write some .bat files. In them, I need to get the current directory without the remaining "\". I use this kind of script :

@ECHO OFF
SET ROOT_DIR=%~dp0
@ECHO ROOT_DIR IS "%ROOT_DIR%"
REM Remove last "\"
if "%ROOT_DIR:~-1%" == "\" (
  SET ROOT_DIR=%ROOT_DIR:~0,-1%
)
@ECHO ADAPTED ROOT_DIR IS "%ROOT_DIR%"
PAUSE

It generally works. For example, if I put such a file in D:\test, the result is :

ROOT_DIR IS "D:\test\"
ADAPTED ROOT_DIR IS "D:\test"
Appuyez sur une touche pour continuer...

However, for some reason I don't understand, there is a problem when the folder name ends with ")", like "C:\Program Files (x86)". In that folder, it removes the last ")" too, which is a behaviour I don't want :

ROOT_DIR IS "C:\Program Files (x86)\"
ADAPTED ROOT_DIR IS "C:\Program Files (x86"
Appuyez sur une touche pour continuer...

Does anyone have any idea about how to solve that problem, i.e. removing only the last "\" in folder name, whatever the folder name, and without removing the parenthesis ?

Thank you by advance for your answers.

candelrg
  • 19
  • 5
  • The reason can be seen on modification of first line to `@ECHO ON`. The __IF__ condition becomes after processing `if "\" == "\" (SET ROOT_DIR=C:\Program Files (x86 )`. The solution is quite simple. The second line should be changed to `SET "ROOT_DIR=%~dp0"` to work also for batch file directory being `C:\Temp\Development & (Test)!` and the three lines of the __IF__ condition to a single line with `IF "%ROOT_DIR:~-1%" == "\" SET "ROOT_DIR=%ROOT_DIR:~0,-1%"`. See also [Issue 2: Usage of a command block for a single command](https://stackoverflow.com/a/60686543/3074564). – Mofi Sep 29 '21 at 09:35
  • The `)` in directory path is interpreted as end of command block by the Windows command processor `cmd.exe` in your case with using unnecessary a command block and additionally do not enclose the argument string of command __SET__ in double quotes to get all characters inside interpreted literally as characters. See also: [How does the Windows Command Interpreter (CMD.EXE) parse scripts?](https://stackoverflow.com/questions/4094699/) and [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) – Mofi Sep 29 '21 at 09:38
  • In other words common __Issue 1: File/folder argument strings not enclosed in quotes__ in combination of common __Issue 2: Usage of a command block for a single command__ results in the unwanted and for you unexpected behavior. Avoiding at least one of the two common issues (best both) solves the problem and results in correct behavior whereby for directory paths with `&` in path it is necessary to avoid common issue 1 everywhere in the batch file by putting all path strings in double quotes. – Mofi Sep 29 '21 at 09:44
  • 2
    don't use "DOS batch file" because this won't work in DOS. `%~dp0` and `%ROOT_DIR:~-1%` are cmd features which aren't supported at all in DOS. [They're very different things](https://superuser.com/q/451432/241386) – phuclv Sep 29 '21 at 09:51
  • Hint: If the directory is the root directory of a drive, it could be counterproductive to remove the backslash at end, i.e. modify ``D:\`` to just `D:` depending on where `ROOT_DIR` is used next and how it is concatenated with other strings. See the Microsoft documentation about [Naming Files, Paths, and Namespaces](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file) for more details, especially the special type of a relative path like `C:Windows` in comparison to an absolute path like `C:\Windows`. – Mofi Sep 29 '21 at 10:24
  • That is also the reason why `%CD%` expands to a directory path not ending with a backslash, except the current directory is the root directory of a drive in which case `%CD%` expands to drive letter + colon + backslash. – Mofi Sep 29 '21 at 10:27
  • Check out my answer to [Remove trailing backslash from a path with batch](https://stackoverflow.com/a/67095328) to learn how to reliably remove the trailing `\ `from a path without deranging it… – aschipfl Sep 29 '21 at 12:16

1 Answers1

-1

@Mofi had the right answer : I should use quotes around the SET ROOT_DIR instructions. That way, the parenthesis are kept (no interpretation). I tested on various folders and it did work. So thank you very much @Mofi for the solution.

And thank you @phuclv concerning "DOS batch file", I will keep that in mind (I updated the title).

--Edit--

So the right code becomes

@ECHO OFF
SET "ROOT_DIR=%~dp0"
@ECHO ROOT_DIR IS "%ROOT_DIR%"
REM Remove last "\"
if "%ROOT_DIR:~-1%" == "\" (
  SET "ROOT_DIR=%ROOT_DIR:~0,-1%"
)
@ECHO ADAPTED ROOT_DIR IS "%ROOT_DIR%"
PAUSE

This kind of code is a base to then use recursive algorithms that will enter sub-directories (sub-sub-directories and so on) by adding "\" then sub-directory name and do that at any desired depth. So, in my case, it is useful even when the parent directory is directly the drive letter itself.

candelrg
  • 19
  • 5