指定文件夹内所有文件自动去重复行php脚本

亲测源码11个月前更新 admin
109 0

指定文件夹内所有文件自动去重复行php脚本

一个php文件  实现自动给多个文件夹里的文件去重复行操作

在这个示例中,你可以将需要处理的文件夹路径添加到 $folders 数组中。脚本会递归地遍历每个文件夹,并处理其中的文件。如果文件是文本文件(扩展名为 .txt),它会执行去重操作并输出相应的消息。

请在运行脚本之前确保已备份重要的文件,以防误操作导致数据丢失。

指定文件夹内所有文件自动去重复行php脚本

 

指定文件夹内所有文件自动去重复行php脚本

<?php
// 指定要处理的文件夹数组
$folders = [
$_SERVER[‘DOCUMENT_ROOT’] . ‘/link/juzi’,

// 添加更多文件夹路径…
];

// 递归遍历文件夹并处理文件
foreach ($folders as $folder) {
processFilesInFolder($folder);
}

/**
* 递归遍历文件夹并处理文件
*
* @param string $folderPath 文件夹路径
*/
function processFilesInFolder($folderPath)
{
// 获取文件夹中的文件列表
$files = glob($folderPath . ‘/*’);

if (is_array($files)) {
foreach ($files as $file) {
if (is_file($file)) {
// 如果是文本文件
if (pathinfo($file, PATHINFO_EXTENSION) === ‘txt’) {
// 读取文件内容
$contents = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

// 去除重复行
$contents = array_unique($contents);

// 清空文件内容
file_put_contents($file, ”);

// 将去重后的内容写回文件
file_put_contents($file, implode(“\n”, $contents), FILE_APPEND);

echo “已去除文件 $file 中的重复行。\n”;
}
}
elseif (is_dir($file)) {
// 递归处理子文件夹
processFilesInFolder($file);
}
}
}
}
?>

 

 

© 版权声明

相关文章