I'm developing a command line Batch file on Windows 7. I'm trying to add \ to a dir if it hasn't.

This is only a test:

@echo off

set solutionDir=%CD%\

echo %solutionDir%

if [%solutionDir:~-1%] EQU [\] echo ends with \

This line if [%solutionDir:~-1%] EQU [\] echo ends with \ works fine to check if it ends with \ but I want to check if it doesn't end with a \.

I have tried all of these but in all of them I get a syntax error:

  1. if [%solutionDir:~-1%] NEQ [\] echo ends with \
  2. if NOT [%solutionDir:~-1%]==[\] echo ends with \
  3. if [%solutionDir:~-1%]!=[\] echo ends with \

I'm getting current dir and I want to add it \ if it hasn't it.

How can I do it?

  • 1
    have you tried: if NOT [%solutionDir:~-1%] EQU [\] echo does not end with \
    – LPChip
    May 2, 2016 at 8:44
  • Added an answer so you can mark it as solved.
    – LPChip
    May 2, 2016 at 8:48

1 Answer 1


Given that if [%solutionDir:~-1%] EQU [\] echo ends with \ works, the most logical thing to do is add a NOT clause after the if to make it do the opposite.

It would become:

if NOT [%solutionDir:~-1%] EQU [\] echo does not end with \

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.