user3308131
user3308131

Reputation: 179

Why does execution of batch file result in error message "was unexpected at this time"?

Not sure what the exact problem is with this batch file code. I'm running it as an administrator in a CMD window. It runs up until the point of the IF NOT statement. I've included the output (below the code) in an effort to make this as easy as possible for someone looking at it.

The desire of this code is to remove the last sub-directory from the path in pInstallDir environment variable.

For example: pInstallDir="C:\program Files (x86)\uploads\field"

This code should delete a character from the end of pInstallDir until it finds a backslash \. Which would make the contents of pInstallDir equal "C:\program Files (x86)\uploads"

I'm sure I'm missing something obvious. Any help would be much appreciated.

echo Cleaning up Directories
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set pInstallDir="C:\Program Files(x86)\uploads\field\"
set pInstallDir=%pInstallDir:"=%
IF "%pInstallDir:~-1%"=="\" SET pInstallDir=%pInstallDir:~0,-1%
set nf="1"
:r_str
IF NOT "%pInstallDir:~-1%"=="\" (SET pInstallDir=%pInstallDir:~0,-1%) else (set nf="0")
echo %pInstallDir%
if "%nf%"=="1" (goto r_str) else (goto done)
pause
:done
IF %pInstallDir:~-1%"=="\" SET pInstallDir=%pInstallDir:~0,-1%
echo %pInstallDir%

OUTPUT

(echo is off)

Cleaning up Directories
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set pInstallDir=C:\Program Files(x86)\uploads\field\
IF "\" == "\" SET pInstallDir=C:\Program Files(x86)\uploads\field
set nf="1"
\uploads\fiel) was unexpected at this time.
IF NOT "d"=="\" (SET pInstallDir=C:\Program Files(x86)\uploads\fiel) else (set nf="0")

Code exits on itself after the line above.

Upvotes: 0

Views: 4203

Answers (2)

MC ND
MC ND

Reputation: 70923

It is easier to retrieve the full path of the parent folder

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "pInstallDir=C:\Program Files(x86)\uploads\field\"
    for %%a in ("%pInstallDir%\..") do set "pInstallDir=%%~fa"
    echo %pInstallDir%

The main problem in your code are the quotes, both existence of unneeded and missing needed ones, and the presence of parenthesis in the value being handled. For a working version

echo Cleaning up Directories
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

    set "pInstallDir=C:\Program Files(x86)\uploads\field\"
    IF "%pInstallDir:~-1%"=="\" SET "pInstallDir=%pInstallDir:~0,-1%"
    set "nf=1"
:r_str
    IF NOT "%pInstallDir:~-1%"=="\" (SET "pInstallDir=%pInstallDir:~0,-1%") else (set "nf=0")
    echo %pInstallDir%
    if "%nf%"=="1" (goto r_str) else (goto done)
:done
IF "%pInstallDir:~-1%"=="\" SET "pInstallDir=%pInstallDir:~0,-1%"
echo %pInstallDir%

Upvotes: 0

Mofi
Mofi

Reputation: 49086

The double quotes are not correct used on assigning a string value to an environment variable.

Not good:

set pInstallDir="C:\Program Files(x86)\uploads\field\"

This results in environment variable pInstallDir having the path assigned with including the double quotes and additionally also whitespaces at end of the line if there are whitespaces.

Better is:

set "pInstallDir=C:\Program Files(x86)\uploads\field\"

variable=value is the parameter for command set and therefore this syntax should be always used to assign a value with spaces to an environment variable.

It is also possible to use:

set pInstallDir=C:\Program Files(x86)\uploads\field\

But this can result in unexpected behavior of batch file if there are 1 or more trailing spaces at end of this line as in this case they are also assigned to the variable.

Best would be to use

set "pInstallDir=%ProgramFiles(x86)%\uploads\field\"

On Windows x64 there is an environment variable predefined with name ProgramFiles(x86) containing path to default directory for 32-bit applications. This environment variable is not defined on Windows x86 and therefore most batch files test with this variable if a batch file is executed on machine using a 32-bit or a 64-bit Windows.

An example showing the different results for the 4 lines above:

@echo off
set pInstallDir1="C:\Program Files(x86)\uploads\field\"
set "pInstallDir2=C:\Program Files(x86)\uploads\field\"
set pInstallDir3=C:\Program Files(x86)\uploads\field\
set "pInstallDir4=%ProgramFiles(x86)%\uploads\field\"
set pInstallDir
pause

Open a command prompt window and execute the command set to see all the predefined environment variables plus the environment variables added on your machine system wide or for your user account only by applications during installation.

Upvotes: 1

Related Questions