Solved

batch file to read and parse multiple text files

Posted on 2014-03-03
10
2,769 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
10 Comments
 
LVL 28

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 68

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
 
LVL 51

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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 51

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 51

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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 …

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now