Computing.Net > Forums > Programming > How to break/exit FOR /F in DOS

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to break/exit FOR /F in DOS

Reply to Message Icon

Name: porschephile2k3
Date: April 24, 2007 at 23:19:23 Pacific
OS: Win2K
CPU/Ram: 2 GB
Product: DELL
Comment:

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 off

set varNUM=0
set ROOT=C:\Membership Reporting\2005
set FILE=memrpt2.txt

FOR /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



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: April 25, 2007 at 00:34:39 Pacific
Reply:

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



0

Response Number 2
Name: porschephile2k3
Date: April 25, 2007 at 13:26:34 Pacific
Reply:

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!



0

Response Number 3
Name: IVO
Date: April 25, 2007 at 14:00:27 Pacific
Reply:

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.


1

Response Number 4
Name: porschephile2k3
Date: April 25, 2007 at 14:56:29 Pacific
Reply:

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!


0

Response Number 5
Name: porschephile2k3
Date: April 25, 2007 at 16:10:23 Pacific
Reply:

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%\SQL

rem -- date stamp the log
FOR /f "tokens=2-4 delims=/ " %%a in ('DATE/T') do set DATE=%%c_%%a_%%b

rem -- time stamp the log
FOR /f "tokens=1-4 delims=:." %%a in ('TIME/T') do set TIME=%%a_%%b_%%c

IF 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!


0

Related Posts

See More



Response Number 6
Name: porschephile2k3
Date: April 25, 2007 at 20:58:51 Pacific
Reply:

Just an update, for some reason IVO's code doesn't work inside an IF EXIST statement... :-(



0

Response Number 7
Name: Mechanix2Go
Date: April 26, 2007 at 02:26:06 Pacific
Reply:

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



0

Response Number 8
Name: IVO
Date: April 26, 2007 at 09:23:47 Pacific
Reply:

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 EnableDelayedExpansion

then 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?


0

Response Number 9
Name: Mechanix2Go
Date: April 27, 2007 at 01:51:34 Pacific
Reply:

I dunno Duetto, but I know thw song 'Mrs Robinson'.


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 10
Name: IVO
Date: April 27, 2007 at 03:05:45 Pacific
Reply:

"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).


0

Response Number 11
Name: Mechanix2Go
Date: April 27, 2007 at 03:37:08 Pacific
Reply:

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



0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: How to break/exit FOR /F in DOS

How to remove end-of-line in a file www.computing.net/answers/programming/how-to-remove-endofline-in-a-file/10873.html

How to delete first for lines in a txt file www.computing.net/answers/programming/how-to-delete-first-for-lines-in-a-txt-file/20172.html

how to compile a .Reg file in VC++? www.computing.net/answers/programming/how-to-compile-a-reg-file-in-vc/10251.html