Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
y option 920d243 Feb 16, 2015
1 contributor

Users who have contributed to this file

executable file 122 lines (108 sloc) 2.04 KB
#!/usr/bin/php
<?php
/*
Requires: php 5.x
*/
// 使い方
function help(){
print <<<EOS
Usage: rename_to_utf8 [option]
Option:
-r Scan files recursively.
-y Answer yes for all questions.
-h (-?) Show help
EOS;
}
// 起動オプション
global $opt_r;
global $opt_y;
$opt_r = false;
$opt_y = false;
$opt_h = false;
foreach($argv as $arg){
if($arg === '-r'){
$opt_r = true;
}
else if($arg === '-y'){
$opt_y = true;
}
else if($arg === '-h' || $arg === '-?'){
$opt_h = true;
}
}
// ヘルプ表示
if($opt_h){
help();
exit;
}
// グローバル結果
global $ok, $ng, $skip;
$ok = 0;
$ng = 0;
$skip = 0;
function myscan($dir){
global $opt_r;
global $opt_y;
global $ok, $ng, $skip;
$dirhandle = opendir($dir);
while($file = readdir($dirhandle)){
// Skip /.*/
if($file[0] === '.')continue;
// パス (※フルパスとは限らない。元が '.' から始まることもあるので)
$path = "$dir/$file";
print "...checking $path\n";
//
if($opt_r && is_dir($path)){
myscan($path);
}
// 自分自身
$utf8_file = mb_convert_encoding($file, 'utf8', 'utf8,sjis');
// $utf8_file = exec("echo " . escapeshellarg($file) . " | nkf -w");
if($file !== $utf8_file){
$newpath = "$dir/$utf8_file";
print "--------------------------\n";
print "Confirm to rename\n";
print " OldPath: $path\n";
print " NewPath: $newpath\n";
print "--------------------------\n";
while(1){
if($opt_y){
$input = 'y';
}
else{
$input = readline("y or n> ");
$input = strtolower($input);
}
if($input === 'y'){
// リネーム実行
$b = rename($path, $newpath);
if($b){
$ok++;
print "ok.\n";
}
else{
$ng++;
print "error.\n";
}
break;
}
elseif($input === 'n'){
$skip++;
print "skip.\n";
break;
}
else{
continue;
}
}
print "\n";
}
}
closedir($dirhandle);
}
myscan('.');
print "\n----\n";
print "ok count:$ok\n";
print "error count:$ng\n";
print "skip count:$skip\n";
?>
You can’t perform that action at this time.