Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello all! I was wondering if there's a way to parse text from text files in DOS
without using the iterative nature of the FOR command. I have a
delimited text file with over 72000 rows and all I would like to get from the
file is the year (2005 or 2006 in a different file) which is the same
all through out and is the first token in the file. I thought of using
the skip option but the file that would be provided to us on a monthly
basis will not have a set number of records (volatile). I would like to
use the year as part of the name of the raw files we archive. I would
appreciate any help you can give. Attached is the code I've written so far, which doesn't work :-(
REM @echo offset varNUM=0
set ROOT=C:\Membership Reporting\2005
set FILE=memrpt2.txtFOR /f "tokens=2-4 delims=/ " %%a in ('DATE/T') do SET DATE=%%c_%%a_%%b
for /f "usebackq tokens=1" %%a in ("%ROOT%\%FILE%") do call :PROC %%a
goto :ENDPROC:PROC
if not %varNUM%==1 (
set varYEAR=%1
set /a varNUM=%varNUM%+1
)
goto :EOF
:ENDPROC
@echo %varYEAR%
@echo Filename is %DATE%_memrpt_%varYEAR%.txt

It may help to post a chunk of the orig filr; maybe five lines. And an example of what the output file[s] should look like.
=====================================
If at first you don't succeed, you're about average.M2

Hello M2,
Thanks for your reply. If the file you're referring to is the raw delimited file, it looks something like this:
2005 *1 *9* Whatever * ....
2005 *1 *9* Whatever2 * ....
2005 *2 *3* Whatever2 * ....
2005 *2 *9* Whatever3 * ....The text file is delimited by a space and *. There can be up to more than 100K of lines at a time so obviously it wouldn't make sense to go over all the rows just to get 1 year value. All the year values should be the same for all records so going to the first line should be enough. Anyway, let me know if this is what you're referring to.
Thanks again!

I'm sure M2 will not be offended by my tip, so the following two lines of code should fit your need to catch the first line's year
Set /P varYEAR=<"your file name"
Set varYEAR=%varYEAR:~0,4%Post again if anything goes wrong.

Thanks for your reply, IVO. That trick worked without a hitch! M2, please still do post if you find any other way of doing it; a different method can always be useful at a later time.
Cheers you both!

Hi IVO,
I tested this code several times, although there seems to be a problem the variable retaining it's value. Here's the full code I wrote. Sorry if this is clunky, DOS scripting hasn't been my greatest strength.
set ORACLESID=MYORACLESERVER.ORG
set MBRNFILE=mbrrptn.txt
set MBRSFILE=mbrrpts.txt
set MBRFILE=mbrrpt.txt
set MBRCTL=mbrrpt.ctl
set MBRBAD=mbrrpt.bad
set MBRLOG=mbrrpt.log
set MBRSQL=mbrrpt.sql
set MBRTRSQL=mbrrpt_trunc.sql
set MBRSQLLOG=mbrrpt_sql.log
set ROOT=c:\jobs
set SUBFOLDER=membership_rpt
set LOGROOT=%ROOT%\LOG
set RAWROOT=%ROOT%\RAW
set BADROOT=%ROOT%\BAD
set CTLROOT=%ROOT%\CTL
set SQLROOT=%ROOT%\SQLrem -- date stamp the log
FOR /f "tokens=2-4 delims=/ " %%a in ('DATE/T') do set DATE=%%c_%%a_%%brem -- time stamp the log
FOR /f "tokens=1-4 delims=:." %%a in ('TIME/T') do set TIME=%%a_%%b_%%cIF EXIST %RAWROOT%\%MBRNFILE% (
IF EXIST %RAWROOT%\%MBRSFILE% (
copy /b %RAWROOT%\%MBRNFILE%+%RAWROOT%\%MBRSFILE% %RAWROOT%\%MBRFILE%
IF EXIST %RAWROOT%\%MBRFILE% (
set /p varYEAR=<"%RAWROOT%\%MBRFILE%"
set varYEAR=%varYEAR:~0,4%
sqlldr userid=bob/bob@%ORACLESID% control=%CTLROOT%\%MBRCTL% log=%LOGROOT%\%SUBFOLDER%\%MBRLOG% bad=%BADROOT%\%SUBFOLDER%\%DATE%_%MBRBAD%
IF EXIST %BADROOT%\%SUBFOLDER%\%DATE%_%MBRBAD% (
@echo ************** Membership Reporting - %DATE% - %TIME% ************** >> %LOGROOT%\%SUBFOLDER%\%MBRSQLLOG%
sqlplus -s /nolog @%SQLROOT%\%MBRTRSQL% >> %LOGROOT%\%SUBFOLDER%\%MBRSQLLOG%
sqlplus -s /nolog @%SQLROOT%\%MBRFAILSQL% >> %LOGROOT%\%SUBFOLDER%\%MBRSQLLOG%
@echo SQL Loader aborted due to constraint violation in membership file >> %LOGROOT%\%SUBFOLDER%\%MBRSQLLOG%
@echo Upload process aborted
) else (
@echo ************** Membership Reporting - %DATE% - %TIME% ************** >> %LOGROOT%\%SUBFOLDER%\%MBRSQLLOG%
sqlplus -s /nolog @%SQLROOT%\%MBRSQL% >> %LOGROOT%\%SUBFOLDER%\%MBRSQLLOG%
REM IF EXIST %RAWROOT%\%MBRNFILE% (
REM del %RAWROOT%\%MBRNFILE%)
REM IF EXIST %RAWROOT%\%MBRSFILE% (
REM del %RAWROOT%\%MBRSFILE%)
IF EXIST %RAWROOT%\%MBRFILE% (
@echo %varYEAR%
REM ren %RAWROOT%\%MBRFILE% %DATE%_%varYEAR%_%MBRFILE%
REM move %RAWROOT%\%DATE%_%varYEAR%_%MBRFILE% %RAWROOT%\%SUBFOLDER%
)
)
)
) else (
echo Raw file/s non-existent 2
)
) else (
echo Raw file/s non-existent 1
)
This is basically a proc that checks for raw files in the folders, merges the raw files, uploads to oracle using sql loader and executes a proc that transfers the data from the raw to the main table. Any help would be appreciated!

I must be having a senior moment. I still don't know what we're tryimh to do. LOL
BTW, I avoid resetting a built-in var, like DATE.
BTW #2, I had a 1967 912 5 speed.
*<)
=====================================
If at first you don't succeed, you're about average.M2

