如何使用批处理脚本将值存储在变量中并将该变量作为新列加载到文件中?

如何使用批处理脚本将值存储在变量中并将该变量作为新列加载到文件中?

我想读取一个 csv 文件并将一个值存储在一个变量中,然后使用该变量值将其写入同一文件的最后一列。

@echo off 
del three.txt 
setlocal ENABLEDELAYEDEXPANSION 
set N=1 
for /f "tokens=* delims= " %%a in (combined.csv) do ( 
  set /a N+=1 
  if !N! equ 7 >> three.txt echo.%%a 
  ) 
for /f "tokens=1-10 delims=, " %%a in (three.txt) do ( 
  SET x=%%g 
) 
echo %x% >> three.txt 
endlocal

我希望将 three.txt 中的最后读取值存储在一个变量中,并使用相同的值在同一文件中打印一列。以下是 combined.csv 中的数据

,Safaricom,,,,Channel to subscriber transfer,,,,,,,,,
,,,,,,,,,,,Report code :,,C2STRANSFER02,
,Network name    :,,Safaricom,,,,,,,Service:,,,ALL,
,Zone :,,Safaricom    Zone,,,,,,,Category :,,,ALL,
,Domain :,,Dealer Domain,,,,,,,User    name :,,,ALL,
,Dated :,,02/12/15 08:56:46,,,,,,,Date :,,,01/12/15,
,Sl. No.,Transaction ID,,Request source,User name,Sender mobile number,Receiver mobile number,Service   
   class,Service,Sub-service,Requested amount,Credit amount,Bonus,Process fee
,Transfer,,,,,,,,,,,,,
,1,R151201.2053.140019,,EXTGW,GAZETI LIMITED,700945609,700648591,PREPAID,Customer Recharge,Airtime,130.00,130.00,0.00,0.00
,2,R151201.2243.140015,,EXTGW,GAZETI LIMITED,700945609,727783637,PREPAID,Customer Recharge,Airtime,50.00,50.00,0.00,0.00

我从 combined.csv 中读取了日期 01/12/15,我想将其存储在一个变量中,并将所有记录的相同日期存储为最后一列。

答案1

我想读取日期combined.csv并将其作为新的最后一列添加到文件中three.txt

您的问题不是很清楚(即使经过多次编辑)并且您的批处理文件在很多方面都存在问题。

请尝试以下批处理文件(example.cmd):

@echo off 
setlocal
if exist three.txt del three.txt 
rem get the date "01/12/15"
for /f "skip=5 tokens=1-10 usebackq delims=," %%a in (`type combined.csv`) do ( 
  set _date=%%d
  goto :next
  )
:next
rem add the date to the end of every line and output to "three.txt"
for /f "tokens=* usebackq" %%a in (`type combined.csv`) do ( 
  echo %%a,%_date%>>three.txt
  )
endlocal

示例输出:

F:\test>type combined.csv
,Safaricom,,,,Channel to subscriber transfer,,,,,,,,,
,,,,,,,,,,,Report code :,,C2STRANSFER02,
,Network name    :,,Safaricom,,,,,,,Service:,,,ALL,
,Zone :,,Safaricom    Zone,,,,,,,Category :,,,ALL,
,Domain :,,Dealer Domain,,,,,,,User    name :,,,ALL,
,Dated :,,02/12/15 08:56:46,,,,,,,Date :,,,01/12/15,
,Sl. No.,Transaction ID,,Request source,User name,Sender mobile number,Receiver mobile number,Service class,Service,Sub-service,Requested amount,Credit amount,Bonus,Process fee
,Transfer,,,,,,,,,,,,,
,1,R151201.2053.140019,,EXTGW,GAZETI LIMITED,700945609,700648591,PREPAID,Customer Recharge,Airtime,130.00,130.00,0.00,0.00
,2,R151201.2243.140015,,EXTGW,GAZETI LIMITED,700945609,727783637,PREPAID,Customer Recharge,Airtime,50.00,50.00,0.00,0.00

F:\test>example.cmd

F:\test>type three.txt
,Safaricom,,,,Channel to subscriber transfer,,,,,,,,,,01/12/15
,,,,,,,,,,,Report code :,,C2STRANSFER02,,01/12/15
,Network name    :,,Safaricom,,,,,,,Service:,,,ALL,,01/12/15
,Zone :,,Safaricom    Zone,,,,,,,Category :,,,ALL,,01/12/15
,Domain :,,Dealer Domain,,,,,,,User    name :,,,ALL,,01/12/15
,Dated :,,02/12/15 08:56:46,,,,,,,Date :,,,01/12/15,,01/12/15
,Sl. No.,Transaction ID,,Request source,User name,Sender mobile number,Receiver mobile number,Service class,Service,Sub-service,Requested amount,Credit amount,Bonus,Process fee,01/12/15
,Transfer,,,,,,,,,,,,,,01/12/15
,1,R151201.2053.140019,,EXTGW,GAZETI LIMITED,700945609,700648591,PREPAID,Customer Recharge,Airtime,130.00,130.00,0.00,0.00,01/12/15
,2,R151201.2243.140015,,EXTGW,GAZETI LIMITED,700945609,727783637,PREPAID,Customer Recharge,Airtime,50.00,50.00,0.00,0.00,01/12/15

进一步阅读

  • Windows CMD 命令行的 AZ 索引- 与 Windows cmd 行相关的所有事物的绝佳参考。
  • 对于/f- 循环命令以执行另一个命令的结果。
  • - 指示批处理程序跳转到标记的行。
  • 类型- 显示一个或多个文本文件的内容。

相关内容