Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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
.....
20081001I 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

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 FileI only Batch if possible, 2000 more lines of code, oh well.

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

There is no for /f n DOS.
Nor SKIP etc.
=====================================
If at first you don't succeed, you're about average.M2

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.

LOL It's straightforward in NT.
=======================================
@echo off > #
setLocal EnableDelayedExpansionfor /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

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.

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

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.

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

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