Computing.Net > Forums > Programming > DOS for loop

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.

DOS for loop

Reply to Message Icon

Name: tanners
Date: November 12, 2008 at 14:13:32 Pacific
OS: win2003
CPU/Ram: 1GB
Product: HP
Comment:

Hi,

I have a DOS For loop that goes through a file I named test.txt. Within this file, it contains a series of dates:

20081112
20081111
20081110
.....
20081001

I would like to write a For loop that will delete the first value (20081112) from this text file. I am not sure how I can structure the For loop just to delete this value from the txt file. Thanks



Sponsored Link
Ads by Google

Response Number 1
Name: BatchFreak
Date: November 12, 2008 at 16:10:36 Pacific
Reply:

Im not familiar with the FOR command, well not good... But couldnt you use

FOR "Skip=1" %%a IN (Old Txt file) DO ECHO %%a>>new text file
DEL Old Txt File
RENAME new text file Old Txt File

I only Batch if possible, 2000 more lines of code, oh well.


0

Response Number 2
Name: Judago
Date: November 13, 2008 at 00:12:43 Pacific
Reply:

BatchFreak,

I can see your trying but there are a few things wrong with your script.

1. The for loop only takes the skip, delims, tokens, usebackq and eol options when the /f switch is used.

2. The skip option tells the for loop how many lines should be ignored before it starts parsing the text, so skip=1 means the output will start at the second line.

3. You file names have spaces and in general need to be surrounded by quotes, to do this with a for /f loop you also need to specify the usebackq option so the input is interpreted as a file name and not a literal string. If you don't add quotes your file name would be interpreted as a file set; 3 files named old, txt and file.

4. I find it's generally better to move one file "ontop" of another file rather than delete one and rename another, but this is more preference than anything else.

I don't mean to discourage or "flame" you, just trying to give a little info.


tanners,

What data makes up the rest of the line and what seperates the date from the rest of the data?(possibly a space?) Does the line contain any of the symbols >,<,|,&,!,%,",*,(,) or ^? Does this file contain a header and/or footer? If so what are they?

If it doesn't contain any of the above, the date and data are seperated by a space and the file doesn't contain a header or footer the below script *may* work.


for /f "usebackq tokens=1* delims= " %%a in ("your text file.txt") do (
if not "%%b"=="" echo %%b>>"new text file.txt"
)

Without knowing more about your file I can only guess...


0

Response Number 3
Name: Mechanix2Go
Date: November 13, 2008 at 00:27:09 Pacific
Reply:

There is no for /f n DOS.

Nor SKIP etc.


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

M2


0

Response Number 4
Name: Judago
Date: November 13, 2008 at 01:33:44 Pacific
Reply:

M2,

I know your right; this one seems to be one of those

"but isn't the command prompt in windows called DOS?" 

The OS is listed as win2003 and DOS for loops can't read files either.

"I have a DOS For loop that goes through a file I named test.txt."
and
"I would like to write a For loop that will delete the first value (20081112) from this text file."

When people hear it enough they generally stop calling it DOS, it's just a pity they don't all do it at once.


0

Response Number 5
Name: Mechanix2Go
Date: November 13, 2008 at 03:46:08 Pacific
Reply:

LOL It's straightforward in NT.

=======================================
@echo off > #
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (test.txt) do (
set /a N+=1
if !N! neq 1 echo %%a >> #
)
copy # test.txt > nul & del #


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

M2


0

Related Posts

See More



Response Number 6
Name: BatchFreak
Date: November 13, 2008 at 06:28:35 Pacific
Reply:

Oh and the reason i used skip was because, what i got from his question was how to remove the first line. Thanks again. :D

I only Batch if possible, 2000 more lines of code, oh well.


0

Response Number 7
Name: Judago
Date: November 13, 2008 at 11:46:19 Pacific
Reply:

BatchFreak,

You are right; I can't believe I missed that. Anyway M2's solution is better because skip includes blank lines. So for example if the file contained 5 blank lines at the start anything less than skip=6 would pick up the first line that contains text. It's a real pain sometimes....


0

Response Number 8
Name: BatchFreak
Date: November 13, 2008 at 14:24:56 Pacific
Reply:

I have learned so much just in this topic it is unbelievable thank you

I only Batch if possible, 2000 more lines of code, oh well.


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: DOS for loop

DOS - For Loop Problem www.computing.net/answers/programming/dos-for-loop-problem/17488.html

pipe into a dos for loop www.computing.net/answers/programming/pipe-into-a-dos-for-loop-/18566.html

DOS FOR loop www.computing.net/answers/programming/dos-for-loop/19222.html