Skip to main content

Command Palette

Search for a command to run...

Rename script

Updated
1 min read
K

Khanh is a full stack web developer with over 15 years of experience developing for the web

Đô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 :))

/* * 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'; } } } } } } }

More from this blog

K

Khánh's blog

146 posts