Q:

Remove first and last character from a String in a Windows Batch file

I have the following string inside my Windows batch file:
"-String"

The string also contains the twoe quotation marks at the beginning and at the end of the string, so as it is written above.
I want to strip the first and last characters so that I get the following string:
-String

I tried this:
set currentParameter="-String"
echo %currentParameter:~1,-1%

This prints out the string as it should be:
-String

But when I try to store the edited string like this, it fails:
set currentParameter="-String"
set currentParameter=%currentParameter:~1,-1%
echo %currentParameter%

Nothing gets printed out. What do I do wrong?

This really is strange. When I remove the characters like this it works:
set currentParameter="-String"
set currentParameter=%currentParameter:~1,-1%
echo %currentParameter%

it prints out:
-String

But actually my batch is a bit more complicated and there it does not work. I will show what I programmed:
@echo off

set string="-String","-String2"

Set count=0
For %%j in (%string%) Do Set /A count+=1

FOR /L %%H IN (1,1,%COUNT%) DO ( 

    echo .
        call :myFunc %%H
)
exit /b

:myFunc
FOR /F "tokens=%1 delims=," %%I IN ("%string%") Do (

    echo String WITHOUT stripping characters: %%I 
    set currentParameter=%%I
    set currentParameter=%currentParameter:~1,-1%

    echo String WITH stripping characters: %currentParameter% 

    echo .

)
exit /b   

:end

And the output is:
.
String WITHOUT stripping characters: "-String"
String WITH stripping characters:
.
.
String WITHOUT stripping characters: "-String2"
String WITH stripping characters: ~1,-1
.

But what i want is:
.
String WITHOUT stripping characters: "-String"
String WITH stripping characters: -String
.
.
String WITHOUT stripping characters: "-String2"
String WITH stripping characters: -String2
.

A:

hope will help you. echo String WITHOUT stripping characters: 
%%I 
set currentParameter=%%I
set currentParameter=!currentParameter:~1,-1!

echo String WITH stripping characters: !currentParameter! 

echo .

It might work. Try this once.

A:

You are modyfing a variable inside a parenthesized block. Watch out - the new value will not be used within the same block (unless you delimit the variable with ! instead of % - and running in the enabledelayedexpansion mode).
Or just extract the couple of lines into another sub-function, using a plain sequence of lines insted of (  )
greets, Stach

A:

This script takes advantage of ENABLEDELAYEDEXPANSION.
If you don't know, batch scripts execute for and if commands all in one; hence if you do:
if true==true (
@echo off
set testvalue=123
echo %testvalue%
pause >NUL
)

You wont output anything, because when echo %testvalue% is executed, it has not recognized the testvalue has been changed.
Using delayedexapnsion allows the script to read that value as it is now, and forget the problem I stated before. You use it just like %testvalue%, but you may do !testvalue! to fix this:
if true==true (
@echo off
set testvalue=123
echo !testvalue!
pause >NUL
)

Would echo 123.

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set string="-String","-String2"
Set count=0

For %%j in (%string%) Do Set /A count+=1

FOR /L %%H IN (1,1,%COUNT%) DO ( 
echo .
call :myFunc %%H
)

exit /b
:myFunc

FOR /F "tokens=%1 delims=," %%I IN ("%string%") Do (
echo String WITHOUT stripping characters: %%I 
set currentParameter=%%I
set currentParameter=!currentParameter:~1,-1!
echo String WITH stripping characters: !currentParameter! 
echo .
)

exit /b   
:end

~ Alex

