Remove Trailing Slash From Batch File Input

Batch FileSubstring

Batch File Problem Overview


I have a batch file that I want to improve. Instead of requiring a user to provide a folder path without a trailing slash, is there an easy way for me to just remove the last character from the path if there is a slash on the end?

: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 %datapath:~-1%==\ GOTO:START

Batch File Solutions


Solution 1 - Batch File

you can use syntax similar your evaluation:

::Does string have a trailing slash? if so remove it 
IF %datapath:~-1%==\ SET datapath=%datapath:~0,-1%

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrookView Question on Stackoverflow
Solution 1 - Batch FileakfView Answer on Stackoverflow