Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
.
like image 487
Metalhead89 Avatar asked Aug 22 '12 13:08

Metalhead89


People also ask

What does %% do in batch?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

What is @echo off batch?

When echo is turned off, the command prompt doesn't appear in the Command Prompt window. To display the command prompt again, type echo on. To prevent all commands in a batch file (including the echo off command) from displaying on the screen, on the first line of the batch file type: Copy. @echo off.

What is %% K in batch file?

So %%k refers to the value of the 3rd token, which is what is returned.

What is %% g'in batch file?

%%parameter : A replaceable parameter: in a batch file use %%G (on the command line %G) FOR /F processing of a command consists of reading the output from the command one line at a time and then breaking the line up into individual items of data or 'tokens'.


2 Answers

Hope this will help you.

    setlocal enabledelayedexpansion
    
    echo String WITHOUT stripping characters: %%I 
    set currentParameter=%%I
    set currentParameter=!currentParameter:~1,-1!
    echo String WITH stripping characters: !currentParameter! 
like image 75
phani Avatar answered Oct 27 '22 01:10

phani


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

like image 44
Stach Avatar answered Oct 27 '22 01:10

Stach