Jump to content

renaming all files in a directory, replacing spaces with underscore


Recommended Posts

I need to know how to rename files in a directory using php, specifically just files that have a space in the filename.  I want to replace those spaces with underscores.  First I need to know to to get a list of just the filenames that have spaces in them.  What's the best way to do this?  I'm thinking get a list of all the filenames and foreach the array, then split each filename into each character and check if that character is a space, then add that filename to another array, then foreach that other array and str_replace() each filename to replace spaces with the desired charcter.  Is there a better way?

depending on what files you want to rename (if its all, only gifs, etc.) you can use the rename() function in combination with glob().

 

something like this would work for all file types (assuming you are in the same directory

$files = glob("*.*");//get all files in directory (note, if you need to set
//the directory, you should so something like glob("/path/to/*.*");

foreach($files as $file){
//$file is each individual file
//replace spaces with underscores
$new_file = str_replace(" ", "_", $file);
rename($file, $new_file);//rename file
}

 

a little side note, you can add an if statement using the logic that PFMaBiSmAd suggested to only rename files with spaces if you want

Same/similar to above ^^^ -

<?php
$dir = "folder/*.*"; // where and what files to operate on 
$files = glob($dir);
foreach($files as $file){
if($file != $ren = str_replace(" ","_",$file)){
	// there was a space
	rename($file,$ren);
	echo "Renaming: [$file] to [$ren]<br />";
}
}
?>

Well I'm going to be processing all files with .wma, .mp3, and .wav extensions, would that be similar enough code?  I can do this:

 

<?php
$path = "./";
$handle = opendir($path);
$allow = array("wma", "mp3", "wav");
while(($file = $readdir($handle) != false) {
  $ext = substr(strtolower($file), strrpos($file, ".")+1, strlen($file));
  if(filetype($path.$file) == file && in_array($ext, $allow)) { // Only adds the files to the array if the extension is in the $allow array
    $files[] = $file;
    }
  }

foreach($files as $file) {
  if($file != ($ren = str_replace(" ", "_", $file))) {
    rename($file, $ren);
    echo "File ".$file." renamed to ".$ren;
    } else {
    echo "No spaces in filename, skipping";
    }
  echo "<br>";
  }
?>

 

That should get the job done, right?  And I only need to do this once, not like, on a schedule or anything.  It's just because I have files already there that have spaces that need to not have spaces, future uploaded files will have no spaces, I just don't feel like manually renaming hundreds of files.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.