使用批处理,您将如何遍历一个您不知道它可能包含多少个值的数组 运行 每个单独变量的函数

Using batch, how would you iterate through an array that you don't know how many values it may contain to run a function on each individual variable

我有一个批处理文件,它创建了一个从文本文件中获取的变量数组,如下所示:

for /f "skip=1 tokens=9 delims= " %%a in (%findfile%) do set "_%%a=yes"
set count = 0
for /f "tokens=1* delims==#" %%b in ('set _') do (
    set /a count+=1
    set x=%%b
    set location[!count!]=!x:~1!
)
set %location%

我试图让数组中的每个变量分别循环到一个函数中,但不知道该怎么做!!

存储所有变量的location数组必须被调用到一个for循环和我试图得到它的函数循环进入的是一个 FFMPEG 函数:

for %%i in (%location%\*.mp4) do (if not exist "%%~ni\" MD "%%~ni"

    ffmpeg -i "%%i" -vframes 1 -f image2 -start_number 0 
    "%%~ni\%%~ni_Summary_%%3d.jpeg"

)

所有帮助将不胜感激

因为您已经有了元素的计数,您可以使用 FOR /L 循环。

FOR /L %%n in (1 1 %count%) DO (
  set "content=!location[%%n]!"
  call :subFunc content
)
...

:subFunc
set "var=!%1!"
echo Process: !var!

set %location% 应该是 set location。它应该显示所有以 location.
开头的变量 没有(真正的)数组这样的东西,它只是一堆变量。所以你不能将整个集合添加为 %location%.
那就是说:你有 %count% 中的变量数,所以你可以愉快地使用 for /L:

@echo off 
SetLocal EnableDelayedExpansion
REM get locations from textfile:
for /f "tokens=9" %%a in (file.txt) do set "_%%a=yes"

REM translate into proper variables:
set count=0
for /f "tokens=1* delims==#" %%a in ('set _') do (
  set /a count+=1
  set x=%%a
  set _var[!count!]=!x:~1!
)

REM build the new bat file:
(for /l %%i in (1 1 %count%) do (
  for %%a in ("!_var[%%i]!\*.mp4") do (
    ECHO ffmpeg -i "%%i" -vframes 1 -f image2 -start_number 0 "%%~na\%%~na_Summary_%%3d.jpeg"
  )
))>new.bat

type new.bat