1

How to store a value in a variable and load that variable as a new column in a file using a batch script?

The above link is used to read a date field and write it to all the records in a csv file. I want it to be done for a set of csv files in a folder. Below is the script for the requirement, I want to know what is the mistake in the below script

@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
0

I want it to be done for a set of csv files in a folder

Again, your batch file is broken in many different ways.

You need to learn how to debug your own batch files:

Rather than spend time debugging your batch file, it is quicker for me to modify the batch file from my answer to the linked question to answer your new question.

Use the following batch file (example.cmd):

@echo off 
setlocal 
if exist three.txt del three.txt 
for /r %%i in (*.csv) do ( 
  call :process_file %%i
  )
goto :eof

:process_file
  echo processing %1
  for /f "skip=5 tokens=1-10 usebackq delims=," %%a in (`type %1`) 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 %1`) do ( 
    echo %%a,%_date%>> three.txt 
    )
  ) 
endlocal

How do I write the results to the same csv file instead of the three.txt?

Use the following batch file (example.cmd):

@echo off 
setlocal 
for /r %%i in (*.csv) do ( 
  call :process_file %%i
  )
goto :eof

:process_file
  echo processing %1
  for /f "skip=5 tokens=1-10 usebackq delims=," %%a in (`type %1`) 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 %1`) do ( 
    echo %%a,%_date%>> three.txt 
    )
  )

  rem rename the file
  del %~nx1
  ren three.txt %~nx1 
endlocal

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • call - Call one batch program from another, or call a subroutine.
    • del - Delete one or more files.
  • for /f - Loop command against the results of another command.
  • for /r - Loop through files (Recurse subfolders).
  • goto - Direct a batch program to jump to a labelled line.
  • parameters - A command line argument (or parameter) is any value passed into a batch script.
  • ren - Rename a file or files.
  • type - Display the contents of one or more text files.
2
  • Thank u so much @DavidPostill. Request doing it in another way is how to store it in the same csv file instead of the three.txt? – user2500677 May 11 '16 at 9:42
  • @user2500677 Please don't keep changing the question. I've updated the answer for you this time. – DavidPostill May 11 '16 at 10:06

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.