0

User must edit my batch file to set variables. For example this line:

REM Enter there you program path:
set Path1="C:\folder1\folder2"

I want to bring value of Path1 to form:

C:\folder1\folder2\

User can write\or not quotes, last backslash

C:\folder1\folder2\
C:\folder1\folder2
"C:\folder1\folder2"

Maybe even some unforeseen situations. My script uses some concatenation with this string, so I need a single view. For example:

mkdir %Path1%\plugins
copy %Path2% %Path1%\plugins\plugin.dll
%Path1%\program.exe

If variable contains quotes or last backslash script error.

How can I do this?

Mofi
  • 38,783
  • 14
  • 62
  • 115
Yin and yang
  • 3
  • 1
  • 4

2 Answers2

0

With Path1 being later concatenated with other strings, first double quote should be left to variable name and not after the equal sign as this makes a big difference, see How to set environment variables with spaces? and Why is no string output with 'echo %var%' after using 'set var = text' on command line?

So at top of your batch file there should be:

REM Enter here you program path:
set "ProgramPath=C:\folder1\folder2"

In the batch file you can use string substitutions to make sure the environment variable ProgramPath holds the path without double quotes and without a backslash at end.

if not defined ProgramPath goto InvalidProgPath
set "ProgramPath=%ProgramPath:"=%"
if not defined ProgramPath goto InvalidProgPath
if "%ProgramPath:~-1%" == "\" set "ProgramPath=%ProgramPath:~0,-1%"
if not defined ProgramPath goto InvalidProgPath
if not exist "%ProgramPath%\" goto InvalidProgPath

rem Other commands as shown below.

mkdir "%ProgramPath%\plugins"
copy "%Path2%" "%ProgramPath%\plugins\plugin.dll"
"%ProgramPath%\program.exe"

goto :EOF

:InvalidProgPath
echo The program path as defined with ProgramPath at top of batch file
echo %~f0
echo is invalid. Please correct the path.
echo/
pause

The second command line replaces all double quotes in value of ProgramPath by nothing, i.e. all double quotes are removed from program path string.

The fourth line compares case-sensitive the last character of value of ProgramPath with a backslash and if this character is a backslash, ProgramPath is set again with all characters with exception of last character.

In the remaining batch file you have to enclose the concatenated strings in double quotes.

By the way: If the program path is the path of the folder containing the batch file, then all needed at top is:

set "ProgramPath=%~dp0"
set "ProgramPath=%ProgramPath:~0,-1%"

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /? ... explains %~dp0 ... drive and path of argument 0 which is the batch file itself always ending with a backslash and %~f0 ... full file name of argument 0 with file extension and path.
  • copy /?
  • echo /?
  • goto /?
  • if /?
  • mkdir /?
  • pause /?
  • rem /?
  • set /?
Mofi
  • 38,783
  • 14
  • 62
  • 115
0

In addition @Mofi answer, i did a small function for formatting path:

REM Use double quote "" in CALL parameter ""%param1%""
:formatPath
set "var=%1"
set "var=%var:"=%"
if "%var:~-1%" == "\" set "var=%var:~0,-1%"
set "_return=%var%"
set "var="
goto :eof

Example of using it:

@echo off
REM Evil user entered wrong quotes
set Path1="C:\Program Files (x86)\!HALO\"

call :normalizeFolderPath ""%Path1%""
set "Path1=%_return%"
set "_return="

echo %Path1%
pause
goto :eof

REM Use double quote "" in CALL parameter ""%param1%""
REM Returns _return variable
:normalizeFolderPath
set "var=%1"
set "var=%var:"=%"
if "%var:~-1%" == "\" set "var=%var:~0,-1%"
set "_return=%var%"
set "var="
goto :eof
Yin and yang
  • 3
  • 1
  • 4