我正在尝试将批处理作业电子邮件发送到“OLD”文件夹后移动文件。
我已经成功确定了这样做的代码。现在该文件名为monthly_mssql
,我正在尝试将文件重命名为monthly_mssql_mmmyyyy
。
将月份和年份添加到文件的末尾,我可以跟踪确定哪个月的报告。我该如何编辑现有的命令文件?
我的CMD文件代码:
REM The report is generated automatically and must be sent at 9:50 AM of the first Monday of every month, file is located
REM in C:\Reports\mssql\Monthly_Stats
"C:\Program Files\xampp\php\php.exe" "c:\htdocs\MultipleReport\monthly_smurfreport_email.php"
move C:\Smurf_Reports\mssql\Monthly_Stats\monthly_mssql.csv C:\Smurf_Reports\mssql\Monthly_Stats\old
我的PHP文件代码:
$dirpath = "C:/Reports/mssql/Monthly_Stats/";
if ($handle = opendir($dirpath ))
{
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 8bit\n\n" .
$message_text . "\n\n";
$email_message .= "--{$mime_boundary}\n";
/* This is the correct way to loop over the directory. */
while (false !== ($entry = readdir($handle)))
{
if (strpos($entry, '.csv',1))
{
$filepath = "";
$filesize = 0;
$filepath = $dirpath."".$entry;
$filesize = filesize ($filepath);
if (file_exists($filepath) && $filesize > 1)
{
$fileatt = $filepath;
$fileatt_name = $entry;
$fileatt_type = "application/octet-stream";
$message_text .= "<P>Hi</P>";
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$data = chunk_split(base64_encode($data));
$email_message .= "Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
$email_message .= "--{$mime_boundary}\n";
}
$email_from = "k.j@yahoo.com";
// $ email_to =“k.j@yahoo.com”; $ email_to =“k.j@yahoo.com”;
$lastMonth = date('F-Y',strtotime('last month'));
$email_subject = "($lastMonth) Report for MS-SQL.";
$headers .= "From: ".$email_from."\r\n";
echo $filepath."</br>";
}
}
closedir($handle);
$ok = @mail($email_to, $email_subject, $email_message, $headers);
}
/*if (file_exists($filename) && $size > 228 && $EmailAdd <> "")
{
$fileatt = $filename;
$fileatt_name = $groupname.".csv";
if ($uemail <> "")
$email_from = $uemail;
else
$email_from = $username."@yahoo.com";
$email_to = $email_from;
$email_subject = " Messages for Priority '".$p."' - ".$groupname." - ".$displaydate;
$headers .= "From: ".$email_from. "\r\n";
$headers .= "Cc: ".$EmailAdd. "\r\n";
$headers .= "Bcc: k.j@yahoo.com";
$email_message .= "<P>Hi</P>";
$email_message .= "<P><B>Please find attached Monthly Smurf Report Stats."</U></B>\n";
$email_message .= "<P><P>Thanks,<P>;
set_error_handler("myErrorHandler");
$ok = @mail($email_to, $email_subject, $email_message, $headers);
if ($ok)
{
unlink($filename);
}
else
{
$file = "C:/Reports/Operation/Daily_Stats/smtperrlog.txt";
$fh = fopen($file, 'r+');
$contents = fread($fh, filesize($file));
fclose($fh);
$stderr = fopen ('C:/Reports/Operation/Daily_Stats/smtperrlog.txt', 'w+');
fwrite($stderr,"");
fclose($stderr);
$email_fromerr = "k.j@yahoo.com";
$email_toerr = "k.j@yahoo.com";
$email_subjecterr = "Monthly Stats";
$email_messageerr = $contents;
$headerserr .= "From: ".$email_fromerr. "\r\n";
@mail($email_toerr, $email_subjecterr, $email_messageerr, $headerserr);
}
}
*/
$message_text = "";
$p = "";
$EmailAdd = "";
$headers = "";
$counter = 0;
答案 0 :(得分:0)
在我看来,使用批处理文件移动它是不必要的。我会使用rename()
方法直接在PHP中移动它。您可以使用date()
在文件末尾附加日期。
这方面的一个例子是:
rename("C:\Smurf_Reports\mssql\Monthly_Stats\monthly_mssql.csv", "C:\Smurf_Reports\mssql\Monthly_Stats\old\monthly_mssql_" . date("m_Y") . ".csv");
修改强>
$cMonth = intval(date("m")); //Retrieves current month, converts to int value
$lastMonth = ($cMonth == 1 ? "12" : $cMonth - 1); //If it's January, let's set the month to December
$cYear = ($cMonth == 1 ? intval(date("Y")) - 1 : date("Y")); //If it's January, let's also set the year back by one so the dates match up
rename("C:\Smurf_Reports\mssql\Monthly_Stats\monthly_mssql.csv", "C:\Smurf_Reports\mssql\Monthly_Stats\old\monthly_mssql_" . $lastMonth . "_" . $cYear . ".csv");