Solved

batch file to read and parse multiple text files

Posted on 2014-03-03
10
2,997 Views
Last Modified: 2014-03-08
I have a folder which contains few text files and they are of format as attached below.
I want to write a batch file which will read files in a folder one at a time and print first name and last name from file. I want to have the first name and last name stored in two variables before printing.
0
Comment
Question by:michaelm702
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 39902308
No file attached , also are you open to using powershell to accomplish this task  ?
0
 

Author Comment

by:michaelm702
ID: 39902361
sorry have attached it now.
I would like to do this using windows batch file.
1.txt
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39905055
So you want to read one file per run only? I assume you provide that file name as parameter of the batch when calling:
@echo off
setlocal EnableDelayedExpansion
for /F "skip=1 tokens=1,2 delims=| " %%A in (%1) do (
  set givenname=%%A
  set surname=%%B
  echo, !givenname! !surname!
)

Open in new window

You have to use those vars in the loop, as their values will be overwritten in each go, of course. After the loop they will have the last row's values.
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 54

Expert Comment

by:Bill Prew
ID: 39905297
Give this a try, it will find all files in the specified folder, then read each data line form each file, and display it as well as setting variables.  It also strips off any surrounding spaces but will allow multi word first or last names.

I have it displaying the file name being processed to for testing.

@echo off
setlocal EnableDelayedExpansion

set BaseDir=B:\EE\EE28379426\Files

for %%F in ("%BaseDir%\*.*") do (
  echo File:%%~F
  for /f "usebackq skip=1 tokens=1-2 delims=|" %%A in ("%%~F") do (
    call :Trim %%~A
    set FirstName=!Trim!
    call :Trim %%~B
    set LastName=!Trim!
    echo [!FirstName!] [!LastName!]
  )
)

:Trim [string]
  set Trim=%*
  exit /b

Open in new window

~bp
0
 

Author Comment

by:michaelm702
ID: 39914552
Thanks billprew it works perfect. Just want to know what I should do if I dont want to process rows that start with "#" thats the commented rows?
Had a question if I have to edit the file after processing and keep commenting the rows processed can this be done? I mean after I read first name and last name for 1st row and when I move to next row can I just comment the first row with "#" ?
0
 
LVL 54

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39914621
This should skip any line that has a "#" in the first character.

Updating the file at the same time you are reading it would be quite a bit harder, since you would actually have to create a new file while reading the old file, updating lines as needed.  Then after reading the whole old file, you would have to delte it and rename the new file to it's old name.  Could be done but is a bit more risky and would take a bit more code.

@echo off
setlocal EnableDelayedExpansion

set BaseDir=B:\EE\EE28379426\Files

for %%F in ("%BaseDir%\*.*") do (
  echo File:%%~F
  for /f "usebackq skip=1 tokens=1-2 delims=|" %%A in ("%%~F") do (
    set Line=%%~A
    if "!Line:0,1!" NEQ "#" (
      call :Trim %%~A
      set FirstName=!Trim!
      call :Trim %%~B
      set LastName=!Trim!
      echo [!FirstName!] [!LastName!]
    )
  )
)

:Trim [string]
  set Trim=%*
  exit /b

Open in new window

~bp
0
 

Author Comment

by:michaelm702
ID: 39914717
thanks. so how do i add the current row to new file do i just redirect?
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39915363
Yes, you can ECHO the current values on the line to a new file using >> for redirection (appending).

~bp
0
 

Author Comment

by:michaelm702
ID: 39915391
worked perfect.. only changed the if condition as below to skip commented rows..
if "!Line:~0,1!" NEQ "#"
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question