test2.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. require('../vendor/autoload.php');
  3. // 定义要查询的目录路径
  4. $directoryPath = 'F:\声音文件\bark sound\UrbanSound8K_Expand_verison\UrbanSound8K_Expand_verison\audio';
  5. // 定义分类的目标目录路径
  6. $categoryDir = 'F:\声音文件\bark sound\category_new';
  7. // 确保目标目录存在
  8. if (!file_exists($categoryDir)) {
  9. mkdir($categoryDir, 0777, true);
  10. }
  11. // 递归函数,用于遍历文件夹并处理文件
  12. function processDirectory($dir) {
  13. global $categoryDir;
  14. // 打开目录
  15. if ($handle = opendir($dir)) {
  16. // 读取目录中的文件
  17. while (false !== ($entry = readdir($handle))) {
  18. // 排除 "." 和 ".."
  19. if ($entry == "." || $entry == "..") {
  20. continue;
  21. }
  22. // 获取文件或目录的完整路径
  23. $filePath = $dir . DIRECTORY_SEPARATOR . $entry;
  24. // 如果是文件,则进行分类处理
  25. if (is_file($filePath)) {
  26. $fileInfo = pathinfo($filePath);
  27. $parts = explode('-', $fileInfo['filename']);
  28. // 获取第二个下标的元素并判断是否在 1 到 9 之间
  29. if (isset($parts[1]) && is_numeric($parts[1]) && $parts[1] >= 0 && $parts[1] <= 9) {
  30. $type_arr=[
  31. 0 => 'air_conditioner',
  32. 1 => 'car_horn',
  33. 2 => 'children_playing',
  34. 3 => 'dog_bark',
  35. 4 => 'drilling',
  36. 5 => 'engine_idling',
  37. 6 => 'gun_shot',
  38. 7 => 'jackhammer',
  39. 8 => 'siren',
  40. 9 => 'street_music'
  41. ];
  42. $path=$categoryDir. DIRECTORY_SEPARATOR. $type_arr[$parts[1]].'\\';
  43. if (!file_exists($path)) {
  44. mkdir($path, 0777, true);
  45. }
  46. var_dump($filePath);
  47. var_dump($path);
  48. var_dump($fileInfo['filename']);
  49. // 复制文件到目标目录
  50. copy($filePath, $path.DIRECTORY_SEPARATOR.$fileInfo['basename']);
  51. } else {
  52. }
  53. }
  54. // 如果是目录,则递归处理该目录
  55. if (is_dir($filePath)) {
  56. processDirectory($filePath); // 递归调用
  57. }
  58. }
  59. // 关闭目录
  60. closedir($handle);
  61. } else {
  62. echo "无法打开目录 $dir!";
  63. }
  64. }
  65. // 开始处理根目录
  66. processDirectory($directoryPath);