zoom1 zoom2 zoom3

philippewang.info

SpaceKiller.ml : replaces spaces in file names by underscores.
Copyright (C) 2006 Philippe Wang

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

The Gnu General Public License is available here.

If you want to use it without compiling it to a binary file, I suggest you add this line at the very beginning of the file :
#!/usr/bin/env ocaml
(but you won't be able to compile it with ocamlc or ocamlopt with this line)
(**     SpaceKiller.ml
        Renames files which names contains space characters
        to their name without space. The spaces are replaced
        by underscores. If the new file name exists, "(1)" is
        inserted between the basename and the extension.
        If "foo_bar.ext" and "foo_bar_(1).ext" exist, then
        "foo bar.ext" is renamed to "foo_bar_(2).ext", and so on.
        
        License : Gnu GPL (Gnu General Public License) version 2 or further.
        If you need to use this program and want another license, contact me.
        @author Philippe Wang  ( mail@philippewang.info )
*)
(*      $Id: script.spacekiller.whp,v 1.1 2006/11/02 23:21:51 philippej Exp $ *)

open Sys 

let cwd = getcwd () 
let myFiles = readdir (cwd) 

let replaceSpaceByUnderscore s =
  let r = String.copy s in
(* a copy is made to delete the risk of changing a string
   without wanting to do it *)
    for i = 0 to -1 + String.length s do
      if r.[i] = ' ' 
      then
        r.[i] <- '_'
    done ;
    r

(* let _ = replaceSpaceByUnderscore "hello !" *)

let basename n ext = 
  if n = "" then "" 
  else
    let r = String.copy n in
    let extLength = String.length ext in
      if (String.sub r ((String.length r) - extLength) extLength) = ext
      then
        String.sub r 0 ((String.length n) - extLength)
      else
        r 

(* let _ = basename "trucmp3" "mp3" *)

exception ResFound of string 

let extension fileName =
  let fileNameLength = String.length fileName in
    (try
       for i = fileNameLength - 1 downto 0 do
         if fileName.[i] = '.'
         then
           raise
             (ResFound 
                (String.sub fileName (i) (fileNameLength - i)))
       done ;
       "" 
     with ResFound s -> s 
    ) 

(* let _ = extension "hello..b145.1" *)
(* let _ = basename "hello..b145.1" (extension "hello..b145.1")  *)

let newName n =
  let r = replaceSpaceByUnderscore n in
    if r = n then r else
      if file_exists r then
        let e = (extension n) in
        let rec newName c =
          let a_try = ((basename r e) ^ "_" ^ (string_of_int c) ^ e)
          in
            if file_exists a_try
            then newName (c+1)
            else a_try
        in newName 1  
      else r

      

let main () =
  for i = 0 to -1 + Array.length myFiles do
    (* Printf.printf "%s\n%s\n" myFiles.(i) (newName myFiles.(i)) ; *)
    let n, m = myFiles.(i), (newName myFiles.(i)) in
      if n <> m
      then
        ( print_endline n ;
          print_endline m ;
          rename n m )
  done
in main ()
  

:: philippewang.info ::

:: design & photos by Philippe Wang :: XHTML 1.1 :: CSS 2 :: RSS 2 :: stats :: contact ::
:: Best viewed with Safari or Opera or Firefox or Links :: No SPAM Please ::
 
This page was generated on Sun Nov 18 16:58:32 CET 2007 by BashGXD