Link to home
Start Free TrialLog in
Avatar of MGradoia66
MGradoia66

asked on

Parsing text files

Hello,

By using FTP, I copy a dozen or so reports from a UNIX print spooler to my c:\temp folder.  I then give them all a .txt extension so I can read them.

I would like to find a way programatically to read/parse the file and extract the report name (which appears on the left of the first line of the file) and rename the file the report name (such as territory sales.txt, returns.txt, orders booked.txt.....).

Could this be done in just DOS?

Thanks in advance.
Avatar of Scott C
Scott C
Flag of United States of America image

WinBatch...www.winbatch.com  could do this.

It's not free but it's not too expensive either.

It's a fairly easy script maker that is very powerful.
Does the report name start in column 1?
What signals the end of the report name?
Avatar of MGradoia66
MGradoia66

ASKER

Thanks Scott, I will take a look at Winbatch.
Hi Ramrom,

The name starts at the top left of the file.  I guess you can say a blank space would signal the end of the name.
"guess" is not accurate enough. Please post a sample first line.
Yes please post a sample but you can read the file easily using a for command, and another for command nested around that to find the files.  No point writing more till we see the details though:

@echo off
setlocal enabledelayedexpansion
cd /d c:\temp
for /f %%a in ('dir /b /a-d *.') do (
   rem another for loop to get part of first line of file into "name"
   rename "%%~a" "%name%.txt"
)
Hi All,

Please find attached a copy of an example.  In this case I would want to parse the file and name it Territory.Sales.txt

Thanks.
TERRITORY.SALES.txt
SOLUTION
Avatar of ReneGe
ReneGe
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You could need to change line 6 by:
CALL :ReadIt "%%~fA"
A variation:

script: re_name.bat
@echo off
setlocal enabledelayedexpansion
set /a counter=0
for /f "usebackq delims=" %%a in (%1) do (
if "!counter!"=="1" goto gotline
set line="%%a"
set /a counter+=1
)
:gotline
for /f %%n in (%line%) do set name=%%n
echo rename "%1" "%name%.txt"

Open in new window

Call like this:
re_name testfile.txt

and it will (echo) this:
rename "testfile.txt" "TERRITORY.SALES.txt"

To have it rename, remove the echo in the last line of the script. Note that testfile.txt is your TERRITORY.SALES.txt file.

Substitute the above for the 2 lines in Steve's script (the rem and rename lines).
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Like it.... I suppose you should change the file check to *. and add .txt onto the rename though:

@echo off
setlocal EnableDelayedExpansion
set BaseDir=c:\temp
for %%A in ("%BaseDir%\*.") do (
  set /P Line1=<"%%~A"
  for /F "tokens=1" %%B in ("!Line1!") do ren "%%~A" "%%B.txt"
)

Open in new window


When you say you ftp them to the dir, that may well be scriptable too if you want to?
Ah, I read it that he renamed them to TXT first, then wanted this step, but I see how it could be the other way round too.  As you point out Steve, easy enough to do the TXT part at the same time.

~bp
@Bill
set /P Line1=<"%%~A"  =  very cool :)

Cheers,
Rene
Thanks Rene.

~bp
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks to everyone, all were great recommendations.
Welcome.

~bp
Glad I could help.

Cheers,
Rene