• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Tiny bit of help with a batch file script...

Hi all, I have the following attached script that runs to search through a bunch of email files (*.eml) and delete them if they match any of the words in the 'spamwords.ext' file.

I need to modify the script so that instead of deleting the .eml files based on whether they match the words in 'spamwords.txt' they are moved to 'd:\email_archive', and then whatever is left in the folder after positive matches are moved is deleted.

Would someone be able to help me modify the script please?

Thanks,


@echo off
setLocal EnableDelayedExpansion
 
pushd D:\IMF_spam_archive
 
for /f "tokens=*" %%w in (spamwords.txt) do (
 
for /f "tokens=* delims= " %%e in ('dir/b/a-d *.eml') do (
find "%%w" < %%e > nul
if not errorlevel 1 del "%%e"
)
)

Open in new window

0
Nicola_Bourne
Asked:
Nicola_Bourne
  • 4
  • 2
1 Solution
 
SteveGTRCommented:
You could do something like this:
@echo off
 
setlocal
 
set srcDir=D:\IMF_spam_archive
set destDir=d:\email_archive
set spamFile=spamwords.txt
 
pushd "%srcDir%"
 
for /f "tokens=*" %%a in ('findstr /G:"%spamFile%" /I /M *.* 2^>NUL') do if /i "%%a" NEQ "%spamFile%" move "%%a" "%destDir%"
 
popd

Open in new window

0
 
SteveGTRCommented:
Sorry, it gets a lot simpler with the proper mask:
@echo off
 
setlocal
 
set srcDir=D:\IMF_spam_archive
set destDir=d:\email_archive
set spamFile=spamwords.txt
 
pushd "%srcDir%"
 
for /f "tokens=*" %%a in ('findstr /G:"%spamFile%" /I /M *.sml 2^>NUL') do move "%%a" "%destDir%"
 
popd

Open in new window

0
 
SteveGTRCommented:
Goodness... typo:

*.sml should be *.eml
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Nicola_BourneAuthor Commented:
Thanks Steve, the 'move' bit works perfectly, only it doesn't delete all that is left afterwards yet?

Any further thoughts?

Thanks again.
0
 
SteveGTRCommented:
So you want to delete all files that don't have spam that are left in the directory?

If so, then you'd just add a del *.eml after the for:
@echo off
 
setlocal
 
set srcDir=D:\IMF_spam_archive
set destDir=d:\email_archive
set spamFile=spamwords.txt
 
pushd "%srcDir%"
 
for /f "tokens=*" %%a in ('findstr /G:"%spamFile%" /I /M *.eml 2^>NUL') do move "%%a" "%destDir%"
 
if exist "*.eml" del *.eml /F /Q
 
popd

Open in new window

0
 
Nicola_BourneAuthor Commented:
Yes basically - What was happeneing was we were filtering an insane amount of spam via IMF on Exchange. Some of this was positive email which needed to be kept, so previously we ran a script that deleted from this archive all emails that contained words in a text file. While this worked, it still left too much email to be manageable (i.e. manual searching through to be 'unblocked' - so I'm attempting to change the script to instead move all positive email to a safe location and delete what is left in the folder permanently.

Thanks for all your help - it seems to be working perfectly now :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now