2012-09-19 46 views
0

我是linux新手,嘗試在我嘗試編寫的PHP腳本中使用bash命令,我需要編寫每個PDF文件名從一個目錄中,我通過使用PHP中的DirectoryIterator()迭代到.lst文件,該文件位於另一個文件夾中,並且每個文件名都列在文本文件的新行中。如何使用bash命令將文件名列表寫入.lst文件

.lst文件應該是這樣的:

次數1.pdf
2.pdf
3.PDF
...

我希望這是有道理的。任何幫助/方向將不勝感激。

這裏是我想出來的,到目前爲止的代碼:

// Use the command line cp to copy each PDF file in the sourceDir to the destinationDir. 
foreach ($srcDir as $entity) { 

    /** 
    * @var DirectoryIterator $entity 
    */ 
    /*$result = rename($sourceDir . '/' . $entity->getFilename(), $destinationDir .'/' . $entity->getFileName()); 
    if(!$result) 
    { 
     throw Exception('Could not copy file ' . $entity->getFilename() . 'to destination directory '); 
    }*/ 

    $cpString = 'cp ' . $sourceDir . '/' . $entity->getFilename() . ' ' . $destinationDir .'/' . $entity->getFileName() . ' 2<&1'; 
    passthru($cpString, $returnVar); 

    if($entity->isFile() && strtoupper($entity->getExtension()) == 'PDF') 
    { 
     $cpString = 'cp ' . $sourceDir . '/' . $entity->getFilename() . ' ' . $destinationDir .'/' . $entity->getFileName(); 
     if ($counter = 1) { 
      $catString = 'cat ' . $destinationDir . '/' . $batchNum . '.lst'; 
     } 
     $cpString = '' 
    } 
    $counter++; 
} 
+0

@下面grawity的答案應該是完美的。是否有某些原因需要使用bash命令?您是否還試圖將文件複製到某處? – terdon

+0

是的。我的老闆想讓我在我的PHP代碼中使用bash行命令,因爲我是bash和PHP腳本的新手,所以我對它們更加熟悉。希望這是有道理的。謝謝。 – Melinda

+0

像[本答案](http://superuser.com/a/474581/57398)中描述的'echo -n'選項可能會對您有所幫助。 – martineau

回答

2
$fh = fopen("foo.lst", "w"); 

foreach ($srcDir as $entity) 
    fwrite($fh, $entity->getFilename() . "\n"); 

fclose($fh); 
+0

感謝您的快速回復。我會試一試。問候。 – Melinda

相關問題