情景
你需要在Mac下複製文件到文件夾“B”,但是有部分文件重複了,這時系統沒有“保留二者”的選項,只能"跳過","停止","替換",這個簡單的要求"保留"爲什麼不做出來?APP store裏面有收費的軟件針對這個小問題,¥30。。。有必要麼?
以下腳本是我開發出來,Mac端合併重複文件夾、文件用的,歡迎一起探討。
效果圖「2」
準備材料
- 本工具(mergeFolders.php) https://download.csdn.net/download/qq285744011/12235772
- 桌面新建兩個文件夾,from和to,你需要把你要合併的所有文件夾、文件都扔進去from
使用方法
* 【使用方法】
* 1. 在桌面上新建兩個文件夾,取名"from"和"to",把你需要合併的文件夾、文件統統複製到文件夾"from"
* 2. 下載本文件"mergeFolders.php"到Macbook上的桌面
* 3. 打開命令行工具 (按F4 > 文件夾"其它" > 終端)
* 4. 在Mac中開啓PHP語言支持: https://rudon.blog.csdn.net/article/details/104745975
* 5. 在命令行中,輸入命令:php ~/Desktop/mergeFolders.php
* 6. OK, 稍等片刻,檢查文件夾"to"是否包含了所有的文件
腳本分享
<?php
/**
* 主題:文件夾合併工具 for Mac, 實現複製文件時"保留兩個文件"的效果
*
* 作者:Rudon <[email protected]>
* 創建:2020-03-09
* 描述:
* 在Windows系統下從-文件夾A-複製文件到-文件夾B-時,
* 如果文件logo.jpg重複了,系統會在文件夾B裏創建文件'logo(1).jpg',
* 但是,Mac系統不會,只能"跳過","停止","替換",這個簡單的要求"保留"爲什麼不做出來?
* 本工具支持多層文件夾合併。
*
*
* 【使用方法】
* 1. 在桌面上新建兩個文件夾,取名"from"和"to",把你需要合併的文件夾、文件統統複製到文件夾"from"
* 2. 下載本文件"mergeFolders.php"到Macbook上的桌面
* 3. 打開命令行工具 (按F4 > 文件夾"其它" > 終端)
* 4. 在Mac中開啓PHP語言支持: https://rudon.blog.csdn.net/article/details/104745975
* 5. 在命令行中,輸入命令:php ~/Desktop/mergeFolders.php
* 6. OK, 稍等片刻,檢查文件夾"to"是否包含了所有的文件
*
*/
/* summary.xls => summary(1).xls */
define('SYMBOL_LEFT', '(');
define('SYMBOL_RIGHT', ')');
define('FOLDER_FROM', 'from');
define('FOLDER_TO', 'to');
function a ($v){
header('Content-Type: text/css; charset=utf-8');
print_r($v);
die();
}
$action = new mergeFolder();
$action->merge();
$action->echoResult();
class mergeFolder {
public $working_path;
public $from;
public $to;
public $merged_list;
public $special_cases;
public $need_log;
public function __construct() {
/* 當前文件夾 */
$this->working_path = dirname(__FILE__).'/';
$this->from = $this->working_path . FOLDER_FROM . '/';
$this->to = $this->working_path . FOLDER_TO . '/';
$this->merged_list = array();
$this->special_cases = array();
$this->need_log = true;
$this->check_folders();
}
public function check_folders () {
if (!is_dir($this->from)) {
$this->echoError('找不到文件夾`'.FOLDER_FROM.'`');
}
if (!is_dir($this->to)) {
$this->echoError('找不到文件夾`'.FOLDER_TO.'`');
}
}
public function echoError ($message) {
die( PHP_EOL .'ERROR: '. $message . PHP_EOL );
}
public function get_list_of_files_recursive ($folder) {
$return = array();
$folder = rtrim($folder, '/').'/';
if(is_dir($folder)){
$list = scandir($folder);
foreach ($list as $oneFile) {
if (!preg_match('/^\./', $oneFile)) {
/* 除掉那些點號開頭的文件 */
if (is_dir($folder. $oneFile)) {
$sub_files = $this->get_list_of_files_recursive($folder.$oneFile);
$return = array_merge($return, $sub_files);
} else {
$return[] = $folder . $oneFile;
}
}
}
}
return $return;
}
public function merge () {
$files = $this->get_list_of_files_recursive($this->from);
$counts = count($files);
if(!$counts){
$this->echoError('沒有任何文件可合併');
}
foreach ($files as $k => $one) {
/* 對比文件名 */
$file_name = pathinfo($one, PATHINFO_BASENAME);
$tobe = $this->to . $file_name;
$file_ext = pathinfo($one, PATHINFO_EXTENSION);
$file_name_pre = pathinfo($one, PATHINFO_FILENAME);
if (!in_array($file_name, $this->merged_list)) {
/* 不重複 */
$this->merged_list[] = $file_name;
copy($one, $tobe); // 複製
unlink($one); // 刪除
} else {
$this->log('Repeat: '.$file_name.' in '.$one);
/* 重複文件 */
$zuo = SYMBOL_LEFT;
$you = SYMBOL_RIGHT;
if (!key_exists($file_name, $this->special_cases)) {
/* 新豬肉 */
$this->special_cases[$file_name] = 1;
} else {
/* 舊豬肉 */
$lastNum = $this->special_cases[$file_name];
$newNum = $lastNum + 1;
$this->special_cases[$file_name] = $newNum;
}
$repeat_num = $this->special_cases[$file_name];
$file_name_new = $file_name_pre.$zuo.$repeat_num.$you.'.'.$file_ext;
$this->log('Repeat: '.$file_name.' [Repeat] '.$repeat_num);
$this->log('Repeat: '.$file_name.' [To Be] '.$file_name_new);
while (in_array($file_name_new, $this->merged_list)) {
$this->log('Repeat: '.$file_name.' [Exists in Target folder] yes');
$repeat_num++;
$this->special_cases[$file_name] = $repeat_num;
$file_name_new = $file_name_pre.$zuo.$repeat_num.$you.'.'.$file_ext;
$this->log('Repeat: '.$file_name.' [New Repeat] '.$repeat_num);
$this->log('Repeat: '.$file_name.' [New To Be] '.$file_name_new);
}
$this->merged_list[] = $file_name_new;
copy($one, $this->to . $file_name_new); // 複製
unlink($one); // 刪除
$this->log('Repeat: '.$file_name.' [Done] ');
}
}
}
public function log ($mess) {
if ($this->need_log) {
echo PHP_EOL.'[DEV] '.$mess.PHP_EOL;
}
}
public function echoResult () {
die( PHP_EOL .'成功!其中重複文件共有'. count($this->special_cases) .'組' . PHP_EOL );
}
} /* End of CLASS */