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

次の例を試してみてください。

<?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 に答える
1

これは、シェル スクリプトを使用すると非常に簡単に実行できます。で示したファイル リストから始めます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 に答える