Here is my actual code I was trying to make work
This version tries to skip the code entirely when the IF evaluates to true
It fails because the goto exits the loop entirely
Code: Select all
:MoveObjectOnly
set "_MoveObjectOnly_prefix=_MOI"
set _MOI_Input=%~1
set _MOI_Output=%~2
REM this should copy all variable suffix to the new destination
for /f "tokens=1,2* delims==" %%a in ('set %_MOI_Input% 2^>nul') do (
if "[%_MOI_Input%]" NEQ "[%%a]" GoTo :MoveObjectOnly-skip
setlocal enabledelayedexpansion
set _MOI_Suffix_buffer_input=%%a
set _MOI_Suffix_buffer_output=%_MOI_Output%!_MOI_Suffix_buffer_input:%_MOI_Input%=!
for /f "tokens=*" %%Z in ('echo.!_MOI_Suffix_buffer_output!') do (
endlocal
echo set %%Z=%%b
if "[%_MOI_Input%]" NEQ "[%%a]" set %%Z=%%b
)
if defined _MOI_localscope endlocal
:MoveObjectOnly-skip
)
if defined _MOI_localscope endlocal
REM this clears the old base variable
if "[%_MOI_Input%]" NEQ "[]" for /f "tokens=1,2 delims==" %%a in ('set %_MOI_Input% 2^>nul') do if "[%_MOI_Input%]" NEQ "[%%a]" set %%a=
Call :ClearVariablesByPrefix %_MoveObjectOnly_prefix% _MoveObjectOnly
if "[%~3]" NEQ "[]" if "[%~4]" NEQ "[]" shift & shift & GoTo :MoveObjectOnly
GoTo :EOF
This version put the nested for loop on the IF
This version the "set" command runs correctly but the results are lost, the variables don't actually change
Code: Select all
:MoveObjectOnly
set "_MoveObjectOnly_prefix=_MOI"
set _MOI_Input=%~1
set _MOI_Output=%~2
REM this should copy all variable suffix to the new destination
for /f "tokens=1,2* delims==" %%a in ('set %_MOI_Input% 2^>nul') do (
setlocal enabledelayedexpansion
set _MOI_Suffix_buffer_input=%%a
set _MOI_Suffix_buffer_output=%_MOI_Output%!_MOI_Suffix_buffer_input:%_MOI_Input%=!
if "[%_MOI_Input%]" NEQ "[%%a]" for /f "tokens=*" %%Z in ('echo.!_MOI_Suffix_buffer_output!') do (
endlocal
echo set %%Z=%%b
set %%Z=%%b
)
if defined _MOI_localscope endlocal
)
if defined _MOI_localscope endlocal
REM this clears the old base variable
if "[%_MOI_Input%]" NEQ "[]" for /f "tokens=1,2 delims==" %%a in ('set %_MOI_Input% 2^>nul') do if "[%_MOI_Input%]" NEQ "[%%a]" set %%a=
Call :ClearVariablesByPrefix %_MoveObjectOnly_prefix% _MoveObjectOnly
if "[%~3]" NEQ "[]" if "[%~4]" NEQ "[]" shift & shift & GoTo :MoveObjectOnly
GoTo :EOF
This version works as expected
Code: Select all
:MoveObjectOnly
set "_MoveObjectOnly_prefix=_MOI"
set _MOI_Input=%~1
set _MOI_Output=%~2
REM this should copy all variable suffix to the new destination
for /f "tokens=1,2* delims==" %%a in ('set %_MOI_Input% 2^>nul') do (
setlocal enabledelayedexpansion
set _MOI_Suffix_buffer_input=%%a
set _MOI_Suffix_buffer_output=%_MOI_Output%!_MOI_Suffix_buffer_input:%_MOI_Input%=!
for /f "tokens=*" %%Z in ('echo.!_MOI_Suffix_buffer_output!') do (
endlocal
echo set %%Z=%%b
if "[%_MOI_Input%]" NEQ "[%%a]" set %%Z=%%b
)
if defined _MOI_localscope endlocal
)
if defined _MOI_localscope endlocal
REM this clears the old base variable
if "[%_MOI_Input%]" NEQ "[]" for /f "tokens=1,2 delims==" %%a in ('set %_MOI_Input% 2^>nul') do if "[%_MOI_Input%]" NEQ "[%%a]" set %%a=
Call :ClearVariablesByPrefix %_MoveObjectOnly_prefix% _MoveObjectOnly
if "[%~3]" NEQ "[]" if "[%~4]" NEQ "[]" shift & shift & GoTo :MoveObjectOnly
GoTo :EOF
Here is the test function
Code: Select all
:MoveObjectOnly-DEMO
set myarray[0]=Open Source, light and extremely simple,
set myarray[1]=It is a single executable file with no dependencies.
set myarray[2]=Just download it and add it to your PATH
set myarray[6]=Create, edit, copy, move, download your files easily,
set myarray[7]=everywhere, every time. Use it as your personal cloud.
set myarray[0].suffixA=test.sufA.0
set myarray[1].suffixA=test.sufA.1
set myarray[2].suffixA=test.sufA.2
set myarray[6].suffixA=test.sufA.6
set myarray[7].suffixA=test.sufA.7
set myarray[0].suffixB=test.sufB.0
set myarray[1].suffixB=test.sufB.1
set myarray[2].suffixB=test.sufB.2
set myarray[6].suffixB=test.sufB.6
set myarray[7].suffixB=test.sufB.7
set myarray.ubound=7
echo.&echo Printing myarray&echo.
call :echoarray myarray LINENUMBERS
echo.&call :echoarray myarray .suffixA LINENUMBERS
echo.&call :echoarray myarray .suffixB LINENUMBERS
echo.&echo Moving object [6] to [4]&echo.
call :moveobjectonly myarray[6] myarray[4]
echo.&echo Printing myarray&echo.
call :echoarray myarray LINENUMBERS
echo.&call :echoarray myarray .suffixA LINENUMBERS
echo.&call :echoarray myarray .suffixB LINENUMBERS
echo.&echo Moving object [4] to [3]&echo.
call :moveobjectonly myarray[4] myarray[3]
echo.&echo Printing myarray&echo.
call :echoarray myarray LINENUMBERS
echo.&call :echoarray myarray .suffixA LINENUMBERS
echo.&call :echoarray myarray .suffixB LINENUMBERS
echo.&echo Moving objects [1][2][3] to [4][5][6]&echo.
call :moveobjectonly myarray[1] myarray[4]
call :moveobjectonly myarray[2] myarray[5]
call :moveobjectonly myarray[3] myarray[6]
echo.&echo Printing myarray&echo.
call :echoarray myarray LINENUMBERS
echo.&call :echoarray myarray .suffixA LINENUMBERS
echo.&call :echoarray myarray .suffixB LINENUMBERS
GoTo :EOF