Como armazenar um valor em uma variável e carregar essa variável como uma nova coluna em um arquivo usando um script em lote?

1

Eu quero ler um arquivo csv e armazenar um valor em uma variável e usar esse valor de variável para ser escrito como uma última coluna do mesmo arquivo.

@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

Eu quero que o último valor de leitura que está em três.txt seja armazenado em uma variável e use o mesmo para imprimir uma coluna no mesmo arquivo. Abaixo estão os dados em 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

Eu tinha lido a data 01/12/15 do arquivo combined.csv Eu quero armazená-lo em uma variável e armazenar a mesma data para todos os registros como uma última coluna.

    
por user2500677 09.05.2016 / 12:20

1 resposta

0

Quero ler a data de combined.csv e adicioná-la como uma nova última coluna a um arquivo three.txt .

A sua pergunta não é muito clara (mesmo depois de várias edições) e o seu ficheiro batch é quebrado de muitas formas diferentes.

Por favor, tente o seguinte arquivo de lote (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

Exemplo de saída:

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

Leitura Adicional

por 09.05.2016 / 13:27