Solved How to Nest FOR loops to set variables?

May 22, 2014 at 19:44:42
Specs: Windows 8
I have a generic launch batch file that launches scripts and executables depending on the switch thrown at it.

I am trying to find the absolute path for each variable with something like this:

@echo off && setlocal EnableDelayedExpansion
set var.1=".\clean.bat"
set var.2="..\sort.bat"
set var.3="..\dir1\run.bat"
set var.4="\root1\dir2\run.bat"
set var.5="\\network\path\run.bat"
:
set var.#="..\dir#\... etc."

set no=1
for %%n in (%var.!no!%) do (
if NOT exist %var.!cnt!% goto NEXT
set var.!no!="%%~$path:n"
echo !no! %var.!no!%
set /A no+=1
)

Failed!

On an individual basis, this works:

for %%c in (%var.1%) do (
if NOT exist %%c set OOPS=%%c && goto Error
set var.1="%%~$path:c"
echo %var.1%
)

So, what am I missing?
––––


See More: How to Nest FOR loops to set variables?

Report •


#1
May 22, 2014 at 22:32:57
setlocal & enabledelayedexpansion
:alex2
set var.1=".\clean.bat"
set var.2="..\sort.bat"
set var.3="..\dir1\run.bat"
set var.4="\root1\dir2\run.bat"
set var.5="\\network\path\run.bat"

::set var.#="..\dir#\... etc."

for /L %%n in (1 1 5) do (
echo testing for: !var.%%n!
set test=!var.%%n!
if exist !test! for %%a in (!test!) do echo ---------%%~dpnxa
if exist !test! for %%a in (!test!) do (set var.%%n=%%~dpnxa) else (set var.%%n=)
)
set var.
:: end test-batch

I think something is not optimized here, but I'm not familiar with the whole scenario, so I'll just shut up until more information arises. All this does is test for a file to exist, and then set a "var.x" variable if it does, and nullify the "var.x" if it doesn't exist.

message edited by nbrane


Report •

#2
May 23, 2014 at 06:58:22
Not sure why this wouldn't work:
for /f "tokens=1* delims==" %%a in ('set var.') do set %%a=%%~Fb

How To Ask Questions The Smart Way


Report •

#3
May 25, 2014 at 17:15:16
✔ Best Answer
Good question, great answer.

I also need this to work.

So, after a little tinkering, here is my attempt...

@echo off && setlocal EnableDelayedExpansion
set var.1=".\clean.bat"
set var.2="..\sort.bat"
set var.3="..\dir1\run.bat"
set var.4="\root1\dir2\run.bat"
set var.5="\\network\path\run.bat"
::set var.#="..\dir#\... etc."

set No=1
:TEST
@echo.
set test=!var.%No%!
if exist !test! for %%a in (!test!) do (
  echo   path !No!. !var.%No%!
  set var.!No!=%%~fa
  echo        -- !var.%No%! && @echo.
  set /A No+=1
) else (
  set var.!No!=
  goto :NEXT
)
goto :TEST

:NEXT
@echo ----
set var.
@echo ====

Here’s a thought...

in a “multi-functional” batch to use switches, such as:

  /Start, /Log, /Clear, /Stop, /Kill, /Help (/?)

maybe, experiment with something like:
  set var.1="Title"|"program"|"switches"

It’d be fun to watch the order of the processes.

Any ideas of how to make the extra data work?


Report •

Related Solutions

#4
May 25, 2014 at 18:48:01
This sounds like a terrible amount of work, considering batch already has the %0 - %9 variables and SHIFT.

How To Ask Questions The Smart Way


Report •

#5
May 28, 2014 at 20:48:38
Wow, 2 good solutions.

Thanks guys.


Report •


Ask Question