granitdev
granitdev

Reputation: 146

Batch Script runs on Windows 10 Pro but not on Surface Pro 4

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.

Upvotes: 0

Views: 246

Answers (1)

Mofi
Mofi

Reputation: 49167

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?

Upvotes: 0

Related Questions