Php 如何重命名目录中的一批文件?
我有一个目录,我想重命名一堆文件。我有一个现有文件名的完整列表,在旧名称旁边的列中,我有一个新名称(所需的)文件名,如下所示:(列表在excel中,因此我可以很容易地对所有行应用一些语法) 我希望将旧名称和旧文件保留在它们的当前目录中,并且不干扰它们,但是只创建一个文件副本,而使用新文件名 不确定使用哪种语言以及如何执行此操作。Php 如何重命名目录中的一批文件?,php,Php,我有一个目录,我想重命名一堆文件。我有一个现有文件名的完整列表,在旧名称旁边的列中,我有一个新名称(所需的)文件名,如下所示:(列表在excel中,因此我可以很容易地对所有行应用一些语法) 我希望将旧名称和旧文件保留在它们的当前目录中,并且不干扰它们,但是只创建一个文件副本,而使用新文件名 不确定使用哪种语言以及如何执行此操作。 编辑:如果是- 编辑:如果是- 类似的方法应该可以: $source = '/files/folder'; $target = '/files/newFol
编辑:如果是-
编辑:如果是-
类似的方法应该可以:
$source = '/files/folder';
$target = '/files/newFolder';
$newnames= array(
"oldfilename" => "newfilename",
"oldfilename1" => "newfilename1",
);
// Copy all files to a new dir
if (!copy($source, $target)) {
echo "failed to copy $source...\n";
}
// Iterate through this dir, rename all files.
$i = new RecursiveDirectoryIterator($target);
foreach (new RecursiveIteratorIterator($i) as $filename => $file) {
rename($filename, $newnames[$filename]);
// You might need to use $file as first parameter, here. Haven't tested the code.
}
文档。类似的内容应该可以:
$source = '/files/folder';
$target = '/files/newFolder';
$newnames= array(
"oldfilename" => "newfilename",
"oldfilename1" => "newfilename1",
);
// Copy all files to a new dir
if (!copy($source, $target)) {
echo "failed to copy $source...\n";
}
// Iterate through this dir, rename all files.
$i = new RecursiveDirectoryIterator($target);
foreach (new RecursiveIteratorIterator($i) as $filename => $file) {
rename($filename, $newnames[$filename]);
// You might need to use $file as first parameter, here. Haven't tested the code.
}
文档。使用shell脚本很容易做到这一点。从
files.txt
中显示的文件列表开始
#!/bin/sh
# Set the 'line' delimiter to a newline
IFS="
"
# Go through each line of files.txt and use it to call the copy command
for line in `cat files.txt`; do
cp `echo $line | awk '{print $1;}'` `echo $line | awk '{print $2};'`;
done
使用shell脚本很容易做到这一点。从
files.txt
中显示的文件列表开始
#!/bin/sh
# Set the 'line' delimiter to a newline
IFS="
"
# Go through each line of files.txt and use it to call the copy command
for line in `cat files.txt`; do
cp `echo $line | awk '{print $1;}'` `echo $line | awk '{print $2};'`;
done
试试下面的例子:
试试下面的例子:
您正在考虑使用PHP或其他服务器端语言。绝对不是JS或SQLmake它是一个批处理文件,包含每个文件的copy oldname newname
。服务器是LAMP我正在笔记本电脑上使用windows您正在使用PHP或其他服务器端语言。绝对不是JS或SQLmake,它是一个批处理文件,包含每个文件的copy oldname newname
。服务器是LAMP,我在笔记本电脑上使用windows,他想保留原始文件。”复制“文件”而不是“重命名”。@BenJones:不,请看我的答案。他想保留原始文件复制“文件”而不是“重命名”。@BenJones:不,请看我的答案。@cerbus这比上面的答案好吗?在我看来,显然是这样。至少它更完整。(另一个答案只是从php文档中复制了两段代码)我说这个方法将重命名所有文件,而上面的答案一次只重命名一个文件,这是正确的。假设您有一个列有所有转换的$newnames
数组,这将是一个完全自动的解决方案。我在excel中有一个列表,包含1000多个旧名称和新名称,并排排列,这就是我将名称放在数组中的位置。。。明白了。现在,这不是缺少了像i++?cerbus这样的东西吗?这比上面的答案好吗?在我看来,显然是这样。至少它更完整。(另一个答案只是从php文档中复制了两段代码)我说这个方法将重命名所有文件,而上面的答案一次只重命名一个文件,这是正确的。假设您有一个列有所有转换的$newnames
数组,这将是一个完全自动的解决方案。我在excel中有一个列表,包含1000多个旧名称和新名称,并排排列,这就是我将名称放在数组中的位置。。。明白了。这不是缺少了像i++这样的东西吗?
#!/bin/sh
# Set the 'line' delimiter to a newline
IFS="
"
# Go through each line of files.txt and use it to call the copy command
for line in `cat files.txt`; do
cp `echo $line | awk '{print $1;}'` `echo $line | awk '{print $2};'`;
done
<?php
$source = '../_documents/fees';
$target = '../_documents/aifs';
$newnames= array(
"1276.aif.pdf" => "aif.10001.pdf",
"64.aif.20091127.pdf" => "aif.10002.pdf",
);
function recurse_copy($src,$dst) {
$dir = opendir($src);
@mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}
// Copy all files to a new dir
recurse_copy($source, $target);
// Iterate through this dir, rename all files.
$i = new RecursiveDirectoryIterator($target);
foreach (new RecursiveIteratorIterator($i) as $filename => $file) {
@rename($filename, $target.'/'.$newnames[''.$i.'']);
}
?>