[UPHPU] Removing Spaces in Files/Dir's

David Smith DavidSmith at byu.net
Wed Nov 17 22:28:35 MST 2004


I just did minimal testing in PHP4.

--Dave

<quote who="Keith Pettit">
> Did you test this script with PHP 5?  The script executes, but dosen't
> look like it gets very far.  It dosen't actually rename the files to get
> rid of the spaces.
>
> Anywyas, thanks a ton for taking the time to put that together.  I did
> add a couple lines so I could execute it from the commandline.  Here is
> what I have.
>
> Keith
>
>
>
>
>
>
> #!/usr/bin/php
> <?php
>
> $dir = $argv["1"];
>
> remove_spaces($dir);
>
> /**
>   * Removes spcaes from all files and directories
>   * of the given directory. Also does the same thing to
>   * all sub-directories.
>   */
> function remove_spaces($dir_name)
> {
>      echo "\nRemoving spaces from $dir_name\n\n";
>      $replace_char = " ";
>      $replace_with_char = "_";
>      $separator = '/';
>
>      $dir_name = str_replace( '\\', $separator, $dir_name );
>      $dir = @opendir( $dir_name );
>      if( $dir == false )
>          return false;
>      while( ( $file = readdir( $dir ) ) != false ) {
>          if( $file == '.' || $file == '..' )
>              continue;
>          $file_full_name = $dir_name . $separator . $file;
>          // Recurse?
>          if( is_dir( $file_full_name ) )
>              if( remove_spaces( $file_full_name ) )
>                  continue;
>              else
>                  return false;
>          // Shall we rename this file?
>          if( false !== strpos( $file, $replace_char ) ) {
>              $new_file_name = $dir_name . $separator .
>                  str_replace( $replace_char, $replace_with_char, $file );
>              echo "Renaming $file_full_name to $new_file_name\n";
>              if( ! @rename( $file_full_name, $new_file_name ) )
>                  return false;
>          }
>      }
>
>      closedir( $dir );
>
>      // Rename the directory too
>      if( false !== strpos( $dir_name, $replace_char ) ) {
>          $new_dir_name = str_replace( $replace_char, $replace_with_char,
> $dir_name );
>          if( ! @rename( $dir_name, $new_dir_name ) )
>                  return false;
>      }
>
> }
>
>
>
>
>
>
>
>
>
>
> David Smith wrote:
>> <quote who="Keith Pettit">
>>
>>>I'm wanting to do a script that removes spaces in files and directories
>>>recursively.  But I'm sure this has to have been done before.
>>>
>>>Anybody know of a script that does this?
>>
>>
>> This ought to get the job done for you:
>>
>>    http://linode.thesmithfam.org/spaces.phps
>>
>> This is untested in production, but it passed my little unit tests, so
>> use at your own risk.
>>
>> --Dave



More information about the UPHPU mailing list