Rename script

Đôi khi cần phải đổi tên file hàng loạt trên server. Ngoài cách “đơn giản” nhất, nhanh nhất (túm lại cái gì cũng nhất :D) là dùng shell script, thì còn cách nữa đó là dùng PHP script để đổi. Không yêu cầu nhiều về permission. Tất nhiên, cái giá phải trả ở đây là tốc độ thực thi ;) Với những thư mục có cỡ vài trăm nghìn file thì….thôi, đừng nên dùng cách này :))

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
 * Created on Mar 28, 2008
 * Author: Do Nam Khanh
 * Email: donamkhanh@gmail.com
 * File name: rename.php
 *
 */
 
    error_reporting(E_ALL);
    @set_time_limit(0); // No time limit
 
    $f="data.txt";
    $ft=fopen($f,"r");
    if(!$ft) {
        echo "File is empty now.";
        exit;
    }
    else {
       $directory = dir('p');
        while (!feof($ft)) {
            $list=fgets($ft,1000);
            if($list!='') {
                $filename = trim(substr($list,11));
                while (false !== ($temp = $directory->read())) {
                    if ($temp{0}!='.' && $temp{0}!='..') {
                       $temp = trim($temp);
                        if($temp == $filename) {
                            if(!rename("p/$temp", "p/new/$list")) {
                                echo 'Error';
                            }
                        }
                    }
                }
            }
        }
    }
[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

0 Responses to “Rename script”


  1. No Comments

Leave a Reply

CAPTCHA image