Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Suppose in a batch file, say receive.bat, is called with some arguments. Now receive.bat executes and it needs to process these arguments.

Sometimes (but not always), an argument will end with a trailing slash (e.g. "C:\a\b\"). The number of arguments is not fixed and the (only) such argument may appear anywhere in the list of arguments.

How can my .bat file remove the \ in \", for example, to get "C:\a\b"?


Update:

I tried the examples below and the 3rd one is not working.
It just said there should not be a SET here.

SET var=C:\
IF %var:~-1%==\ SET var=%var:~0, -1%
@ECHO %var%

SET var2="C:\\
IF %var2:~-2%==\\ SET var2=%var2:~0, -2%
@ECHO %var2%

SET var3="C:\"
IF %var3:~-2%==\a" SET var3=%var3:~0, -2%
@ECHO %var3%
share|improve this question
3  
Have a look at stackoverflow stackoverflow.com/questions/2952401/… –  nixda Jan 22 '13 at 18:49
add comment

1 Answer

up vote 1 down vote accepted

You're setting var3 to "C:\", and comparing the last two characters to \a"? See below and this page for help with batch substring extraction.

SET var=C:\
IF %var:~-1%==\ SET var=%var:~0,-1%
@ECHO %var%
REM Output: C:

SET var2="C:\
IF %var2:~-1%==\ SET var2=%var2:~0,-1%
@ECHO %var2%
REM Output: "C:

SET var3="C:\"
IF %var3:~-2,1%==\ SET var3=%var3:~0,-2%"
@ECHO %var3%
REM Output: "C:"
share|improve this answer
    
Thanks, it works. But do you know how can I make this works for a received parameter? For example, I don't want to work with a new variable var, I want to manupulate %1, %2, %3 ...directly. I found %1=%var% or SET %1=%var% doesn't change the value of these parameters. –  user565739 Jan 22 '13 at 20:41
2  
You can't change their values, so you have to copy to another variable. –  Karan Jan 22 '13 at 23:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.