Well porschephile2k3,I agree, your code is not the babe I like to make passes with, but I think the trouble is due to the referencing to varYEAR inside a sequence block, i.e. a sequence of statements embraced by () like that following an IF EXIST.
You have to enable the delayed expansion of variables by coding at the start of the script
@Echo Off
SetLocal EnableDelayedExpansionthen replace each reference to %varYEAR% with !varYEAR!
The full theory of delayed expansion is beyond this fast tip, but what suggested should give away the troubles.
By the way while I loved blondies at high school, eventually I married a dark haired girl met at university.
M2, when young I was an ALFA ROMEO fan, do you remember Mrs Robinson and Duetto?

I dunno Duetto, but I know thw song 'Mrs Robinson'.
=====================================
If at first you don't succeed, you're about average.M2

"Duetto" is the the ALFA ROMEO spider Dustin Hoffman drives at crazy speed in the movie "The Graduate" (1967) on California's highway in his desperate challenge to break the marriage of his love (Mrs. Robinson's daughter).
Another car I loved was McQueen's Ford Mustang in "Bullit" (with a lovely Jacqueline Bisset).

I need to see "The Graduate"; always like Dustin Hoffman.
I had a 1961 Morgan +4
If I was young again [LOL] I'd get an ALFA and an XKE. Maybe even Jacqueline Bisset.
=====================================
If at first you don't succeed, you're about average.M2

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |