-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_dirs
78 lines (64 loc) · 2.06 KB
/
check_dirs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env php
<?php
echo "Processing...\n";
/** Error Reporting */
ini_set('display_errors', true);
error_reporting(E_ALL);
/** Root Path definition */
defined('ROOT_PATH') ||
define('ROOT_PATH', __DIR__ . '/');
/** Require needed classes files */
require_once ROOT_PATH . 'helper/DirHelper.php';
/** @var array $params cli params */
$params = getopt('', [
'dir:',
'rmempty',
]);
if (empty($params['dir'])) {
echo "Param '--dir' is required\n";
exit;
}
/** @var string $dir path to dir need to be checked */
$dir = ROOT_PATH . rtrim($params['dir'], '\\/') . '/';
echo "--dir '$dir'\n";
if (!file_exists($dir) || !is_dir($dir)) {
echo "'--dir' must be existing directory\n";
exit;
}
/** @var bool $rmempty remove empty directories */
$rmempty = isset($params['rmempty']);
echo "--rmempty " . ($rmempty ? 'TRUE' : 'FALSE') . "\n";
/** @var array $list subdirectories listing */
$list = DirHelper::ls($dir, DirHelper::FILTER_DIRS);
foreach ($list as $subdir) {
echo "Checking: '" . $subdir['name'] . "'...\n";
/** @var array $sublist directories files and folders */
$sublist = DirHelper::ls($subdir['path']);
/**
* @var string $key in format "$type::$name"
* @var array $item
*/
foreach ($sublist as $key => $item) {
if ($item['type'] != DirHelper::TYPE_FILE) {
continue;
}
if (empty($item['info']['extension']) || $item['info']['extension'] != 'bak') {
continue;
}
/** @var string $bakName .bak file name without extension */
$bakName = $item['info']['filename'];
if (isset($sublist[DirHelper::TYPE_FILE . "::$bakName.doc"])) {
continue;
}
/** main file for .bak not exists, need remove .bak file */
DirHelper::rm($item['path']);
unset($sublist[$key]);
echo "File '" . $item['name'] . "' removed\n";
}
/** current directory is empty */
if (!count($sublist) && $rmempty) {
DirHelper::rm($subdir['path']);
echo "Dir '" . $subdir['name'] . "' removed\n";
}
}
echo "Done\n";