• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3335
  • Last Modified:

batch file to read and parse multiple text files

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
michaelm702
Asked:
michaelm702
1 Solution
 
becraigCommented:
No file attached , also are you open to using powershell to accomplish this task  ?
0
 
michaelm702Author Commented:
sorry have attached it now.
I would like to do this using windows batch file.
1.txt
0
 
QlemoC++ DeveloperCommented:
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
Independent Software Vendors: 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!

 
Bill PrewCommented:
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
 
michaelm702Author Commented:
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
 
Bill PrewCommented:
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
 
michaelm702Author Commented:
thanks. so how do i add the current row to new file do i just redirect?
0
 
Bill PrewCommented:
Yes, you can ECHO the current values on the line to a new file using >> for redirection (appending).

~bp
0
 
michaelm702Author Commented:
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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now