# Solved: Stip first line from text file



## andy l (Apr 18, 2007)

Hi 

I am trying to write a batch file that will allow me to delete/strip the first line of a text file then keep the rest of the file with a different name. 

I thought I had it working by using the following

for %%I in (%swpath%\DDSORTTEMP) do for /f "delims=, tokens=* skip=1" %%x in (%%I) do echo %%x >> %swpath%\DDSORTOUT

where DDSORTTEMP is the output from a piece of software with a header line on and DDSORTOUT is the input to another piece of software which can't handle the header line.

However for some reason the above syntax only works up on some files. It is fine on smaller files (1 -2 MB files) but when I try it on a larger file 70MB it fails to write.

I am sure that there must be a way to do this but can't figure it out. So any help is appreciated.


----------



## JohnWill (Oct 19, 2002)

Why not write a simple C++ or VB utility to do the trick? For text files, it should be pretty trivial.


----------



## ChuckE (Aug 30, 2004)

What not use the DOS *MORE <filename> +1* command?
That will strip the first line off the file.

Just take your output file, of whatever size, and run it through MORE to create a new output file.


----------



## JohnWill (Oct 19, 2002)

What a cute idea, and it works too! 

I must admit, I never thought of that.


----------



## ghostdog74 (Dec 7, 2005)

you can just use the for loop with the "skip" option. type for /?

```
..
 FOR /F

Loop command: against a set of files - conditionally perform a command against each item.

Syntax
        FOR /F ["options"] %%parameter IN (filenameset) DO command 
      
        FOR /F ["options"] %%parameter IN ("Text string to process") DO command
		
Key   
   options:
      delims=xxx   The delimiter character(s) (default = a space)

      [COLOR="Red"]skip=n     [/COLOR]  A number of lines to skip at the beginning of the file. 
                    (default = 0)
 
.....
```


----------



## JohnWill (Oct 19, 2002)

I think Chuck's idea is way more efficient! That batch job would take FOREVER to run on a large text file. Note the reference to a *70mb* text file!


----------



## Squashman (Apr 4, 2003)

I was going to suggest a native port of the unix head command for windows. I didn't realize MORE could do the same thing.


----------



## JohnWill (Oct 19, 2002)

It's pretty slick, and I too never thought of MORE when this issue was mentioned.


----------



## andy l (Apr 18, 2007)

Thanks for all the replies. I will give the MORE command a try and let you know if this resolves my issue.


----------



## wburlison (Apr 24, 2007)

Very nice. Thanks for taking us back to the basics. I am splitting an 8GB file. Oops...It stops at 65538 lines.


----------



## ChuckE (Aug 30, 2004)

Hummmm, probably any DOS command is going to have problems with such a large text file (8GB!), which could easily have millions of lines in it. DOS quite often is going to have limits like the just discovered 65535 (0xFFFF, or there-abouts) lines.

Perhaps, this should be a two step solution, first separate off the first dozen or so lines, then use the *MORE* command to separate off that first line, then use the *COPY* command to merge the remainders (that first now eleven lines and all of the second part of the first separation).

Clumsy, huh.


----------



## JohnWill (Oct 19, 2002)

I think I'm back to writing a simple C++ application to do the trick.


----------



## andy l (Apr 18, 2007)

Thanks for the suggestions. Unfortunately the MORE command did stop at 65535 lines of the file out of 120,000+ lines.  

Any other suggestions will be helpful otherwise looks like I will have to ask one of my colleagues if they can write me something in C++ or the like, as I am unfortunately untested in coding.


----------



## JohnWill (Oct 19, 2002)

Ask and you shall receive.  I just hacked this together here, but it does the intended function, and should work for any sized file. It's compiled under Visual Studio 2003 C++, but is easily ported to almost any environment, it's really simple. The EXE file is there as well.

It accepts a single argument on the command line of the file name to be processed. It copies the file to a temp file minus the first line, then removes the original and renames the temp file to the original name. As written, it's only good for lines up to about 253 characters long, that could be changed as well.

The ZIP file is attached...


----------



## andy l (Apr 18, 2007)

Superb. Thanks that works brilliantly without any changes with 677 character long lines!

Many Thanks for all your help.


----------



## JohnWill (Oct 19, 2002)

Actually, if you have really long lines, I suggest this one. It handles lines up to 2048 characters long. The other one truncates any line longer than 255 characters. It was just habit on my part using 255, I don't normally run across text files with longer lines.


----------

