我的一个文件夹中有多个文本文件。我想写那些多个文本文件'数据到一个文件并删除那些多个文本文件。我会在一定时间内连续做。我正在使用bat文件。这是第一次正确运行。它可以写入数据并正确删除文件。但是从第二次它给我错误,因为我删除了那些文件,但代码不在第二次运行中检查空目录。但是,让我们认为第一次运行中的目录中没有文件。然后检查并告诉我没有数据。但这不是第二次检查。
这是我的代码。
/* Data loading interval in seconds */
$interval = 10;
/* Log file name and location */
$log_file = "C:/xampp/htdocs/attendance/file/res/result.TXT";
/* Multiple text files' location */
$files = glob("C:/xampp/htdocs/attendance/file/temp_file/*.txt");
/* Event Number and Event Description */
$EventLog = array();
$EventLog['00'] = "Check-In";
$EventLog['01'] = "Check-Out";
$EventLog['02'] = "Breack-Out";
$EventLog['03'] = "Resume-In";
$EventLog['04'] = "OT-In";
$EventLog['05'] = "OT-Out";
// Connect to mysql
$connect = mysql_connect($hostname, $username, $password) or die("connection failed");
mysql_select_db($database) or die("db selection failed");
mysql_query("CREATE TABLE IF NOT EXISTS log (user_id varchar(30) NOT NULL, date_time datetime NOT NULL)");
mysql_query("CREATE TABLE IF NOT EXISTS tmp_log (user_id varchar(30) NOT NULL, date_time datetime NOT NULL)");
mysql_query("CREATE TABLE IF NOT EXISTS process_log (user_id varchar(30) NOT NULL, check_in datetime DEFAULT NULL, check_out datetime DEFAULT NULL, flag char(1) DEFAULT NULL)");
mysql_query("CREATE TABLE IF NOT EXISTS error_log (user_id varchar(30) NOT NULL, date_time datetime NOT NULL)");
mysql_query("CREATE TABLE IF NOT EXISTS attendance_log (user_id varchar(30) NOT NULL, check_in datetime NOT NULL, check_out datetime NOT NULL)");
while(1){
/* Combine multiple text files to one */
if(count($files)==0) {
echo date("Y-m-d H:i:s")." No Files. Empty Dir.\n";
$timenow=date("Y-m-d H:i:s");
$err_msg = $timenow." No Files. Empty Dir.";
ConnectionErrorLog($err_msg);
} else {
$is_loaded = TRUE;
echo date("Y-m-d H:i:s")."Multiple Files Loaded.\n";
foreach($files as $file) {
$content = file_get_contents($file);
file_put_contents($log_file, $content.PHP_EOL, FILE_APPEND);
}
if(!file_put_contents($log_file, $content.PHP_EOL, FILE_APPEND)){
$is_loaded = FALSE;
}
//if($is_loaded == TRUE){
$scan_files = scandir("C:/xampp/htdocs/attendance/file/temp_file/");
$source = "C:/xampp/htdocs/attendance/file/temp_file/";
$destination = "C:/xampp/htdocs/attendance/file/back/";
foreach ($scan_files as $s_file) {
if (in_array($s_file, array(".",".."))) continue;
if (copy($source.$s_file, $destination.date("Y_m_d_H_i_s_").$s_file.".txt")) {
$delete[] = $source.$s_file;
}
}
foreach ($delete as $s_file) {
unlink($s_file);
}
//}
}
if (!$connect) {
$timenow=date("Y-m-d H:i:s");
$err_msg = $timenow." MySQL Connection Failed: ".mysql_error();
ConnectionErrorLog($err_msg);
//Connect again for this cycle.
$connect = mysql_connect($hostname, $username, $password, $database);
}
clearstatcache();
if(file_exists($log_file) && filesize($log_file)){
$is_loaded = TRUE;
if($read_file = fopen($log_file, "r")) {
while($line = fgets($read_file)) {
$line = trim($line);
$data = explode(" ", $line);
if(count($data) == 3){
$query = "INSERT INTO tmp_log (user_id, date_time) VALUES ('".$data[0]."', STR_TO_DATE('".$data[1]."', '%Y-%m-%d-%H:%i:%s'))";
if(!mysql_query($query)){
$is_loaded = FALSE;
}
}
}
// Clear file
fclose($read_file);
if($is_loaded == TRUE){
//# Remove File
//unlink($log_file);
//# Rename file and move to backup folder
$new_name = "C:/xampp/htdocs/attendance/file/back/res/result_".date("Ymd_His").".txt";
rename($log_file, $new_name);
echo date("Y-m-d H:i:s")." Result File Loaded.\n";
}else{
$timenow=date("Y-m-d H:i:s");
$err_msg = $timenow." Error in Loading Data.";
ConnectionErrorLog($err_msg);
//Check and reconnect again for next cycle.
if (!mysql_ping($connect)) {
$connect = mysql_connect($hostname, $username, $password, $database);
}
}
}
} else {
$timenow=date("Y-m-d H:i:s");
$err_msg = $timenow." attendance_test log file not found.";
ConnectionErrorLog($err_msg);
}
//Filter data
handle_crossover();
update_log();
sleep($interval);
}
# Close the connection
mysql_close($connect);
这是错误。
2016-10-24 11:55:38Multiple Files Loaded.
2016-10-24 11:55:38 Result File Loaded.
2016-10-24 11:55:48Multiple Files Loaded.
PHP Warning: file_get_contents(C:/xampp/htdocs/attendance/file/temp_file/LOG_1.
txt): failed to open stream: No such file or directory in C:\xampp\htdocs\attend
ance\final2.php on line 54
Warning: file_get_contents(C:/xampp/htdocs/attendance/file/temp_file/LOG_1.txt):
failed to open stream: No such file or directory in C:\xampp\htdocs\attendance\
final2.php on line 54
PHP Warning: file_get_contents(C:/xampp/htdocs/attendance/file/temp_file/LOG_2.
txt): failed to open stream: No such file or directory in C:\xampp\htdocs\attend
ance\final2.php on line 54
Warning: file_get_contents(C:/xampp/htdocs/attendance/file/temp_file/LOG_2.txt):
failed to open stream: No such file or directory in C:\xampp\htdocs\attendance\
final2.php on line 54
PHP Warning: file_get_contents(C:/xampp/htdocs/attendance/file/temp_file/LOG_3.
txt): failed to open stream: No such file or directory in C:\xampp\htdocs\attend
ance\final2.php on line 54
Warning: file_get_contents(C:/xampp/htdocs/attendance/file/temp_file/LOG_3.txt):
failed to open stream: No such file or directory in C:\xampp\htdocs\attendance\
final2.php on line 54
PHP Warning: unlink(C:/xampp/htdocs/attendance/file/temp_file/LOG_1.txt): No su
ch file or directory in C:\xampp\htdocs\attendance\final2.php on line 74
Warning: unlink(C:/xampp/htdocs/attendance/file/temp_file/LOG_1.txt): No such fi
le or directory in C:\xampp\htdocs\attendance\final2.php on line 74
PHP Warning: unlink(C:/xampp/htdocs/attendance/file/temp_file/LOG_2.txt): No su
ch file or directory in C:\xampp\htdocs\attendance\final2.php on line 74
Warning: unlink(C:/xampp/htdocs/attendance/file/temp_file/LOG_2.txt): No such fi
le or directory in C:\xampp\htdocs\attendance\final2.php on line 74
PHP Warning: unlink(C:/xampp/htdocs/attendance/file/temp_file/LOG_3.txt): No su
ch file or directory in C:\xampp\htdocs\attendance\final2.php on line 74
Warning: unlink(C:/xampp/htdocs/attendance/file/temp_file/LOG_3.txt): No such fi
le or directory in C:\xampp\htdocs\attendance\final2.php on line 74
2016-10-24 11:55:48 Result File Loaded.