2016-01-13 29 views
0

我想用循環重命名420個文件,但我不知道如何。如何用循環重命名大量文件?

在我的項目中,我用fopen()創建了420張圖片;

fopen($meal->imagepath, "w"); 

由於對現有文件的fopen搜索,我沒有了這些,該功能通過循環創建了420個文件,他們有正確的名稱。現在的問題是,這些圖像沒有像高度,寬度或背景顏色的屬性。

因此我想在paint中手動創建一個文件並拷貝這個文件420次,然後用我的420個正確的名字引用一個txt文件來重命名它們。我認爲這應該是可能的,但如何?

感謝您

+0

根據操作系統或stdio的重命名功能,您可能只需要使用系統調用mv或重命名。 http://www.cplusplus.com/reference/cstdio/rename/ – MiltoxBeyond

回答

2

由於這聽起來像一個一次性的事情,我離開了錯誤處理

PHP直接複製文件

foreach(file('names.txt', FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES) as $target) 
    copy('source.png', $target); 
} 

還看到:http://docs.php.net/copyhttp://docs.php.net/function.file

php創建批處理文件:

file_put_contents('rename.sh', '#!/bin/bash'); 
foreach(file('names.txt', FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES) as $target) 
    file_put_contents('rename.sh', "cp source.png $target\r\n", FILE_APPEND); 
} 

還看到:只有http://docs.php.net/file_put_contents

殼/ xargs的:

<source.txt xargs -L 1 cp source.png 

還看到:https://en.wikipedia.org/wiki/Xargs

PowerShell的:

gc .\source.txt | %{ copy source.png $_ } 

見:Using the Get-Content Cmdlet

+0

它的工作原理! 非常感謝你的男人! :-) – CodeVolunteer

-1
foreach (glob("*.txt") as $filename) { 
    file_put_contents('new_name',file_get_contents('path/to/file/'.$filename)); }