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 9226583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:51:59+00:00 2026-06-18T04:51:59+00:00

I have never done any batch scripting before and so I need help in

  • 0

I have never done any batch scripting before and so I need help in building one of them.
We have a file say “GetHistory.bat” and it accepts a parameter ID. The output of the script is as shown below:

Activity Name      Status      Date
----------------------------------------
Act1              Created    1-Jan-2013
Act2              Submitted  2-Jan-2013 
Act3              Approved   2-Jan-2013

Now the problem is I need to export the output in txt as CSV without the header and parameter ID added to each line as below:

1001,Act1,Created,1-Jan-2013
1001,Act2,Submitted,2-Jan-2013 
1001,Act3,Approved,2-Jan-2013

Any help to begin the script would be highly appreciated.
Thanks…!

  • 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-06-18T04:52:00+00:00Added an answer on June 18, 2026 at 4:52 am

    With the assumption that the output of GetHistory.bat has been redirected into a file called history.txt, we could feed that into our new batch file, ParamCSV.bat, like so, with this result:

    C:\stackoverflow>ParamCSV.bat 1001 < history.txt
    1001,Act1,Created,1-Jan-2013
    1001,Act2,Submitted,2-Jan-2013
    1001,Act3,Approved,2-Jan-2013
    

    To put together a quick script for this, I’ve referenced info from:

    • Read stdin stream in a batch file
    • What is the best way to do a substring in a batch file?
    • How to remove trailing and leading whitespace for user-provided input in a batch file?
    • DOS Batch – Function Tutorial provided a thorough overview of parameter passing in functions.

    I came up with this batch script, ParamCSV.bat:

    @echo off
    :: ParamCSV.bat
    ::
    :: Usage: ParamCSV.bat <Parameter_ID> < history.txt
    ::
    :: Thanks to:
    :: https://stackoverflow.com/questions/6979747/read-stdin-stream-in-a-batch-file/6980605#6980605
    :: https://stackoverflow.com/questions/636381/what-is-the-best-way-to-do-a-substring-in-a-batch-file
    :: https://stackoverflow.com/questions/3001999/how-to-remove-trailing-and-leading-whitespace-for-user-provided-input-in-a-batch
    
    :: Copy input parameter to 'id'
    set id=%1
    
    setlocal DisableDelayedExpansion
    
    for /F "skip=2 tokens=*" %%a in ('findstr /n $') do (
      set "line=%%a"
      setlocal EnableDelayedExpansion
      set "line=!line:*:=!"
      set "activity=!line:~0,17!"
      call:trim !activity! activity
      set "status=!line:~17,11!"
      call:trim !status! status
      set "date=!line:~28,11!"
      call:trim !date! date
      echo(!id!,!activity!,!status!,!date!
      endlocal
    )
    goto:EOF
    
    ::function: trim
    ::synopsis: Removes leading and trailing whitespace from a sting. Two
    ::          parameters are expected. The first is the text string that
    ::          is to be trimmed. The second is the name of a variable in
    ::          the caller's space that will receive the result of the
    ::          trim operation.
    ::
    ::usage:    call:trim string_to_trim var_to_update
    ::             e.g.   call:trim %myvar1% myvar2
    
    ::trim left whitespace
    setlocal
    set input=%~1
    for /f "tokens=* delims= " %%a in ("%input%") do set input=%%a
    ::trim right whitespace (up to 100 spaces at the end)
    for /l %%a in (1,1,100) do if "!input:~-1!"==" " set input=!input:~0,-1! 
    ::return trimmed string in place
    endlocal&set "%~2=%input%"
    

    There are a number of assumptions that are made here, and if any of them change or are invalid, the script will break:

    • The output of GetHistory.bat has fixed-width columns, of width 17,11, and 11. You didn’t provide an example of a two-digit day, so I’ve assumed the dates are right-aligned.
    • There are two header lines, which we skip in the for statement.
    • All output lines are for the same ID, so only one input parameter is expected, and it is the first element in all CSV output lines.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've done some small batch files before, but never any that required file names
I have never done any Singleton class before and now I figured that for
I have never done any Windows coding and I would like to give it
I have a quick pro-con question for you. I've never done any reporting within
I have no clue of where to start on this. I've never done any
I've never done this before. The website I have just built is the second
I have done XML parsing before but never on a massive scale. If I'm
I have never really done any serious web programming, other than just a blog
I've never done any encryption before, but I've been all over MSDN and google.
I have never done any testing in javascript. I know, I know. But the

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.