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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:24:27+00:00 2026-06-05T19:24:27+00:00

I want to write a batch file to read an input text file, extract

  • 0

I want to write a batch file to read an input text file, extract information from it put it in an output file.

  1. Each line in the input file has different information so I want the batch to loop through each line.
  2. I want to extract certain information from the input file and discard the rest.
  3. The input file can have any number of lines.
  4. I want to leave an error message if the input line has no useful information.
  5. I need two counters, the first telling the number of lines in the input file, and the second telling the number of lines in the output file (without counting empty lines) .
  6. I want the batch to treat special characters like normal letters.

For example:

FILE_NAME=apple FILE_SIZE=312     C=fwef sdf asdetg

FILE_SIZE=7867 C=ehtrghr  FILE_NAME=sea&ocean G=tryr    yujg
C=gert FILE_NAME=chair=12 tgrgd sfsf FILE_SIZE=66
dfgg ertergf C=ert A=344

fgdfg FILE_NAME=cat

I want to extract only the FILE_NAME=XXX and FILE_SIZE=XXX part, discarding everything else in that line. The output should be:

-name apple -size 312
-name sea&ocean -size 7867
-name chair=12 -size 66
ERROR!!!
-name cat

 input_count=5 and output_count=4
  • 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-05T19:24:29+00:00Added an answer on June 5, 2026 at 7:24 pm

    People don’t give enough credit to batch scripts. Try this:

    @echo off
    setlocal enableDelayedExpansion
    set INPUT_FILE=input.txt
    set OUTPUT_FILE=output.txt
    set INPUT_COUNT=0
    set OUTPUT_COUNT=0
    
    :: Read the input file
    for /f "tokens=*" %%s in (%INPUT_FILE%) do call :ParseLine "%%s"
    echo. >>%OUTPUT_FILE%
    echo input_count=!INPUT_COUNT! and output_count=!OUTPUT_COUNT! >>%OUTPUT_FILE%
    exit /b
    
    :: Parse one line and write to output
    :ParseLine
    set "str=%~1"
    set ^"str=!str: =^
    
    !"
    set file_name=
    set file_size=
    for /f "eol=  tokens=1,2 delims==" %%a in ("!str!") do (
        set "tag=%%a"
        set "value=%%b"
        if "!tag!" equ "FILE_NAME" set "file_name=-name !value!"
        if "!tag!" equ "FILE_SIZE" set "file_size=-size !value!"
    )
    set /a input_count+=1
    if "!file_name!" equ "" if "!file_size!" equ "" (
        echo ERROR^^!^^!^^! >>%OUTPUT_FILE%
        goto Done
    )
    echo.!file_name! !file_size! >>%OUTPUT_FILE%
    set /a output_count+=1
    :Done
    

    Here’s a little explanation about what this does:

    1. The first for loop reads the file line by line, the contents of each line being sent as input arguments to the ParseLine subroutine.
    2. The ParseLine subroutine receives each line in str and iterates through its space-separated words (using jeb’s advanced method described here).
      The logic of the subroutine loop is pretty straightforward: it breaks each word into tag (the text that precedes =) and value (the text that follows =), and sets file_name and file_size accordingly.

    I believe that this method can handle pretty much any input file, with or without special characters.

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

Sidebar

Related Questions

I want to write a batch file that will take the contents of a
I want to write a batch file that performs the following operations: Check if
I want to write the list of file names from a Given directory path
I want to write a batch file that updates a DLL that is in
I want to write batch file for my own php framework. for example i
In my batch file I have the script: set myVar=/someLabel:%1 I want to write
I want to run a program with different configuration file, the program write with
I want to read 4GB file & create a copy of it by making
I never wrote a batch file, and now I have to write a batch
HI i want to implement this C code in batch file int i; scanf(%d,

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.