[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

batch file to read and parse multiple text files

Posted on 2014-03-03
10
Medium Priority
?
3,269 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 71

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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 57

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 57

Accepted Solution

by:
Bill Prew earned 2000 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 57

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Introduction to Processes
Starting up a Project

656 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