Solved

Delete line from text file using batch command

Posted on 2009-07-02
6
1,287 Views
Last Modified: 2012-08-14
Hi,

I have a batch file that creates a text doc from an AD query. Basically it produces a text file with a list of all hosts from particular OU's on the domain. The idea is that I can then use this list to run various scripts and commands against.

I wish to add a line in my batch file AFTER the creation of this doc that will allow me to remove given lines with me providing hostnames in the string. This is to exclude certain systems from whatever commands I run post this.

So, basically, does anyone know a DOS string that will search a text file for a text string and then delete any lines with that contain that string and save the modded text file?

Hope this makes sense!

Thanks,
0
Comment
Question by:bheroniphr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 125 total points
ID: 24762282
Hi,

you can use 'type' and 'find', i.e.:

> type in.txt | find /V "<search sting>" > out.txt

This will write all lines from 'in.txt' to 'out.txt' which don't contain the '<search string>'. To remove multiple strings just copy 'out.txt' to 'in.txt' and redo the same with another '<search string>'

Hope that helps,

ZOPPO
0
 
LVL 12

Expert Comment

by:Serge Fournier
ID: 24762430
try using WSH (windows host script) instead of .BAT
it have all the function to read a files and rewrite it without the unwanted line

lots of examples on internet as how to open file, read it, write it
i put a very crude example in my code snippet

crude cause it use two different file name and n ddo nott rename the files after to have the original file name


objfil04 = read
objfil05 = write

      Set objfil04 = objFSO.OpenTextFile("c:\aef\" & fil02 & "_aef.csv", 1, 0) 
      Set objfil05 = objfso.OpenTextFile("c:\aef\" & fil02 & ".csv", 2, true)
      a = objFil04.readLine
if instr(a,"i dont want this line") = 0 then      
   objFil05.Write a
end if
 
      objfil05.close
      objfil04.close

Open in new window

0
 
LVL 12

Expert Comment

by:Serge Fournier
ID: 24762446
i also forgot a:

do while objfil04.AtEndOfStream <> True
loop

and a objfso definition object

Set objfso = CreateObject("Scripting.fileSystemObject")      
Set objfil04 = objFSO.OpenTextFile("c:\aef\" & fil02 & "_aef.csv", 1, 0) 
      Set objfil05 = objfso.OpenTextFile("c:\aef\" & fil02 & ".csv", 2, true)
do while objfil04.AtEndOfStream <> True      
   a = objFil04.readLine
   if instr(a,"i dont want this line") = 0 then      
      objFil05.Write a
   end if
loop
 
      objfil05.close
      objfil04.close

Open in new window

0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 43

Expert Comment

by:Steve Knight
ID: 24763354
If you use findstr instead of find you can do it with one line:

findstr /v "pc1 pc2 pc3" yourlist.txt > newlist.txt

Steve
0
 

Author Closing Comment

by:bheroniphr
ID: 31599135
Exactly what I was looking for, short and sweet.

Thanks to the others but I did need something that could be embedded in my existing Batch and this solution was also much shorter and cleaner than the others suggested.

Thanks to all.

B
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24765312
You didn't like the easy option ofusing one command then?

Steve
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question