Computing.Net > Forums > Windows XP > Batch file search and delete help

Batch file search and delete help

Reply to Message Icon

Original Message
Name: Dale_Harry
Date: December 6, 2007 at 04:07:26 Pacific
Subject: Batch file search and delete help
OS: Win XP
CPU/Ram: Intel Core 2 1.8, 2GB RAM
Model/Manufacturer: Dell
Comment:

Hi there,

I need some help where I want to create a small batch file that will search inside all the text files (email files to be precise - *.eml) in a specific given directory (d:\archive) for specific strings.

Basically my plan is this:

We receive a LOT of spam email that is archived. Rather than manually sift through the whole lot I would like to run a batch script on the directory that will search for the specific words such as "Viagra", or "Pills" or etc etc - you know, the normal spam related emails.

The script will have a look in each email; if it finds such a string it will simply delete the file and move on to the next one.

Doing this will automatically eliminate a lot of the bulk of spam, leaving us to search a much smaller amount for false positive emails caught up in the spam filter.

Unfortunately I don't know how to write such a file, so any help would be very gratefully appreciated.

Many thanks,

Dale.


Report Offensive Message For Removal


Response Number 1
Name: Mechanix2Go
Date: December 6, 2007 at 06:30:02 Pacific
Reply: (edit)

Once you're satisfied it will work, remove the ECHO from the last line to 'activate' it.


=============================
:: delete emails containing spam words

@echo off
setLocal EnableDelayedExpansion

pushd d:\archive

for /f "tokens=* delims= " %%e in ('dir/b/a-d *.eml') do (
for /f "tokens=* delims= " %%a in (%%e) do (
echo %%a | find "viagra" > nul
if not errorlevel 1 echo del %%e
)
)


=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 2
Name: Dale_Harry
Date: December 6, 2007 at 07:37:38 Pacific
Reply: (edit)

Thanks very much for that, for the 'find' bit, what is the syntax for adding multiple entries?

i.e. Find "Viagra" and "Pills" and "Head" and "Arms" etc?

Many thanks again.


Report Offensive Follow Up For Removal

Response Number 3
Name: Dale_Harry
Date: December 6, 2007 at 08:10:52 Pacific
Reply: (edit)

EDIT:

I've tried the script so far and I see these errors:

http://www.zen87038.zen.co.uk/image...

Any pointers?

Many thanks....


Report Offensive Follow Up For Removal

Response Number 4
Name: Derek
Date: December 6, 2007 at 13:00:58 Pacific
Reply: (edit)

"IF" you are using Outlook or Outlook Express you can do this filtering using email rules. Many spam mails now add text in picture format so neither my suggestion or batch files will find these.

Once you get spam you are stuck with it and "it will keep growing". The only real answer is to change your email address then review your approach. Whatever else you do is fighting a losing battle - I've been down that road.

Never leave your email in "open text" on a website (for example). I have just found yours by simply clicking on your name here then using Google. If Google can find it then the spammers engines will also find it. By now it will be part of data bases that they sell to each other across the world so you can never undo this.

Machines won't click on your name so leaving your email set on this website is "reasonably" safe.

some other bloke...


Report Offensive Follow Up For Removal

Response Number 5
Name: Dale_Harry
Date: December 7, 2007 at 03:09:58 Pacific
Reply: (edit)

Thanks Derek, unfortunately its not really a case of setting up junk email rules on local Outlook accounts. Basically as a company we receive an absolutely ludicrous amount of spam each day already (somewhere near 10,000).
Our external (Linux) mail server handles a fair amount of this, but a lot will sift through to Exchange where IMF filters out the rest. As a result end users actually don't receive much spam at all, but the problem is picking up all the false positives.
Now what we have at the moment is IMF archives all the spam it picks up, we then go through it to check for false positives.
But the majority of spam we receive is in fact text based, and using specific words (although I know exactly what you mean by the image spams) - if I can auto-filter these out with a little script that will delete them, then this is just going to make our job of sifting through the other emails that much quicker.
The issue I have is that I have ZERO experience in writing anything like this - hence the cry for help as I just don't know where to start...



Report Offensive Follow Up For Removal


Response Number 6
Name: Mechanix2Go
Date: December 7, 2007 at 06:51:17 Pacific
Reply: (edit)

I must have been having a senior moment.

::== DELspam3.bat
:: delete emails containing spam words

@echo off
setLocal EnableDelayedExpansion

pushd d:\archive

for /f "tokens=* delims= " %%w in (wordlist) do (

for /f "tokens=* delims= " %%e in ('dir/b/a-d *.eml') do (
find "%%w" < %%e > nul
if not errorlevel 1 del "%%e"
)
)



=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 7
Name: Dale_Harry
Date: December 12, 2007 at 02:13:55 Pacific
Reply: (edit)

Hi again,

Many thanks for that, I've posted up a link to a screenie of the latest 'error':

http://www.zen87038.zen.co.uk/image...

You can see that I tried to search for the word "afternoon" which was contained inside an email (also pictured) but the batch file returned the error: "The system cannot find the file afternoon."

Not sure if this is a user error by me though!

Thanks so much for all this help.


Report Offensive Follow Up For Removal

Response Number 8
Name: Mechanix2Go
Date: December 12, 2007 at 05:57:28 Pacific
Reply: (edit)

Hi,

I think I goofed. You'd asked if it could search for mulitple 'spam words' so I made the first FOR loop use a list of words {I called it WORDLIST]. You need to create your own list of words. Call it whatever you like, but it must be the file named in the first FOR loop.

I couldn't make anything of that error JPG


=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Batch file search and delete help

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 6 Days.
Discuss in The Lounge