Reputation: 1792
I'm trying to check a variable that contains a path to see if there is a trailing slash and if there is then remove it. I have it working if the variable value is not surrounded by quotes but I also need to check for the trailing slash even if quotes exist.
The issue that I'm running into is trying to get an If statement to work to check for the double quote so I can basically check if it has a double quote and then check for the trailing slash. The If statement fails with "( was unexpected at this time". I'm sure it's an escaping issue but I've tried every way I can think of and haven't been able to get it to work. I've been searching on the web for hours with no luck.
Here's what I have for it so far (I left out the code checking for it without the quotes, basically the same format). Also, if there's a better way to achieve this I'm all ears.
set appRoot="C:\test\"
REM grab the last two characters
set lastChar=%appRoot:~-2%
if %lastChar% == \" (
echo It works!
)
Upvotes: 0
Views: 1802
Reputation: 70951
Once the line if %lastChar% == \" (
is expanded (variables replaced with values), what you get is if \" == \" (
which is not a valid condition, so the parenthesis is not expected.
With the value assigned to appRoot, your best option is directly check not the last but the previous character with
set "lastChar=%appRoot:~-2,1%"
if %lastChar% == \
Or, if you can change the value of appRoot
set "appRoot=C:\test\"
REM grab the last character
set lastChar=%appRoot:~-1%
if %lastChar% == \ (
echo It works!
)
EDITED to adjust to comments
Since the OP don't have control on how the paths are asigned to the variables, the possible cases are: variables in the form set var=
, set var="path"
, set var=path
or set "var=path"
(we can assume the two last are equivalent if no special characters present). With or without trailing backslash. With or without spaces in path. And the need is to get a correct path (good looking, exists or not, this to be checked later), without trailing backslashes and (not in OP question, but should be) with or without quotes in a consistent manner.
So, here we go
@echo off
setlocal enableextensions
set "appRoot=c:\some where\"
call :cleanToFullPath appRoot
echo %appRoot%
set "appRoot=c:\some where"
call :cleanToFullPath appRoot
echo %appRoot%
set appRoot="c:\some where\"
call :cleanToFullPath appRoot
echo %appRoot%
set appRoot="c:\some where\a\"
call :cleanToFullPath appRoot
echo %appRoot%
set appRoot=c:\some where\in a rare place
call :cleanToFullPath appRoot
echo %appRoot%
set appRoot=""
call :cleanToFullPath appRoot
echo %appRoot%
goto :EOF
:cleanToFullPath variableName
rem Prepare environment
setlocal enableextensions enabledelayedexpansion
rem get variable name
set "_varName=%~1"
rem get value of variable
set "_tmp=!%~1!"
rem remove quotes from variable value
set "_tmp=%_tmp:"=%"
rem handle empty variables. Default current folder
if not defined _tmp set "_tmp=."
rem prepare to process trailing bar if any
if "%_tmp:~-1%"=="\" set "_tmp=%_tmp%."
rem resolve to full path
for %%# in ("%_tmp%") do set "_tmp=%%~f#"
rem cleanup and update variable
endlocal & set "%~1=%_tmp%"
goto :eof
Upvotes: 0
Reputation: 41287
This is a robust method
@echo off
set appRoot="C:\test\"
for %%a in (%approot%) do for %%b in ("%%~a\.") do echo "%%~fb"
pause
If you can change the layout of quotes in the set statement then this is simpler:
@echo off
set "appRoot=C:\test\"
for %%a in ("%approot%\.") do echo "%%~fa"
pause
Upvotes: 1