Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is my script:

SET origfile="C:\Documents and Settings\user\Desktop\test1\before.txt" 
SET tempfile="C:\Documents and Settings\user\Desktop\test1\after.txt" 
SET insertbefore=4 
FOR /F %%C IN ('FIND /C /V "" ^<%origfile%') DO SET totallines=%%C 

<%origfile% (FOR /L %%i IN (1,1,%totallines%) DO ( 
  SETLOCAL EnableDelayedExpansion 
  SET /P L= 
  IF %%i==%insertbefore% ECHO( 
) >%tempfile% 
COPY /Y %tempfile% %origfile% >NUL 
DEL %tempfile% 

I saved this as run1.bat. After running, I have a problem with the format, it is out of order.

Original file:

header 1<--here got tab delimited format--><--here got tab delimited format--> 
header 2<--here got tab delimited format--><--here got tab delimited format-->
header 3<--here got tab delimited format--><--here got tab delimited format-->
details 1
details 2 


header 1<--tab delimited is missing--><--tab delimited is missing--> 
header 2<--tab delimited is missing--><--tab delimited is missing-->
header 3<--tab delimited is missing--><--tab delimited is missing-->

details 1
details 2 
share|improve this question
up vote 0 down vote accepted

If you have Perl installed, you can use the following command in a Command Prompt window:

perl -p -i -e "print qq(\n) if $. == 4" filename.txt

I'm sure you can do it with batch scripts but sometimes I think it just isn't worth the effort.

Update 1:

To make the above into a file that can be used like your bat file you could create a script file "" as follows

use strict;
use warnings;

open my $in, '<', 'before.txt'    or die "can't read before.txt because $!\n";
open my $out, '>', 'after.txt'    or die "can't write after.txt because $!\n";    

while (<$in>) {
  print $out $_;
  print "\n" if $. == 4;

close $out;
close $in;

unlink 'before.txt'               or die "can't delete before.txt because $!\n";
rename 'after.txt', 'before.txt'  or die "can't rename temp file because $!\n";


Update 2:

When you install the Perl interpreter, it should automatically associate the .pl filename extension with Perl scripts to be run by the Perl interpreter. You should be able to confirm this with assoc .pl, If it says ".pl=perlscript", check ftype perlscript.

The installer should also add the perl interpreter's location to the command search PATH used by Windows' Command-Prompt.

Perl scripts should be saved in files with a filename extension of .pl.

If all the above is OK, you can run perl scripts by just typing their filename otherwise you have to explicitly tell Windows what interpreter to use: perl or in the worst case C:\strawberry\perl\bin\perl

share|improve this answer
thanks for ur reply RedGrittyBrick. i save this script as run2.bat and it come out with error msg ('perl' is not as an internal or external command. this is my script <code>perl -p -i -e "print qq(\n) if $. == 4" C:\Documents and Settings\Mun Kar\Desktop\test1\file1.txt pause <code> – newbie18 Sep 13 '11 at 8:59
@newbie: Sorry, you need to download and install the Perl interpreter before you can run Perl scripts. What I quoted was a command but it is not hard to make it into the equivalent of a bat file, I'll edit the answer. – RedGrittyBrick Sep 13 '11 at 9:03
is Perl interpreter is freeware or i need to pay before use? – newbie18 Sep 13 '11 at 9:09
The Perl interpreter is free, there is nothing to pay. If you were in a commercial business context where you needed a commercially supported product, you could buy support from Activestate - they also provide a free version. – RedGrittyBrick Sep 13 '11 at 9:18
after running, still have error come out '#!perl' is not as an internal or external command. after installed this software – newbie18 Sep 13 '11 at 9:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.