# Windows Batch Scripting to edit contents of a file



## karthik.arch (Oct 31, 2011)

Hi, 
I believe this is the right place to post my query on Windows Batch Scripting. 

I have 2 files - MASTER and SLAVE. 

I want to delete a fixed range of lines from MASTER (say 2500-3500). Then,
I want to copy the same fixed range of lines from SLAVE (same 2500-3500) to the MASTER file.

Please help me out in this query and thanks in advance.


----------



## Squashman (Apr 14, 2005)

It is possible but I would advise against using Batch to accomplish this. Basically when a batch file works on a single file in a For Loop it loads the entire file into Memory. With your batch file you will need two For Loops nested inside each other. This will end up loading two files completely into memory. If both of your files are rather large you will run out of Memory and your batch file will abort. 

Using batch to process data like this is very slow. I know this for a fact. I have a batch utility that processes text files to append a value to the end of each line. It takes a long time to run.

Other scripting languages work much better than batch with performing a task like this. Even PowerShell would be a better solution. I was working on PowerShell solutions for many of my batch files a long time ago but I can't get my company to get everyone onto Windows 7 and I didn't want to have to worry about getting PowerShell installed on Everyones XP computer's not to mention the other extra registry settings we had to make to get things to work correctly with Drag and Drop. 

Python would also be a better solution for text file processing.


----------



## Ninjaboi (Dec 27, 2010)

I agree with Squashman. I don't use Batch at all, so I was quite interested in hearing what was being said about the disadvantage of using it for this task. However, it should be relatively easy to do this in Python.


----------



## jcgriff2 (Sep 30, 2007)

```
[FONT=Lucida Console]Start:  Fri 11/04/2011     10:16:29.34     

End:    Fri 11/04/2011     10:19:19.39 [/FONT]
```
*2 min 50 secs* = the runtime for a batch script to add HTML tags (<p> </p>) to each line in a 362k text file. The output = 407k HTML file. The batch script has 1 FOR loop.

*A VBS script did the same task in just 2 seconds. *

Regards. . .

jcgriff2.

`


----------



## AlbertMC2 (Jul 15, 2010)

Hi

Taking a cue from jcgriff2 here is a VB script you can try:


```
Option Explicit
Dim objFSO, readlines, writelines
Dim toReadFile, toWriteFile
Dim i

Set objFSO = CreateObject("Scripting.FileSystemObject")

toReadFile = "readfile.txt"
toWriteFile = "writefile.txt"
 
readlines = Split(objFSO.OpenTextFile(toReadFile).ReadAll, vbNewLine)
writelines = Split(objFSO.OpenTextFile(toWriteFile).ReadAll, vbNewLine)

For i = 2500 To 3500 
  writelines(i-1) = readlines(i-1)
Next

objFSO.OpenTextFile(toWriteFile, 2).WriteLine Join(writelines, vbNewLine)
```
Where your MASTER file is the writefile.txt and your SLAVE file is the readfile.txt
Where i = 2500 To 3500 are the lines 2500-3500
(Note that the lines are written into an array that starts with writelines(0) and readlines(0) 
and that is why you use writelines(i-1) and readlines(i-1) )

Also note that text files are very small so you should not have a memory problem. The text files I used are about 400kilobytes in size and have 4500 lines of 80digit numbers. Since there are 2 files it will only read about 800kilobytes into memory - which is hardly anything.

Please make copies of your MASTER and SLAVE files before testing this.


----------

