重命名目录中的文件

时间:2017-03-22 12:30:16

标签: javascript php

嘿伙计我的服务器目录中有大量的amout .mp3文件用于播放音乐,不幸的是大多数文件都是这样的:(艺术家_-_ SongName)。 是否有可能在php或js中创建脚本,它将获取目录中的所有文件并在其中删除'_'符号并留下空格?我真的很感激你的帮助!

2 个答案:

答案 0 :(得分:2)

尝试这样的事情......

foreach (glob('*.mp3') as $filename) {
    rename($filename, str_replace('_', '', $filename));
}

答案 1 :(得分:1)

您可以通过循环浏览目录并使用str_replace()rename()来实现此目的:

<?php 
# IgnoreFiles
$IgnoreFiles = array(".","..","Thumbs.db");

# define your directory
$MP3Directory = "your-directory";

# get directory files
$MP3Files = scandir($MP3Directory);

# loop through the files
foreach($MP3Files as $MP3File){
    # make sure the file isn't in the "ignore" array
    if(!in_array($MP3File, $IgnoreFiles)){
        # get file details
        $MP3Details = pathinfo($MP3Directory."\\".$MP3File);
        # make sure file extension is mp3
        if(strtolower($MP3Details['extension']) == "mp3"){
            # find/replace
            $NewFileName = str_replace("_"," ",$MP3File);
        }
        # rename file
        rename($MP3Directory."\\".$MP3File, $MP3Directory."\\".$NewFileName);
    }
}    
?>