如何使用嵌套FOR在批处理文件中循环 [英] How to use nested FOR loops in batch files
问题描述
我有一些code中,我想使用的
循环处理一组文件。
作为处理文件的部分有一个 FOR / F
循环读取它,将数据追加到基于文件名的其他文件。
为了能够设置输出文件的名字我已经延迟的变量扩充设置。
这是code应该是什么样子,因为我本来想它是:
SETLOCAL enabledelayedexpansion
在(DataFolder \\ *。EXT)%% f执行(
设置POI = %%〜F
设置POI = JbPOI:DataFolder \\ =!
设置POI = JbPOI:.EXT =!
FOR / F令牌= 1,2,3 delims =%%一中(%%〜F)做(
设置CX = %%一
设置CY = %% b
设置FN = %%Ç
回音!FN!,9!CX!,!CY! >> DataFolder \\!POI!.TMP
)
)
ENDLOCAL
这code不起作用,因为变量 %% A
, %% b
和 %%ç
永远不会收到一个值, %% A
始终具有相同的值 %%˚F
。
我看了一下这个问题的一些文章,但不能提取他们工作的解决方案。
我已经试过几件事,没有工作至今...
- 添加一个虚拟的外循环创建变量
%% A
到%%Ĵ
明确 - 内
的
环出,并使它成为子程序 - 内
的
循环并把它作为单独的批处理文件
采取
采取
谁能告诉我如何能 - 或者必须 - 解决
SETLOCAL enabledelayedexpansion
在(DataFolder \\ *。EXT)%% f执行(
设置POI = %%〜F
设置POI = JbPOI:DataFolder \\ =!
设置POI = JbPOI:.EXT =!
FOR / Fuseback令牌= 1,2,3 delims =%%一中(%%〜F)做(
设置CX = %%一
设置CY = %% b
设置FN = %%Ç
回音!FN!,9!CX!,!CY! >> DataFolder \\!POI!.TMP
)
)
ENDLOCAL
我不知道,如果你想读取文件%%〜F
,但我认为这是你所需要的。
I have some code in which I want to use a FOR
loop to handle a set of files.
As part of handling a file there's a FOR /F
loop that reads it and appends data to an other file based on the file name.
In order to be able to set the output file name I have delayed variable expansion set on.
This is what the code should look like as I originally intended it to be:
setlocal enabledelayedexpansion
for %%f in (DataFolder\*.Ext) do (
set POI=%%~f
set POI=!JbPOI:DataFolder\=!
set POI=!JbPOI:.Ext=!
for /f "tokens=1,2,3 delims=," %%a in ("%%~f") do (
set CX=%%a
set CY=%%b
set FN=%%c
echo !FN!,9,!CX!,!CY! >> "DataFolder\!POI!.tmp"
)
)
endlocal
This code doesn't work because variable %%a
, %%b
and %%c
never receive a value, %%a
always has the same value as %%f
.
I have read some articles about this issue but couldn't extract a solution from them that worked.
I have tried several things, none worked so far...
- added a dummy outer loop to create variable
%%a
through%%j
explicitly - inner
FOR
loop taken out and made it a subroutine - inner
FOR
loop taken out and made it a separate batch file
Can anybody please tell me how this can - or must - be solved?
setlocal enabledelayedexpansion
for %%f in (DataFolder\*.Ext) do (
set POI=%%~f
set POI=!JbPOI:DataFolder\=!
set POI=!JbPOI:.Ext=!
for /f "useback tokens=1,2,3 delims=," %%a in ("%%~f") do (
set CX=%%a
set CY=%%b
set FN=%%c
echo !FN!,9,!CX!,!CY! >> "DataFolder\!POI!.tmp"
)
)
endlocal
I'm not sure if you want to read the file "%%~f"
, but I think this is what you need.
这篇关于如何使用嵌套FOR在批处理文件中循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!