2013-01-07 2 views
0

이름을 바꾸려는 디렉토리에 여러 파일이 있습니다. 나는 기존 파일 이름의 전체 목록을 가지고 있으며, 이전 이름 ​​옆의 열에는 다음과 같이 새 이름 (원하는) 파일 이름이 있습니다. (목록이 Excel에 있으므로 모든 행에 매우 쉽게 구문을 적용 할 수 있습니다)디렉토리에서 파일 배치의 이름을 바꾸는 방법은 무엇입니까?

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

내가 대신 새 파일 이름으로, 현재 디렉토리에 이전 이름과 오래된 파일을 보관하지를 방해하지만, 단지 파일의 복사본을 생성하고 싶습니다.

어떤 언어를 사용해야하고 어떻게해야하는지 잘 모릅니다.

+3

. 확실히 JS 나 SQL – Undefined

+0

은 각 파일에 대해'copy oldname newname'을 포함하는 배치 파일이됩니다. –

+0

어떤 OS를 사용하고 있습니까? – ddinchev

답변

1

그냥 다음 예제를보십시오 : 귀하는 PHP 또는 다른 서버 측 언어를 사용하여보고

<?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.'']);  
} 
?> 
+0

그레이트 솔루션! – Ben

+0

감사합니다. Ben :-) –

2

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

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

편집 : copy의 경우 -이 같은

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

if (!copy($file, $newfile)) { 
    echo "failed to copy $file...\n"; 
} 
?> 
+0

원래 파일을 보관하려고합니다. '이름 바꾸기'대신 '복사'파일. – AlexStack

+0

이 작업을 수행하기 전에 디렉토리를 수동으로 복사해야합니다 ... – Ben

+0

나는 시도한 적이 없지만 자동으로 dir을 만들 수 있습니다! –

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 문서를.

+0

@cerbus가 위의 답변보다 좋습니다. – Ben

+0

내 의견으로는, 그것은 분명합니다. 적어도 더 완벽합니다. (다른 답변은 단순히 PHP 문서의 코드 2 조각을 복사했습니다.) – Cerbrus

+0

위의 대답은 한 번에 하나씩 이름이 바뀌는 반면이 방법은 모든 파일의 이름을 바꿉니다. – Ben

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 
관련 문제