delayed expansion
inside delayed expansion
doesn't work, try this:
CALL SET "myvar=%%PARAMS[!BEFORE_LAST!]%%"
문제
I need to make delayed expansion inside of delayed expansion, i.e. smth like:
!PARAMS[!BEFORE_LAST!]!
Of course, the above is not valid, so I tried to workaround it with for-loop, but with no success:
SETLOCAL EnableExtensions EnableDelayedExpansion
SET PARAM_COUNT=0
FOR %%P IN (%*) DO (
SET /A PARAM_COUNT+=1
SET PARAMS[!PARAM_COUNT!]=%%P
IF !PARAM_COUNT! GTR 1 (
SET /A BEFORE_LAST = !PARAM_COUNT!-1
FOR /L %%G IN (!BEFORE_LAST!) DO SET BEFORE_LAST_PARAM=!PARAMS[%%G]!
IF "!BEFORE_LAST_PARAM!"=="--buildroot" (
REM perfrom some actions here
)
)
)
ENDLOCAL
How could I achieve the explained behavior?
해결책
delayed expansion
inside delayed expansion
doesn't work, try this:
CALL SET "myvar=%%PARAMS[!BEFORE_LAST!]%%"
다른 팁
This is not so much of an answer, but using @Enduro's Answer, I solved my problem. I am just giving an example here, to see how to use it.
This answer was the only one that worked for me! After a LOT of searching; Basically I was trying to generate a file with all android app package names, without version codes. We have used a gradle script to generate names in a particular format also, something like: com.package.appname-releaseVersion.apk
.
Here is the original code for generating file (apps.ls) with required package names, which I mainly use like this, inside android shell: for app in $(cat < apps.ls ); do monkey -p $app 1; done;
. It just launches all those apps to make sure their services start running and registered.
@echo off
setlocal EnableDelayedExpansion
cd "C:\remoteUpdate"
>apps.ls (
for /r %%v in (*.apk) do (
set "name=%%~nv"
call :indexof "!name!" "-" idx
call echo %%name:~0,!!idx!!%%
)
)
C:\dos2unix\bin\dos2unix.exe apps.ls
@echo Done generating apps.ls
:indexof [%1 - string ; %2 - find index of ; %3 - if defined will store the result in variable with same name]
::http://ss64.org/viewtopic.php?id=1687
@echo off
setlocal enableDelayedExpansion
set "str=%~1"
set "s=!str:%~2=&rem.!"
set s=#%s%
if "%s%" equ "#%~1" endlocal& if "%~3" neq "" (set %~3=-1&exit /b 0) else (echo -1&exit /b 0)
set "len=0"
for %%A in (2187 729 243 81 27 9 3 1) do (
set /A mod=2*%%A
for %%Z in (!mod!) do (
if "!s:~%%Z,1!" neq "" (
set /a "len+=%%Z"
set "s=!s:~%%Z!"
) else (
if "!s:~%%A,1!" neq "" (
set /a "len+=%%A"
set "s=!s:~%%A!"
)
)
)
)
endlocal & if "%~3" neq "" (set %~3=%len%)
exit /b 0
Now the issue was that I needed to filter out certain packages out of my apps.ls
file,
All I needed to do was change the code inside the for loop like so:
CALL SET "truncname=%%name:~0,!!idx!!%%"
if NOT "!truncname!"=="com.blhealthcare.package1" if NOT "!truncname!"=="com.android.package2" echo !truncname!
Everything I tried before that, using only set was not working. However using call with set worked really well. I cannot find any documentation on this as well, as it's difficult to even search this issue. What I noticed was that call echo %%name:~0,!!idx!!%%
was printing correctly, whereas echo %%name:~0,!!idx!!%%
was printing something like name:~0,28
and not working. I couldn't understand it then, but today I can understand from @Endoro's answer that
delayed expansion
insidedelayed expansion
doesn't work