Solved

batch file to read and parse multiple text files

Posted on 2014-03-03
10
2,917 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 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 69

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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 53

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 53

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 53

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
A short article about problems I had with the new location API and permissions in Marshmallow
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

830 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