1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- require('../vendor/autoload.php');
- // 定义要查询的目录路径
- $directoryPath = 'F:\声音文件\bark sound\UrbanSound8K_Expand_verison\UrbanSound8K_Expand_verison\audio';
- // 定义分类的目标目录路径
- $categoryDir = 'F:\声音文件\bark sound\category_new';
- // 确保目标目录存在
- if (!file_exists($categoryDir)) {
- mkdir($categoryDir, 0777, true);
- }
- // 递归函数,用于遍历文件夹并处理文件
- function processDirectory($dir) {
- global $categoryDir;
- // 打开目录
- if ($handle = opendir($dir)) {
-
- // 读取目录中的文件
- while (false !== ($entry = readdir($handle))) {
-
- // 排除 "." 和 ".."
- if ($entry == "." || $entry == "..") {
- continue;
- }
- // 获取文件或目录的完整路径
- $filePath = $dir . DIRECTORY_SEPARATOR . $entry;
-
- // 如果是文件,则进行分类处理
- if (is_file($filePath)) {
- $fileInfo = pathinfo($filePath);
-
- $parts = explode('-', $fileInfo['filename']);
- // 获取第二个下标的元素并判断是否在 1 到 9 之间
- if (isset($parts[1]) && is_numeric($parts[1]) && $parts[1] >= 0 && $parts[1] <= 9) {
- $type_arr=[
- 0 => 'air_conditioner',
- 1 => 'car_horn',
- 2 => 'children_playing',
- 3 => 'dog_bark',
- 4 => 'drilling',
- 5 => 'engine_idling',
- 6 => 'gun_shot',
- 7 => 'jackhammer',
- 8 => 'siren',
- 9 => 'street_music'
- ];
-
- $path=$categoryDir. DIRECTORY_SEPARATOR. $type_arr[$parts[1]].'\\';
- if (!file_exists($path)) {
- mkdir($path, 0777, true);
- }
- var_dump($filePath);
- var_dump($path);
- var_dump($fileInfo['filename']);
-
-
- // 复制文件到目标目录
- copy($filePath, $path.DIRECTORY_SEPARATOR.$fileInfo['basename']);
- } else {
-
- }
- }
- // 如果是目录,则递归处理该目录
- if (is_dir($filePath)) {
- processDirectory($filePath); // 递归调用
- }
- }
- // 关闭目录
- closedir($handle);
- } else {
- echo "无法打开目录 $dir!";
- }
- }
- // 开始处理根目录
- processDirectory($directoryPath);
|