2014-06-08 106 views
0

比方說,我們有4個變量,如:刪除和替換變量

set var1=Hello 
set var2=Some text 
set var3=Some more text 
set var4=Even some more text 

然後提示用戶刪除他們中的一個,在這種情況下,(1-4),如:

set /p delete_one="Delete one:" 

如果用戶「2」中寫道例如新的變量是:

var1=Hello 
var2=Some more text 
var3=Even some more text 

我將如何使這項工作?

回答

0
@echo off 
setlocal EnableDelayedExpansion 

set var1=Hello 
set var2=Some text 
set var3=Some more text 
set var4=Even some more text 

SET VAR 

set /p delete_one="Delete one: " 

set "Xvar=" 
for /F "tokens=1,2 delims==" %%a in ('set var') do (
    if defined Xvar (
     set "!Xvar!=%%b" 
     set "Xvar=%%a" 
    ) else if "%%a" equ "var%delete_one%" (
     set "Xvar=%%a" 
    ) 
) 
set "%Xvar%=" 

SET VAR 

您必須檢查是否存在要刪除的給定數量的變量。需要注意的是變量按字母順序排列,所以如果超過9個變量存在,並且要保留它的「自然秩序」,變量小於10必須有一個左零:var01=Hello,你必須給這個左零值刪除變量。

0
@ECHO OFF 
SETLOCAL ENABLEDELAYEDEXPANSION 
set var1=Hello 
set var2=Some text 
set var3=Some more text 
set var4=Even some more text 
FOR /L %%a IN (5,1,12) DO SET var%%a=MORE text %%a 

SET VAR 

set /p delete_one="Delete one: " 

SET /a get=1 
SET /a put=1 

:loop 
IF %get%==%delete_one% SET /a get+=1 
IF %get%==%put% GOTO next 
SET var%put%=!var%get%! 
:next 
SET /a put+=1 
SET /a get+=1 
IF DEFINED var%get% GOTO loop 
SET "var%put%=" 

SET VAR 
GOTO :EOF 

...免疫極限-9。