Home Page
  • April 29, 2024, 08:54:11 am *
  • Welcome, Guest
Please login or register.

Login with username, password and session length
Advanced search  

News:

Official site launch very soon, hurrah!



Post reply

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.

Name:
Email:
Subject:
Message icon:

Attach:
(Clear Attachment)
(more attachments)
Restrictions: 10 per post, maximum total size 8192KB, maximum individual size 5120KB
Note that any files attached will not be displayed until approved by a moderator.
Verification:
Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:
Please stop spamming. Your spam posts are moderated and will never be displayed on the internet. What is eighty-eight minus eighty-six (spell out the answer):
Пожалуйста, прекратите спамить. Ваши спам-сообщения модерируются и никогда не будут отображаться в Интернете. What color is grass.:

shortcuts: hit alt+s to submit/post or alt+p to preview


Topic Summary

Posted by: Dakusan
« on: September 28, 2009, 05:31:45 am »

Original post for Comparing Log File can be found at https://www.castledragmire.com/Posts/Comparing_Log_File.
Originally posted on: 08/22/08

So for reasons I’m not going to go into, today I had to compare some log files. I was tempted to write the code in C, just because I miss it so much these days x.x;, but laziness won out, especially as there weren’t that many log files and they weren’t that large, so I wrote it in PHP.

Nothing else to this post except the working code which took me about 5 minutes to type out... The function goes through one directory and all of its subdirectories and checks all files against the same path in a second directory. If the file doesn’t exist in the second directory or its contents doesn’t match the first file up to the first file’s length, a message is outputted.



//Start the log run against 2 root directories
TestLogs("/DIR1", "/DIR2");

function TestLogs($RootDir1, $RootDir2, $CurDir="")
{
   //Iterate through the first directory
   $Dir1=opendir("$RootDir1$CurDir");
   $SubDirs=Array(); //Holds subdirectories
   while($File=readdir($Dir1))
      if($File=="." || $File=="..") //Skip . and ..
         continue;
      else if(is_dir("$RootDir1$CurDir/$File")) //Do not try to compare directory entries
         $SubDirs[]=$File; //Remember subdirectories
      else if(!file_exists("$RootDir2$CurDir/$File"))
         print "File '$CurDir/$File' does not exist in second directory.
";
      else if(file_get_contents("$RootDir1$CurDir/$File")!=substr(file_get_contents("$RootDir2$CurDir/$File"),0,filesize("$RootDir1$CurDir/$File"))) //Both files exist, so compare them - if first file does not equal second file up to the same length, output error
         print "'$CurDir/$File' does not match.
";
   
   //Run subdirectories recursively after current directories' file-run so directories do not get split up
   foreach($SubDirs as $NewDir)
      TestLogs($RootDir1, $RootDir2, "$CurDir/$NewDir");
}