我有一个非常老旧的 Window XP 程序,我想让它自动化一点。它需要三个参数:
ITF.EXE <control-file> <input-file> <output-file>
并且它只接受 8.3 文件名。
我想将文件名 done_fgx0229.all 输入到批处理脚本解析中,然后运行以下命令:
ITF.EXE <control-file> fgx0229.all fgx0229.cvs
我有一个目录,其中包含大约一百个文件,其名称如下:
done_fgx0229.all
done_fgx0330.all
ETC。
这是我正在尝试运行的批处理脚本:
ECHO ON
CD C:\0-Do_Infotran\ToDo
SetLocal EnableDelayedExpansion
DIR /B done_*.all > ToDo_Files.txt
set "File=C:\0-Do_Infotran\ToDo_Files.txt"
set /a count=0
FOR /F "tokens=* delims=" %%i IN ('Type "%File%"') DO (
Set /a count+=1
Set "FILENAME[!count!].i=%%i"
SET j=
SET "j=%i:~6,11%"
SET k=
SET "k=%i:~6,7%"
SET l=
SET "l=%i:~9,4%"
echo i = %%i, j = %j%, k = %k%, l = %l%
Set "FILENAME[!count!].j=%j%"
Set "FILENAME[!count!].k=%k%"
Set "FILENAME[!count!].l=%l%"
)
When I run the script here the output for filename done_fgx0229.all
C:\0-Do_Infotran>ECHO ON
C:\0-Do_Infotran>CD C:\0-Do_Infotran\ToDo
C:\0-Do_Infotran\ToDo>SetLocal EnableDelayedExpansion
C:\0-Do_Infotran\ToDo>DIR /B done_*.all 1>ToDo_Files.txt
C:\0-Do_Infotran\ToDo>set "File=C:\0-Do_Infotran\ToDo_Files.txt"
C:\0-Do_Infotran\ToDo>set /a count=0
C:\0-Do_Infotran\ToDo>FOR /F "tokens=* delims=" %i IN ('Type "C:\0-Do_Infotran\ToDo_Files.txt"') DO (
Set /a count+=1
Set "FILENAME[!count!].i=%i"
SET j=
SET "j=fgx0229.all"
SET k=
SET "k=fgx0229"
SET l=
SET "l=0229"
echo i = %i, j = fgx0229.all, k = , l =
Set "FILENAME[!count!].j=fgx0229.all"
Set "FILENAME[!count!].k="
Set "FILENAME[!count!].l="
)
C:\0-Do_Infotran\ToDo>(
Set /a count+=1
Set "FILENAME[!count!].i=done_fgx0229.all"
SET j=
SET "j=fgx0229.all"
SET k=
SET "k=fgx0229"
SET l=
SET "l=0229"
echo i = done_fgx0229.all, j = fgx0229.all, k = , l =
Set "FILENAME[!count!].j=fgx0229.all"
Set "FILENAME[!count!].k="
Set "FILENAME[!count!].l="
)
i = done_fgx0229.all, j = fgx0229.all, k = , l =
我不明白为什么 k 和 l 变量的 SET 命令不起作用。看起来字符串解析正确,但 k 和 l 变量未设置。
答案1
试一下:
@echo off && setlocal enabledelayedexpansion
cd /d "c:\0-do_infotran\ToDo"
dir /b done_*.all >.\ToDo_files.txt
set "file=c:\0-do_infotran\ToDo_files.txt"
for /f "tokens=1*delims=_" %%i in ('type "!file!"')do (
set /a "count+=1+0"
set "j=%%~i_%%~j"
set "k=%%~j"
set "l=%%~nj.csv"
set "filename_[!count!].j=!j!"
set "filename_[!count!].k=!k!"
set "filename_[!count!].l=!l!"
echo\ file = %%~i_%%~j, j = !j!, k = !k!, l = !l!
)
for /l %%L in (1 1 !count!)do (
echo\ ITF.EXE !filename_[%%~L].j! !filename_[%%~L].k! !filename_[%%~L].l!
)
endlocal & goto :eof
- 对于此文件列表:
done_fgx0221.all
done_fgx0222.all
done_fgx0223.all
done_fgx0224.all
done_fgx0225.all
done_fgx0226.all
done_fgx0227.all
done_fgx0228.all
done_fgx0229.all
done_fgx0210.all
- 这是输出:
file = done_fgx0221.all, j = done_fgx0221.all, k = fgx0221.all, l = fgx0221.csv
file = done_fgx0222.all, j = done_fgx0222.all, k = fgx0222.all, l = fgx0222.csv
file = done_fgx0223.all, j = done_fgx0223.all, k = fgx0223.all, l = fgx0223.csv
file = done_fgx0224.all, j = done_fgx0224.all, k = fgx0224.all, l = fgx0224.csv
file = done_fgx0225.all, j = done_fgx0225.all, k = fgx0225.all, l = fgx0225.csv
file = done_fgx0226.all, j = done_fgx0226.all, k = fgx0226.all, l = fgx0226.csv
file = done_fgx0227.all, j = done_fgx0227.all, k = fgx0227.all, l = fgx0227.csv
file = done_fgx0228.all, j = done_fgx0228.all, k = fgx0228.all, l = fgx0228.csv
file = done_fgx0229.all, j = done_fgx0229.all, k = fgx0229.all, l = fgx0229.csv
file = done_fgx0210.all, j = done_fgx0210.all, k = fgx0210.all, l = fgx0210.csv
ITF.EXE done_fgx0221.all fgx0221.all fgx0221.csv
ITF.EXE done_fgx0222.all fgx0222.all fgx0222.csv
ITF.EXE done_fgx0223.all fgx0223.all fgx0223.csv
ITF.EXE done_fgx0224.all fgx0224.all fgx0224.csv
ITF.EXE done_fgx0225.all fgx0225.all fgx0225.csv
ITF.EXE done_fgx0226.all fgx0226.all fgx0226.csv
ITF.EXE done_fgx0227.all fgx0227.all fgx0227.csv
ITF.EXE done_fgx0228.all fgx0228.all fgx0228.csv
ITF.EXE done_fgx0229.all fgx0229.all fgx0229.csv
ITF.EXE done_fgx0210.all fgx0210.all fgx0210.csv
尝试替换:%j% to !j!, %k% to !k!, %l% to !l!
Set "FILENAME[!count!].j=%j%"
Set "FILENAME[!count!].k=%k%"
Set "FILENAME[!count!].l=%l%"
您需要使用:!EnableDelayedExpansion!
Set "FILENAME[!count!].j=!j!"
Set "FILENAME[!count!].k=!k!"
Set "FILENAME[!count!].l=!l!"
- 进一步阅读:
[√]放
[√]命令 /?
[√]For 循环
[√]延缓扩张
[√]CMD/蝙蝠弦操作