Windows Batch读取文件,解析并输出

telsum2

我在Windows批处理文件上的处理方式达到了90%。它具有2个输入参数,即输入和输出文件。然后,它读取输入文件,并将某些行细分为数组(从第2行开始)。然后我们进入输出循环。除非我使用!counter2!,否则我在用于数组的计数器上的延迟扩展不会更新,因此%counter2%不起作用。

使用!arrayname [!counter2!]!不起作用。

这是它的代码。

@Echo off
if [%1] == [] goto usage
if [%2] == [] goto usage

echo start time : %time%>logfile.log
set input_file=%1
set output_file=%2
if exist %output_file% del %output_file%
Echo Start reading %input_file%>> logfile.log
setLocal EnableDelayedExpansion

set /a counter=1

for /F "tokens=* delims=" %%a in ('type %input_file%') DO (
  ::echo !counter!
  if "!counter!"=="1" set header=%%a
  if not "!counter!"=="1" (
  set data[!counter!]=%%a
  set line=%%a
  set jobnumber[!counter!]=!line:~0,7!
  set docnumber[!counter!]=!line:~7,5!
  set pagecount[!counter!]=!line:~12,2!
  set customernumber[!counter!]=!line:~14,20!
  set presort[!counter!]=0000
  set postcode[!counter!]=0000
  set inserts[!counter!]=!line:~36,11!
  set filler[!counter!]=000000
  set address[!counter!]=!line:~58,350!
  set filler2[!counter!]="                                                                                                                                                                                                                                                                                                  "
  set endline[!counter!]=X
)
  set /a counter=counter+1
)
Echo Start writing %output_file%>> logfile.log

for /L %%G in (2,1,%counter%) DO (
  set counter2=%%G
  echo !counter2!
  echo !jobnumber[%counter2%]!!docnumber[%counter2%]!!pagecount[%counter2%]!!customernumber[%counter2%]!!presort[%counter2%]!!postcode[%counter2%]!!inserts[%counter2%]!!filler[%counter2%]!!address[%counter2%]!!filler2[%counter2%]!!endline[%counter2%]!>>%output_file%
)

echo end time : %time%>>logfile.log
pause
goto :eof

:usage
echo Usage: blah.bat input_filename output_filename
pause
goto :eof

这是回声!jobnumber [%counter2%]!事情还没有解决的地方。回声!counter2!工作良好。

在您问之前,是的,我知道可以使用C#或其他编程语言来更好,更轻松地完成此操作,但是我的任务是在Windows批处理文件中执行此操作。

在此先感谢您提供的任何帮助。电话号码

恩波科马卡

尝试:

for /L %%G in (2,1,%counter%) DO (
  set counter2=%%G
  echo !counter2!
  echo !jobnumber[%%G]!!docnumber[%%G]!!pagecount[%%G]!!customernumber[%%G]!!presort[%%G]!!postcode[%%G]!!inserts[%%G]!!filler[%%G]!!address[%%G]!!filler2[%%G]!!endline[%%G]!>>%output_file%
)

您无需更改的值,coutner2因此不需要它,可以直接使用%%G

虽然如果您需要更改,则counter2必须再次将其包装在for循环中并使用其令牌。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章