delete a single line of a text file using bat

Microsoft Windows 7 home premium
January 14, 2010 at 16:42:11
Specs: Windows 7
Hi, I'm new to the forum but I was hoping someone can help me since I have found this website helpful before.

I was hoping to delete a single line of a text file thus moving all the other lines below it one up making something else the first line.

In this case the text file will have something such as...
3
5
5
4
and i was hoping after the program has run that the text file would have
5
5
4
and not

5
5
4
in other words i don't want a space i want the line to just disappear I'm fairly certain this is possible and if someone answers could you please explain the logic to the code thank you.


See More: delete a single line of a text file using bat

Report •


#1
January 14, 2010 at 17:13:58
S gets set to the line num to skip.

===============================
@echo off > newfile & setLocal enableDELAYedeXpansion

set /a S=1
set N=

for /f "tokens=* delims= " %%a in (myfile) do (
set /a N+=1
if !N! neq !S! (
>> newfile echo.%%a
)
)


=====================================
Helping others achieve escape felicity

M2


Report •

#2
January 14, 2010 at 20:15:10
if you KNOW the file will be small (less than about 38600 bytes) edlin can be used:

@goto :aaa
1d
e
:aaa
edlin /b %1<%0.bat >nul 2>nul
(or "hardwire" it as : edlin /b file.txt <%0.bat...)


Report •

#3
January 15, 2010 at 16:05:10
Thank you both of them worked.
If it's not to much trouble could you please explain the syntax both of you used some functions that i have never seen before as well as some syntax i haven't seen before.

Report •

Related Solutions

#4
January 15, 2010 at 16:36:12
See commants in context.

set /a S=1
::sets the num of the line to skip
set N=
:: clears N for a fresh start

for /f "tokens=* delims= " %%a in (myfile) do (
set /a N+=1
:: increments N to sync with line num
:: compares line num and num to skip... etc
if !N! neq !S! (
>> newfile echo.%%a
)
)

===================
Clear as mud, right?


=====================================
Helping others achieve escape felicity

M2


Report •

#5
January 15, 2010 at 16:40:04
yea it was thank you. sorry i just started batch programing a few months ago and don't spend much time on it so i'm still learning the basics thanks

Report •

#6
January 15, 2010 at 18:19:22
this is helpful (courtesy Judago):
(edited, sorry, the machinery MANGLED the
correct url supplied... trying again)
href=http//www.computing.net/howtos/show/batch-script-variable-expansion-win200-and-up/248.html


I assume you have looked at out from cmdline:
set /?
for /?
call /?

for edlin (sometimes handy, sometimes a pita) help:
edlin
?
q

the script:
:: the @ suppresses output, not really needed here
:: and the goto jumps over the edlin commands (which are
:: not batch commands at all.)
@goto :aaa
1d
e
:aaa
edlin /b %1<%0.bat >nul 2>nul

1d is edlin command to delete line 1,
e writes the file and exits
the batchfile pipes itself into edlin, which basically ignores
the first line (it generates an error inside edlin but the error is suppressed: 2>nul
(2> redirects error output, in this case nul)
hth


Report •

#7
January 16, 2010 at 03:45:53
To answer the initial question, this is another way to remove empty lines from a file, you'll need a Windows ported GREP for this:

C:\>type 123.txt
1
2
3

5
6

8
9

C:\>type 123.txt| grep .
1
2
3
5
6
8
9

C:\>


Report •

#8
January 17, 2010 at 02:17:42
You don't need grep to get out blank lines.

But that wasn't the point anyway.


=====================================
Helping others achieve escape felicity

M2


Report •

#9
January 17, 2010 at 04:43:17
Same can be done with FINDSTR indeed, don't need GREP, but I have that habit. Anyway, I read something about the final resulting containing lines, so the solution could be to do the same, then fix the empty lines issues, you may end up avoiding lots of lines of code, by 1 line ... that sort of was the point.

Report •

#10
March 17, 2010 at 10:02:25
Hi ,
The script worked fine if we give in file and out file names But I would appreciate if you could send me script that removes first row of all the text files in a given directory.
I have 5- 10 files with extension .csv and I would like to skip the header from the files

Report •

#11
March 17, 2010 at 11:48:38
@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=* delims= " %%f in ('dir /b *.csv') do (
for /f "skip=1 tokens=* delims= " %%a in (%%f) do (
>> newfile echo.%%a
)
move /Y newfile %%f
)


=====================================
Helping others achieve escape felicity

M2


Report •

Ask Question