0

我想重命名的目录中有一堆文件。我有一个完整的现有文件名列表,在旧名称旁边的列中,我有一个新名称(所需的)文件名,如下所示:(列表在 excel 中,因此我可以很容易地对所有行应用一些语法)

OLD NAME         NEW NAME
--------         --------
aslkdjal.pdf     asdlkjkl.pdf
adkjlkjk.pdf     asdlkjdj.pdf

我想将旧名称和旧文件保留在其当前目录中并且不打扰它们,而只是创建文件的副本,而不是使用新文件名。

不知道使用什么语言以及如何去做。

4

4 回答 4

2

http://php.net/manual/en/function.rename.php

<?php
rename("/tmp/tmp_file.txt", "/home/user/login/docs/my_file.txt");
?>

编辑:在复制的情况下-

<?php
$file = 'example.txt';
$newfile = 'example.txt.bak';

if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}
?>
于 2013-01-07T08:26:15.563 回答
2

像这样的东西应该工作:

$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.
}

RecursiveDirectoryIterator文档。

于 2013-01-07T08:28:27.727 回答
1

使用 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
于 2013-01-07T08:35:51.207 回答
1

只需尝试以下示例:

<?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.'']);    
}
?>
于 2013-01-07T10:41:21.123 回答