0

There are some similar questions but none of them helped my case. All the other questions were talking about versioning number but I needed something that will only increase the build number.

I needed a script that would check if there are any files changed/created/deleted and increase the build number by 1.

I couldn't find an answer to this problem online and prepared a script myself. I am asking this question, so I can share my script as an answer.

Here's the script I came up with. Feel free to improve it further and modify my answer or post your own answer:

// Opening the json file that holds the file paths and file modification dates
$jsonArray =  json_decode(file_get_contents('files.json'), true);
$jsonFileArray = array();

// Putting the values into a local array
foreach ($jsonArray as $filePath => $modifiedDate) {
    $jsonFileArray[$filePath] = $modifiedDate;
}


// Iterating through the directories and putting the file paths and modification dates into a local array
$filesArray = array();
$dir_iterator = new RecursiveDirectoryIterator(".");
$recursive_iterator = new RecursiveIteratorIterator($dir_iterator);
foreach ($recursive_iterator as $file) {
    if ($file->isDir()) {
        continue;
    }
    if (substr($file, -9) != 'error_log') {
        $fileName = $file->getPathname();
        $fileModifiedDate = date('m/d/y H:i:s', $file->getMTime());
        $filesArray[$fileName] = $fileModifiedDate;
    }
}


// Checking if there are any files that are modified/created/deleted
if ($jsonFileArray != $filesArray) {

    // If there are any changes, the build number is increased by 1 and saved into 'build' file
    $buildFile = "build";
    file_put_contents($buildFile, file_get_contents($buildFile) + 1);
}


// Updating the json file with the latest modifiedDates
$jsonFile = fopen('files.json', 'w');
fwrite($jsonFile, json_encode($filesArray, JSON_UNESCAPED_SLASHES));
fclose($jsonFile);
Engin Yapici
  • 5,673
  • 5
  • 22
  • 32
  • 2
    Wow, that's a fast downvote (within 2 seconds). I don't think it is even possible for you to read the question in 2 seconds. – Engin Yapici Dec 31 '15 at 15:09
  • I believe you were down-voted because your question shows no effort and is awfully broad. You have a couple of close votes too, both of which indicate that your question is far too broad. – Jay Blanchard Dec 31 '15 at 15:22
  • Downvoting is fine, I don't oppose to that. But downvoting without even reading the question is ridiculous. He/she should have added a comment instead, so I can improve the question. I added my code in the question and tried to clarify it further in the text. I am trying to make this a quality question but downvotes are not very helpful. Please add a comment or edit the question yourself if you want to improve it. – Engin Yapici Dec 31 '15 at 15:29

1 Answers1

1

The code below fetches all the files in a directory, puts them in an array and saves it as a JSON file. When the script is run again, it fetches all the files and the modification dates again and compares it to the JSON file. If there are any changes (e.g. files were modified/created/deleted), it increases the build number by 1.

// Opening the json file that holds the file paths and file modification dates
$jsonArray =  json_decode(file_get_contents('files.json'), true);
$jsonFileArray = array();

// Putting the values into a local array
foreach ($jsonArray as $filePath => $modifiedDate) {
    $jsonFileArray[$filePath] = $modifiedDate;
}


// Iterating through the directories and putting the file paths and modification dates into a local array
$filesArray = array();
$dir_iterator = new RecursiveDirectoryIterator(".");
$recursive_iterator = new RecursiveIteratorIterator($dir_iterator);
foreach ($recursive_iterator as $file) {
    if ($file->isDir()) {
        continue;
    }
    if (substr($file, -9) != 'error_log') {
        $fileName = $file->getPathname();
        $fileModifiedDate = date('m/d/y H:i:s', $file->getMTime());
        $filesArray[$fileName] = $fileModifiedDate;
    }
}


// Checking if there are any files that are modified/created/deleted
if ($jsonFileArray != $filesArray) {

    // If there are any changes, the build number is increased by 1 and saved into 'build' file
    $buildFile = "build";
    file_put_contents($buildFile, file_get_contents($buildFile) + 1);
}


// Updating the json file with the latest modifiedDates
$jsonFile = fopen('files.json', 'w');
fwrite($jsonFile, json_encode($filesArray, JSON_UNESCAPED_SLASHES));
fclose($jsonFile);
Engin Yapici
  • 5,673
  • 5
  • 22
  • 32
  • Please edit your original post to add any information, such as this code, to your question. – Jay Blanchard Dec 31 '15 at 15:08
  • 1
    I modified it. I believe the question is very clear. I asked it so I can share my knowledge. As far as I know, it is the whole point of having forums such as SO: so we can share our knowledge. – Engin Yapici Dec 31 '15 at 15:13
  • Yes.....and no. SO is not a (discussion) forum, it is a question and answer site. A ***good answer*** will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO. It was not clear that you were answering your own question. – Jay Blanchard Dec 31 '15 at 15:21
  • 1
    I added an explanation of what the script does. I thought the comments in the code were clear enough, but I guess a broader explanation of what the script does is helpful too. Thanks for your comment. – Engin Yapici Dec 31 '15 at 15:31