0

I have a batch script that has been happily running fine on our desktops and laptops running both W8.1 and W10 for some time now. The same exact script can't get past this last line of code without claiming "syntax error"

if not exist C:\epic\tablet.txt if not exist C:\epic\laptop.txt (
    @set SVPATH="D:\EPIC\Backup\"
) else (
    @set SVPATH="C:\EPIC\Backup\"
)

@echo %SVPATH%

if not exist %SVPATH% mkdir %SVPATH%

There are a bunch of lines of code after it, that again the laptops and desktops have no issues with, but the tablet can't get past that last line. It just gives the following output and then exits the script:

ECHO is on.
>The syntax of the command is incorrect
>if not exists  mkdir
>

It looks like it's not actually setting a value to SVPATH, but as I said, this works just fine on both desktop and loptops running windows 8.1 and windows 10. It's only the Surface Pro 4s that have the issue, and it's not just one, it's all that we have tested.

granitdev
  • 136
  • 2
  • 13
  • 1
    check your logic - there is an `else` for the second `if` but none for the first one. You might need another pair of parentheses. – Stephan Aug 31 '18 at 16:58
  • Nope, that didn't help. Also remember this works fine everywhere BUT the Surface Pro. Does Microsoft load a different version of the batch interpreter on the tablet version of Windows 10? – granitdev Aug 31 '18 at 18:18
  • not that I'm aware of. You can check the version by entering `cmd`. What about the existence of both of the .txt files? (and just to be sure: the code you show is the complete code or is there anything around?) – Stephan Aug 31 '18 at 18:25
  • You do not need to check for the existence of a directory before creating it. Just create it and redirect the errors to nul. `md mydir 2>nul` – Squashman Aug 31 '18 at 18:53

1 Answers1

0

I don't have a Surface Pro, but try following batch code:

@echo off
set SVPATH=C:\EPIC\Backup
if not exist C:\epic\tablet.txt if not exist C:\epic\laptop.txt set SVPATH=D:\EPIC\Backup
echo %SVPATH%
md %SVPATH% 2>nul
set SVPATH=

It is required to enclose file/folder argument strings within " on containing a space or one of these characters &()[]{}^=;!'+,`~ which is not the case here. So the double quotes can be safely omitted in this batch file.

The environment variable SVPATH is in any case defined with this code independent on existence of the two files C:\epic\tablet.txt and C:\epic\laptop.txt.

It is recommended to assign a file/folder string to an environment variable always without double quotes to an environment variable and instead reference the environment variable value as is or concatenated with other environment variable value references or with fixed strings enclosed within double quotes.

@echo off
set "SVPATH=C:\EPIC\Backup"
if not exist "C:\epic\tablet.txt" if not exist "C:\epic\laptop.txt" set "SVPATH=D:\EPIC\Backup"
echo %SVPATH%
md "%SVPATH%" 2>nul
set SVPATH=

Please note that recommended syntax set "Variable=Value" requires enabled command extensions. For more details about this syntax read the answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line?

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Thanks for the information, but dang do I hate batch. I ended up stacking if statements and got it to work on both systems. But this is exemplary of why we recommend all scripts be written in python at my workplace. THIS will be rewritten as soon as scheduling allows! – granitdev Sep 04 '18 at 11:49