07-24-2023, 08:16 AM
I'm making a JScript script that sends e-mail messages in HTML format via CDO. I have a message template in .html file, and I put some unique data into each message being sent. So, I create a copy of the original .html file for each message.
The problem is that I cannot detele this temporary file from within script after sending the message. I've checked with ProcessExplorer, which process locks the file, and it is cscript.exe/wscript.exe (wichever is being used to run the script).
Here is the example (simplified) code:
//-------------------------------------------
var fso = new ActiveXObject("Scripting.FileSystemObject");
var BodyFileName = fso.GetAbsolutePathName(WScript.Arguments(3));
var BodyExtensionName = fso.GetExtensionName(BodyFileName)
var BodyFile = fso.OpenTextFile(BodyFileName, 1);
var Body = BodyFile.ReadAll();
BodyFile.Close();
//-------------------------------------------
// Replace something in Body here
//-------------------------------------------
BodyFileName = fso.BuildPath(fso.GetParentFolderName(BodyFileName), fso.GetTempName() + "." + BodyExtensionName);
var TmpBodyFile = fso.OpenTextFile(BodyFileName, 2, true);
TmpBodyFile.Write(Body);
TmpBodyFile.Close();
// Here ProcessExplorer shows that the file is closed
//-------------------------------------------
var objEmail = new ActiveXObject("CDO.Message");
objEmail.From = WScript.Arguments(0);
objEmail.To = WScript.Arguments(1);
objEmail.Subject = WScript.Arguments(2);
objEmail.CreateMHTMLBody("file:///" + BodyFileName.replace("\\","/"));
// Here the file is open again
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2;
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "192.168.0.1";
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25;
objEmail.Configuration.Fields.Update();
objEmail.Send();
objEmail = null;
//-------------------------------------------
// The following loop becomes endless
for (;;) {
try {
fso.DeleteFile(BodyFileName, true);
} catch(e) {
WScript.Sleep(100);
continue;
} finally {
}
break;
}
//-------------------------------------------
Thanks.
The problem is that I cannot detele this temporary file from within script after sending the message. I've checked with ProcessExplorer, which process locks the file, and it is cscript.exe/wscript.exe (wichever is being used to run the script).
Here is the example (simplified) code:
//-------------------------------------------
var fso = new ActiveXObject("Scripting.FileSystemObject");
var BodyFileName = fso.GetAbsolutePathName(WScript.Arguments(3));
var BodyExtensionName = fso.GetExtensionName(BodyFileName)
var BodyFile = fso.OpenTextFile(BodyFileName, 1);
var Body = BodyFile.ReadAll();
BodyFile.Close();
//-------------------------------------------
// Replace something in Body here
//-------------------------------------------
BodyFileName = fso.BuildPath(fso.GetParentFolderName(BodyFileName), fso.GetTempName() + "." + BodyExtensionName);
var TmpBodyFile = fso.OpenTextFile(BodyFileName, 2, true);
TmpBodyFile.Write(Body);
TmpBodyFile.Close();
// Here ProcessExplorer shows that the file is closed
//-------------------------------------------
var objEmail = new ActiveXObject("CDO.Message");
objEmail.From = WScript.Arguments(0);
objEmail.To = WScript.Arguments(1);
objEmail.Subject = WScript.Arguments(2);
objEmail.CreateMHTMLBody("file:///" + BodyFileName.replace("\\","/"));
// Here the file is open again
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2;
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "192.168.0.1";
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25;
objEmail.Configuration.Fields.Update();
objEmail.Send();
objEmail = null;
//-------------------------------------------
// The following loop becomes endless
for (;;) {
try {
fso.DeleteFile(BodyFileName, true);
} catch(e) {
WScript.Sleep(100);
continue;
} finally {
}
break;
}
//-------------------------------------------
Thanks.