Reputation: 187
This is a variation of this question: Remove Trailing Slash From Batch File Input
but it's subtly different, so I don't think it's a dupe.
I'm having trouble making this work with directories that have spaces in them (running WinXP).
:START
@echo What folder do you want to process? (Provide a path without a closing backslash)
set /p datapath=
::Is string empty?
IF X%datapath% == X GOTO:START
::Does string have a trailing slash? if so remove it
IF %datapath:~-1%==\ SET datapath=%datapath:~0,-1%
echo %datapath%
It handles:
c:\
properly (stripping it to c:)
But if you enter:
c:\test space
the error is "space was unexpected at this time."
If you try enter:
"c:\test space"
You get the same error.
I thought it would involve a strategically placed " or two in this line:
IF %datapath:~-1%==\ SET datapath=%datapath:~0,-1%
But I haven't had any luck.
Any ideas?
Upvotes: 3
Views: 3001
Reputation: 82267
You can solve it with delayed expansion, because delayed expansion works different than percent expansion.
:START
setlocal EnableDelayedExpansion
@echo What folder do you want to process? (Provide a path without a closing backslash)
set /p datapath=
::Is string empty?
IF X!datapath! == X GOTO:START
::Does string have a trailing slash? if so remove it
IF !datapath:~-1!==\ SET "datapath=!datapath:~0,-1!"
echo !datapath!
It expands later than the percent expansion, and after the delayed expansion no more parsing is done, so even spaces or special characters have any effect.
Upvotes: 2