user3146967
user3146967

Reputation: 23

Batch delete lines in text files

I have a folder of several hundred text files (file extension is actually .pro, but they are text format files that respond in the same way as .txt), variously named. They all contain these lines, in the middle of the file:

[HotkeysActive]
Hotkey22=1
Hotkey23=1
Hotkey24=1
Hotkey1=1
Hotkey2=1  ..... etc.

which I am trying to batch process so that they all read:

[HotkeysActive]
Hotkey1=1
Hotkey2=1  ..... etc.

i.e. delete lines the lines Hotkey22=1, Hotkey23=1 & Hotkey23=1

Not being very fluent at this, and not finding any thread here that provided a working answer, I enlisted the help of the software 'InfoRapid Search & Replace', but the best I can arrive at is this:

[HotkeysActive]



Hotkey1=1
Hotkey2=1  ..... etc.

Ideally, I now want those blank lines deleted, to achieve the second example: could anyone kindly help me with a batch or vbs script that would either delete the three blank lines after partial processing, or delete the three lines Hotkey22=1, Hotkey23=1 & Hotkey23=1 in the first example?

Many thanks,

Martin

Upvotes: 0

Views: 1578

Answers (3)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200273

The files look like they're in INI file format, so you could parse each file into a dictionary of dictionaries:

Set fso = CreateObject("Scripting.FileSystemObject")

For Each f In fso.GetFolder("C:\some\folder").Files
  If LCase(fso.GetExtensionName(f)) = "pro" Then
    Set data = ParseIni(f.Path)
    ...
  End If
Next

remove the obsolete entries:

data("HotkeysActive").Remove("Hotkey22")
data("HotkeysActive").Remove("Hotkey23")
...

and write the modified data back to the file:

Set output = f.OpenAsTextStream(2)
For Each key In data.Keys
  output.WriteLine "[" & key & "]"
  For Each val In data(key).Keys
    output.WriteLine val & "=" & data(key)(val)
  Next
  output.WriteLine
Next
output.Close

Upvotes: 0

user3150385
user3150385

Reputation:

Text = Replace(Text, vbcrlf, "") Text = Replace(Text, chr(13), "")

Upvotes: 0

Magoo
Magoo

Reputation: 80023

@ECHO OFF
SETLOCAL
FOR %%i IN (*.pro) DO (
 TYPE "%%i"|FINDstr /l /v "Hotkey22=1 Hotkey23=1 Hotkey24=1" >"%%~ni.new"
)
GOTO :EOF

This should process your files producing samename.new

I'd suggest you test it against a small subset first...

Upvotes: 1

Related Questions