0

recently I have been trying to use batch files to do some repetitive work for me, and checking whether a file exist within a current directory is crucial. I have worked with batch files often in the past, but I am now using a new computer, and the old batch files I obtain from dropbox does not work, and I am unsure why. Consider a batch script:

@echo off
echo Build Verification...
if not exist "%cd%\build.bat" GOTO buildFail
echo Success!
pause
exit

:buildFail
echo Building of mod failed!
pause 
exit

build.bat DOES exist, in the current directory, but this is what the cmd gave me:

Build Verification...
The syntax of the command is incorrect.
Building of mod failed!
Press any key to continue...

Baffled at why this would happen, I tried a few variation:

(In case dynamic directory has problems)

@echo off
echo Build Verification...
if not exist "%~dp0\build.bat" GOTO buildFail
echo Success!
pause
exit

:buildFail
echo Building of mod failed!
pause 
exit

But it yield same results

Directly copying the directory wholesale without using %dp~0 or %cd% also yields the same result.

By setting the directory as a variable does not work either:

set randdir = "%cd%\build.bat"
if not exist "%randdir%" GOTO buildFail

But here is the weirdest part: Calling the catch file works

@echo off
echo Build Verification...
call "%cd%\build.bat"

This would result in the activation of build.bat

Please help!

kirin nee
  • 670
  • 5
  • 15
  • 3
    Remove `echo off` which says hide stuff from me. –  Dec 24 '16 at 05:02
  • 2
    The error does not say *current directory does not exist*. It says *The syntax of the command is incorrect*. That's not the same. – Ken White Dec 24 '16 at 05:32
  • 3
    %randir % is not the same as %randir% – Squashman Dec 24 '16 at 06:15
  • 2
    perhaps if you were to show the content of `cd` – Magoo Dec 24 '16 at 07:07
  • 1
    To debug a batch file: Change `@echo off` to `@echo on` or comment it out with `rem @echo off`, remove command `exit` or change it to `exit /B` or comment it out with `rem exit`, open a command prompt window, change with `cd /D "directory path"` to the directory which should be the current directory on running this batch file which of course can be a different directory than the directory containing the batch file, and run now the batch file. You see the commands after preprocessing, the error messages and most important, the console window remains open for studying the output. – Mofi Dec 24 '16 at 10:19
  • 1
    `pause` at end of a batch file is not needed on running a batch file from within a command prompt window if there is no `exit` without `/B` in the batch file. It is in general always better to use `exit` with parameter `/B` if needed at all in a batch file because `exit` without this parameter always exits the command process independent on calling hierarchy and independent on `cmd.exe` was started with parameter `/K` (keep open) as on opening a command prompt window or with parameter `/C` (close) after execution terminated as on double clicking on a batch file. – Mofi Dec 24 '16 at 10:23
  • 1
    One more hint: See answer on [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](http://stackoverflow.com/a/26388460/3074564) for an explanation why `set randdir = "%cd%\build.bat"` and `if not exist "%randdir%" GOTO buildFail` can't work because of the spaces around the equal sign and double usage of double quotes. – Mofi Dec 24 '16 at 10:25
  • I like this one: _"Current directory does not exist"_! **`;)`** – Aacini Dec 24 '16 at 15:49

3 Answers3

2

Heres a small modification:

@Echo Off
SetLocal EnableExtensions
If /I Not "%__CD__%"=="%~dp0" PushD "%~dp0" 2>Nul||Exit/B 2
Echo=Build Verification...
If Not Exist "build.bat" GoTo buildFail
Echo=Success!
Timeout -1
Exit/B 0

:buildFail
Echo=Building of mod failed!
Timeout -1
Exit/B 1

Just let us know how it goes.

Compo
  • 36,585
  • 5
  • 27
  • 39
1

While I can not reproduce the The syntax of the command is incorrect. in your question, it is possible that your computer has command extensions disabled by default (not the usual case, extensions are by default enabled), which will mean that the %cd% variable or the %~dp0 value are not resolved.

Try to include setlocal enableextensions at the start of the batch file

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • `%CD%` would be expanded to an empty string in that case, which would not cause a syntax error... – aschipfl Dec 24 '16 at 13:16
  • @aschipfl, yes, as indicated, I did not get an syntax error. But expanding the `%cd%` to an empty string or or `%~dp0` to `~dp0` could make the `exist` test fail. – MC ND Dec 24 '16 at 13:34
1

I cannot find a reason for a syntax error in your code, unless you overwrote the variable CD by a quoted string that contains white-spaces or other token separators (,, ;, =) or other special characters, or by a string containing unbalanced quotes ".

For example:

set CD="
if not exist "%CD%\build.bat" echo File not found.

Result:

The syntax of the command is incorrect.

However, this does still not explain why the goto command is executed.

aschipfl
  • 33,626
  • 12
  • 54
  • 99