문제

I have a batch file that is passed commands in the form of a string array from a Java file. The commands contain something like the following:

String[] commands = {"A", 
                     "B", 
                     "C", 
                     "C:\users\user\Documents",
                     "C:\users\user\Pictures"}

The commands array is dynamic, as it changes every time the java program is run. In the batch file, I create variables to take the values of the first three elements (A, B, and C in this case). Then I need to shift the directory strings to take up the first three elements of the array. Here is the batch code I have so far:

@echo off
setlocal enableDelayedExpansion
set /A paramCount=0
for %%x in (%*) do (
    set list[!paramCount!]=%%x
    set /A paramCount=paramCount+1
)
set argA=%list[0]%
set argB=%list[1]%
set argC=%list[2]%

set /A old=0
set /A new=!old!+3
for /F "tokens=2 delims==" %%a in ('set list[') do (
    echo old=!old!
    echo new=!new!
    set list[!old!]=!list[%new%]!
    echo !list[%old%]!
    set /A old=!old!+1
    set /A new=!new!+1 ) 

The problem I am having is with the line set list[!old!]=!list[%new%]!. As you can see, I have delayed expansion enabled. However, the !!'s are needed for the list[...] variable that is emulating an element in an array. However, I believe I need to use delayed expansion for "new" as well. What am I to do in this case? Or perhaps that's not the actual problem? The "old" and "new" variables are incrementing correctly, but the echo !list[%old%]! line returns the same value every time. I expect the same issue exists in that line, with "old"--It should have !'s surrounding it but the !'s are already being used for the list[...] variable. So what happens if you need nested !'s in a statement? Thanks for the aid!

도움이 되었습니까?

해결책

@echo off
setlocal ENABLEDELAYEDEXPANSION
set /A paramCount=-3
for %%x in (%*) do (
    set list[!paramCount!]=%%x
    set /A paramCount=paramCount+1
)
set argA=%list[-3]%
set argB=%list[-2]%
set argC=%list[-1]%
for /F "tokens=2 delims==" %%a in ('set list[-') do SET "%%a="
SET arg
SET list
ENDLOCAL
echo==================
setlocal ENABLEDELAYEDEXPANSION

set /A paramCount=0
for %%x in (%*) do (
    set list[!paramCount!]=%%x
    set /A paramCount=paramCount+1
)
set argA=%list[0]%
set argB=%list[1]%
set argC=%list[2]%

set /A old=0
set /A new=!old!+3
for /F "tokens=2 delims==" %%a in ('set list[') do (
    echo old=!old!
    echo new=!new!
    CALL set list[%%old%%]=%%list[!new!]%%
    CALL ECHO(%%list[!old!]%%
    set /A old=!old!+1
    set /A new=!new!+1 
) 
SET arg
SET list
GOTO :EOF

This should work for you - the easy way and the hard way.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top