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.
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.
Does the report name start in column 1?
What signals the end of the report name?
What signals the end of the report name?
ASKER
Thanks Scott, I will take a look at Winbatch.
ASKER
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.
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"
)
@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"
)
ASKER
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You could need to change line 6 by:
CALL :ReadIt "%%~fA"
CALL :ReadIt "%%~fA"
A variation:
script: re_name.bat
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).
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"
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Like it.... I suppose you should change the file check to *. and add .txt onto the rename though:
When you say you ftp them to the dir, that may well be scriptable too if you want to?
@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"
)
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
~bp
@Bill
set /P Line1=<"%%~A" = very cool :)
Cheers,
Rene
set /P Line1=<"%%~A" = very cool :)
Cheers,
Rene
Thanks Rene.
~bp
~bp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks to everyone, all were great recommendations.
Welcome.
~bp
~bp
Glad I could help.
Cheers,
Rene
Cheers,
Rene
It's not free but it's not too expensive either.
It's a fairly easy script maker that is very powerful.