VB Script needed to edit files in a folder and then move them to another folder

Associate
Joined
6 Oct 2006
Posts
375
Location
Luton
Hi

I hope somebody can help. I have a requirement to modify some eml files located inside a folder and then move them to another folder.

inside c:\folderA there are one or more eml files.

I need the script to find the line that starts with:


and change it to:


The email address could be anything, and then move it to c:\FolderB
It would then move onto the next file if there is one. The script would then be set on a schedule to do this every 5 minutes or so.

I have been told that this is possible but I have no idea how to achieve this (Scripting is not my area...)

Can anybody help at all....

Cheers
Richard
 
IIRC correctly .eml files are text-based, so it's a simple - loop through the .eml files in a directory (look at FileSystemObject), open the file (use FSO.OpenTextFile) and read the whole contents to a variable (use the .ReadAll function), close file, do a simple string replace (something like Replace(readall_variable, "[email protected]", """[email protected]""")) on that variable, open and write that variable to the same file and then use MoveFile() function (FSO again).

As for the scheduling, just use the Windows Task Scheduler and get it to run the VBS file every 5 minutes.


Edit - This could be of some use to you http://stackoverflow.com/questions/1975321/find-and-replace-string-in-my-text-with-vbscript. Could use the FindAndReplace() and call it during the looping of the .eml files. This isn't going to be a fast process if you have a lot of/large sized files though.
 
Last edited:
Screw VBS. It's old and PowerShell is way better :D

Code:
#Customise Vars
$SourcePath = "C:\FolderA\"
$DestinationPath = "C:\FolderB\"
$ReplacementText = [char]34 + "[email protected]" + [char]34

#Get file collection disregarding folders
$Files = Get-ChildItem $SourcePath | ? {$_.PSIsContainer -eq $False}

#Loop through files
foreach ($File in $Files)
{
	#Replace text in string line by line
	(Get-Content $File.FullName) | % {$_ -replace ("[email protected]", $ReplacementText)} | Set-Content $File.FullName
	
	#Move the item to Destination Folder
	Move-Item $File.FullName $DestinationPath
	
	"Processed File: " + $File.FullName | Out-Host
}

P.S backup the source files if you want to test it first, which is my advice as always. Also if you want to process all files even those in subfolders, add the -Recurse switch onto the line:

$Files = Get-ChildItem $SourcePath | ? {$_.PSIsContainer -eq $False}
 
Last edited:
I'd just install Cygwin and do it in *nix... with grep|sed|mv and run it under cron or use at :)
 
Last edited:
Thanks for this everyone.

I understand we can use the replace function, but whatabout if we do not know what the email address would be. The constant we would know would be the the start of the line contains From:. Anything after this we would not know but we would still need to get quotes around it.

Also, how would we go about reading multiple files. I am assuimg that this would be some sort of Loop...


Thanks
Richard
 
Screw VBS. It's old and PowerShell is way better :D

I really need to start learing this at some point. I think more and more will be go the Powershell route.

At the moment I only really no a little of VB, and i really mean just a little...

Thanks for that code there. I will have a play with it. How would you go about finding out what is after the From: on that line of the file and then put quotes around it. As I have just said above i wold not know what the email address would be, so i would need the script to find this out before we can do a replace...

I appreciate all your efforts.

Thanks
Richard
 
Can you give me some exact examples of what you want to replace? I can then take a look, spacing/etc is import so maybe if you can take some lines from some real files but redact the email address and paste them here?
 
Thank you everyone for your help. Sorry I haven't replied sooner.

Here is an example of an eml file that will be in the folder. The line that needs changing is the 11th down and the email address needs "" around it. The rest of the line needs to stary the same. Also this email address could well be different on each file in the folder.

x-sender: [email protected]
x-receiver: [email protected]
Received: from mail.company.com ([10.xx.xx.xx]) by helpdesk.company.co.uk with Microsoft SMTPSVC(7.5.7601.17514);
Wed, 31 Aug 2011 10:55:33 +0100
To: [email protected]
Subject: Hello
MIME-Version: 1.0
X-KeepSent: DA179624:87FC33EC-802578FD:00368078;
type=4; name=$KeepSent
X-Mailer: Lotus Notes Release 8.5.2 August 10, 2010
From: [email protected]
Message-ID: <OFDA179624.87FC33EC-ON802578FD.00368078-802578FD.003685C1@copmany.com>
Date: Wed, 31 Aug 2011 10:55:32 +0100
X-MIMETrack: Serialize by Router on XXX01/Servers/Company(Release 8.5.2FP2|March 22, 2011) at
31/08/2011 10:55:33,
Serialize complete at 31/08/2011 10:55:33
Content-Type: multipart/alternative; boundary="=_alternative 003685A3802578FD_="
Return-Path: [email protected]
X-OriginalArrivalTime: 31 Aug 2011 09:55:33.0240 (UTC) FILETIME=[1FD7F780:01CC67C4]
This is a multipart message in MIME format.
--=_alternative 003685A3802578FD_=
Content-Type: text/plain; charset="US-ASCII"
This is a test
Thanks
Richard
--=_alternative 003685A3802578FD_=
Content-Type: text/html; charset="US-ASCII"
<font size=2 face="sans-serif">This is a test</font>
<br>
<br><font size=2 face="sans-serif">Thanks</font>
<br><font size=2 face="sans-serif">Richard</font>
--=_alternative 003685A3802578FD_=--

If anybody could help with this i would really appreciate it

Let mek now if you need anymore info.

Cheers
Richard
 
Last edited:
Sorry missed your reply, try this one. As normal you should test till you are happy :)

Code:
#Customise Vars
$SourcePath = "C:\FolderA\"
$DestinationPath = "C:\FolderB\"

#Get file collection disregarding folders
$Files = Get-ChildItem $SourcePath | ? {$_.PSIsContainer -eq $False}

#Loop through files
foreach ($File in $Files)
{
	#Replace text in string line by line
	(Get-Content $File.FullName) | % {$_ -replace ("From: \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b", ("From: " + [char]34 + ($_.Split())[-1] + [char]34))} | Set-Content $File.FullName
	
	#Move the item to Destination Folder
	Move-Item $File.FullName $DestinationPath
	
	"Processed File: " + $File.FullName | Out-Host
}
 
Back
Top Bottom