2012-08-22 10 views
8

ho la seguente stringa all'interno del mio file batch di Windows:Rimuovere primo e l'ultimo carattere di una stringa in un file batch di Windows

"-String" 

La stringa contiene anche i marchi twoe virgolette all'inizio e alla fine del la stringa, così come è scritta sopra.

Voglio mettere a nudo il primo e l'ultimo carattere in modo che ricevo la seguente stringa:

-String 

ho provato questo:

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

Questo stampa la stringa come dovrebbe essere:

-String 

Ma quando provo a memorizzare la stringa modificata in questo modo, non riesce:

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

Non viene stampato nulla. Cosa faccio di sbagliato?


Questo è davvero strano. Quando ho rimuovere i caratteri come questo funziona:

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

esso stampa:

-String 

Ma in realtà il mio gruppo è un po 'più complicato e là non funziona. Vi mostrerò che cosa ho programmato:

@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 

e l'uscita è:

. 
String WITHOUT stripping characters: "-String" 
String WITH stripping characters: 
. 
. 
String WITHOUT stripping characters: "-String2" 
String WITH stripping characters: ~1,-1 
. 

Ma quello che voglio è:

. 
String WITHOUT stripping characters: "-String" 
String WITH stripping characters: -String 
. 
. 
String WITHOUT stripping characters: "-String2" 
String WITH stripping characters: -String2 
. 
+0

tue opere campione (come previsto), ci deve essere un altro bug . Forse l'hai provato all'interno di un blocco? – jeb

+0

Viene visualizzato il seguente messaggio: 'ECHO ist ausgeschaltet (OFF). ~ 1, -1' – Metalhead89

+0

Forse è perché è in una funzione? ': myFunc FOR/F "tokens =% 1 delims =" %% I IN ("% processChain1%") Do ( \t set currentParameter = %% I \t set currentParameter =% currentParameter: ~ 1 , -1% \t echo% currentParameter% \t rem java -jar app.jar %% I ) uscita/b' – Metalhead89

risposta

4

Si sono modyfing una variabile all'interno di un blocco tra parentesi. Attenzione: il nuovo valore non verrà utilizzato all'interno dello stesso blocco (a meno che non si delimiti la variabile con! Invece di% e sia in esecuzione nella modalità di espansione abilitata). O semplicemente estrarre il paio di righe in un altro sub-funzione, utilizzando una sequenza semplice di linee invece di()

saluta, Stach

4

speranza vi aiuterà. String eco senza togliere caratteri: %% I

set currentParameter=%%I 
set currentParameter=!currentParameter:~1,-1! 

echo String WITH stripping characters: !currentParameter! 

echo . 

Potrebbe funzionare. Prova questo una volta.

2

Questo script sfrutta ENABLEDELAYEDEXPANSION. Se non si sa, gli script batch vengono eseguiti e se i comandi sono tutti in uno; quindi se si fa:

if true==true (
@echo off 
set testvalue=123 
echo %testvalue% 
pause >NUL 
) 

è solito uscita nulla, perché quando echo% testvalue% viene eseguita, non ha riconosciuto il testvalue è stato modificato. L'uso di delayedexapnsion consente allo script di leggere quel valore così com'è ora e di dimenticare il problema che ho affermato prima. Lo usi come% testvalue%, ma puoi farlo! Testvalue! per risolvere questo problema:

if true==true (
@echo off 
set testvalue=123 
echo !testvalue! 
pause >NUL 
) 
  • associo 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

Problemi correlati