Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6057781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:31:36+00:00 2026-05-23T08:31:36+00:00

I am struggling to write a batch script which can read a CSV file

  • 0

I am struggling to write a batch script which can read a CSV file such as below

Name:, City:, Country:
Mark, London, UK
Ben, Paris, France
Tom, Athens, Greece

There will be a heading row in the CSV file. It should output to a text file as below:

Name:Mark
City:London
Country:UK

Name:Ben
City:Paris
Country:France

Name:Tom
City:Athens
Country:Greece

The field separator (:) in the above output is expected to be provided in the header row itself. So all that I need to do is concatenate the field heading and its value.

The number of columns in this CSV file is not fixed, so the script should not limit to 3 tokens. Kindly help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:31:36+00:00Added an answer on May 23, 2026 at 8:31 am
    @ECHO OFF
    IF "%~1"=="" GOTO :EOF
    SET "filename=%~1"
    SET fcount=0
    SET linenum=0
    FOR /F "usebackq tokens=1-10 delims=," %%a IN ("%filename%") DO ^
    CALL :process "%%a" "%%b" "%%c" "%%d" "%%e" "%%f" "%%g" "%%h" "%%i" "%%j"
    GOTO :EOF
    
    :trim
    SET "tmp=%~1"
    :trimlead
    IF NOT "%tmp:~0,1%"==" " GOTO :EOF
    SET "tmp=%tmp:~1%"
    GOTO trimlead
    
    :process
    SET /A linenum+=1
    IF "%linenum%"=="1" GOTO picknames
    
    SET ind=0
    :display
    IF "%fcount%"=="%ind%" (ECHO.&GOTO :EOF)
    SET /A ind+=1
    CALL :trim %1
    SETLOCAL ENABLEDELAYEDEXPANSION
    ECHO !f%ind%!!tmp!
    ENDLOCAL
    SHIFT
    GOTO display
    
    :picknames
    IF %1=="" GOTO :EOF
    CALL :trim %1
    SET /a fcount+=1
    SET "f%fcount%=%tmp%"
    SHIFT
    GOTO picknames
    

    This batch scipt:

    • accepts one parameter, the name of the file to process;

    • does not verify the presence of : at the end of a header token, and when the values are displayed they are placed immediately after the corresponding header tokens;

    • trims all the leading spaces (but not the trailing ones);

    • considers the first row to be the header row, which also defines the number of tokens to process in subsequent rows;

    • supports up to 10 tokens, and the two areas highlighted in bold italics are responsible for that (so when you need to change the maximum number, modify both areas: if you increase the number, you must expand the "%%a" "%%b" "%%c" … list, and, likewise, if you decrease the number, then shrink the list).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm struggling to write a reg-ex that can do the following: Label1.Caption := Edit1.Text;
I need to write a script which merges a list with a dictionary to
I'm struggling to write a Windows Service that accesses a website, logs in using
I am struggling to write a query to result in the following records. I
Hi i'm struggling to write a particular MySQL Join Query. I have a table
I'm struggling with some SQL/php syntax issues here. I'm trying to write two functions.
I'm struggling with implementing the Bridge design pattern (or an alternative such as Adapter)
I am trying to write a method for a Compact Framework 2.0 dll which
I'm struggling to write Makefiles that properly build my unit tests. As an example,
Still struggling to understand what best practices are with respect to macros. I'm attempting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.