1

I want to read a csv file and store a value in a variable and use that variable value to be written as a last column of the same file.

@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

I want the last read value which is in three.txt to be stored in a variable and use the same to be print a column in the same file. Below is the data in 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

I had read the date 01/12/15 from the combined.csv I want to store it in a variable and store the same date for all the records as a last column.

1
  • I had read the date 01/12/15 from the combined.csv I want to store it in a variable and store the same date for all the records as a last column. Does it makes sense now? – user2500677 May 9 '16 at 10:46
0

I want to read the date from combined.csv and add it as a new last column to a file three.txt.

Your question is not very clear (even after several edits) and your batch file is broken in many different ways.

Please try the following batch file (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

Example output:

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

Further Reading

5
  • @user2500677 Do you need more help? If this was helpful to you and answered your question, please don't forget to accept it. – DavidPostill May 10 '16 at 8:38
  • Can this be done to a set of csv files in a folder? – user2500677 May 11 '16 at 5:41
  • @user2500677 Yes. – DavidPostill May 11 '16 at 7:11
  • Find below script and correct me where I am wrong. @echo off setlocal if exist three.txt del three.txt set cnt=1 rem get the date "01/12/15" for %%i in (*.csv) do ( if cnt>=1 ( for /f "skip=5 tokens=1-10 usebackq delims=," %%a in (type "%%i") 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 "%%i") do ( echo %%a,%_date%>> three.txt ) ) set /a cnt+=1 ) endlocal – user2500677 May 11 '16 at 7:53
  • @user2500677 This is a new question. You should accept this answer and ask a new question. You can reference this one for context if it helps. – DavidPostill May 11 '16 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.