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.
MGradoia66Asked:
Who is Participating?
 
AlexPaceConnect With a Mentor Commented:
You could do the entire job with a scriptable FTP client.  Here is a Robo-FTP script that connects to the server, downloads a file, extracts the text up to the first space character as the new name and renames the file.  It processes all files on the server then logs off.

WORKINGDIR "c:\temp"
FTPLOGON "myserver.mydomain.com" /user="Username" /pw="secret"
:loop
FTPGETFILE "*" 
IFERROR GOTO done
RCVFILE %sitefile
READFILE %sitefile new_name /termseq=" "
SET new_name = new_name + ".txt"
RENAME %sitefile new_name
GOTO loop
:done
FTPLOGOFF

Open in new window

0
 
Scott CSenior Systems EnginerCommented:
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.
0
 
ramromconsultant Commented:
Does the report name start in column 1?
What signals the end of the report name?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
MGradoia66Author Commented:
Thanks Scott, I will take a look at Winbatch.
0
 
MGradoia66Author Commented:
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.
0
 
ramromconsultant Commented:
"guess" is not accurate enough. Please post a sample first line.
0
 
Steve KnightIT ConsultancyCommented:
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"
)
0
 
MGradoia66Author Commented:
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
0
 
ReneGeConnect With a Mentor Commented:
Inspired by Steve's code, I did the un-tested following.

@ECHO OFF
Setlocal EnableDelayedExpansion

CHDIR /D C:\Test
FOR /F "tokens=*" %%A IN ('DIR /b /a-d *.txt') do (
   CALL :ReadIt "%%A"
   ECHO [%%~fA] -^> [%NewName%.txt]
   REN "%%~fA" "%NewName%.txt"
) 
ECHO.
ECHO DONE
PAUSE
EXIT

:ReadIt
FOR /F "usebackq tokens=1" %%A IN (%1) DO (SET NewName=%%A & EXIT /b)

Open in new window

0
 
ReneGeCommented:
You could need to change line 6 by:
CALL :ReadIt "%%~fA"
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
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).
0
 
Bill PrewConnect With a Mentor Commented:
Here's a fairly short approach to it:

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

Open in new window

~bp
0
 
Steve KnightIT ConsultancyCommented:
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?
0
 
Bill PrewCommented:
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
0
 
ReneGeCommented:
@Bill
set /P Line1=<"%%~A"  =  very cool :)

Cheers,
Rene
0
 
Bill PrewCommented:
Thanks Rene.

~bp
0
 
MGradoia66Author Commented:
Thanks to everyone, all were great recommendations.
0
 
Bill PrewCommented:
Welcome.

~bp
0
 
ReneGeCommented:
Glad I could help.

Cheers,
Rene
0
All Courses

From novice to tech pro — start learning today.