Go Back   Tech Support Forum > The IT Pro > Programming

User Tag List

Windows Batch Scripting to edit contents of a file

This is a discussion on Windows Batch Scripting to edit contents of a file within the Programming forums, part of the Tech Support Forum category. Hi, I believe this is the right place to post my query on Windows Batch Scripting. I have 2 files


Closed Thread
 
Thread Tools Search this Thread
Old 10-31-2011, 04:11 PM   #1
Registered Member
 
Join Date: Oct 2011
Posts: 1
OS: WinXP



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.
karthik.arch is offline  
Sponsored Links
Advertisement
 
Old 11-02-2011, 06:42 AM   #2
TSF Enthusiast
 
Squashman's Avatar
 
Join Date: Apr 2005
Location: 1265 Lombardi Ave.
Posts: 1,645
OS: All of the above



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.
Squashman is offline  
Old 11-02-2011, 07:50 AM   #3
TSF-Emeritus
 
Ninjaboi's Avatar
 
Join Date: Dec 2010
Location: Washington State, U.S.A.
Posts: 2,055
OS: Windows 7

My System


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.
__________________
Ninjaboi is offline  
Sponsored Links
Advertisement
 
Old 11-04-2011, 08:36 AM   #4
Administrator
Manager, Microsoft Support
Acting Manager, Security
BSOD Kernel Dump Expert
Microsoft Windows Insider MVP
 
jcgriff2's Avatar

Microsoft Most Valuable Professional
 
Join Date: Sep 2007
Location: New Jersey Shore
Posts: 34,338
OS: Windows 10, 8.1 + Windbg :)



Code:
Start:  Fri 11/04/2011     10:16:29.34     

End:    Fri 11/04/2011     10:19:19.39 
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.

`
jcgriff2 is offline  
Old 11-05-2011, 02:09 PM   #5
TSF Enthusiast
 
Join Date: Jul 2010
Location: SA
Posts: 1,930
OS: DOS 3.3



Hi

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

Code:
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.
AlbertMC2 is offline  
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
XP Internet Security 2012 Firewall Alert
Hello. Two days ago I got the virus XP Internet Security 2012 Firewall Alert. It does not let me open any file/folder/browser. When I try to google how to remove it, it redirects me to different websites. Please ...
lilbrat0326 Resolved HJT Threads 15 07-03-2011 07:49 PM
Some kind of redirect on computer (Monster Marketplace)
I don't know how this happened, but something has gotten into my computer that constantly redirects anything I click on. If I enter a search in the box, that does OK, but whenever I click on anything, there's a high likelihood that what I've clicked on won't come up - quite often, Monster...
Calgirl Resolved HJT Threads 198 05-23-2011 07:01 AM
Can someone help me see what's up?
I'm a real computer noob I don't know exactly what's going on but ill try to describe it, mostly I'm concerned about a keylogger though. The other problems are every now and then on Google I'll click a link and I'll get sent to some random website, sometimes outta nowhere a new tab will open up to...
soboman Virus/Trojan/Spyware Help 13 05-13-2011 03:20 PM
Virus and Malware Removal
When opening programs or searching the internet, every activity always causes my memory usage to peak to 100%. It wouldn't bother me if it didn't slow things down. My computer used to perform better. I have a pentium 4, 1.8 ghz with 1gig of ram. Google maps is a particular problem, it uses so much...
NWAngler Resolved HJT Threads 9 03-14-2011 08:18 AM

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is on
Smilies are on
[IMG] code is on
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Post a Question


» Site Navigation
 > FAQ
  > 10.0.0.2
Powered by vBadvanced CMPS v3.2.3


All times are GMT -7. The time now is 12:07 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
vBulletin Security provided by vBSecurity v2.2.2 (Pro) - vBulletin Mods & Addons Copyright © 2020 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.1.0 (Pro) - vBulletin Mods & Addons Copyright © 2020 DragonByte Technologies Ltd.
Copyright 2001 - 2018, Tech Support Forum

Windows 10 - Windows 7 - Windows XP - Windows Vista - Trojan Removal - Spyware Removal - Virus Removal - Networking - Security - Top Web Hosts