Solved

Delete line from text file using batch command

Posted on 2009-07-02
6
1,272 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
6 Comments
 
LVL 30

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 11

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 11

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now