doujuchuan9915
doujuchuan9915
2012-09-19 17:07

如何使用bash命令将文件名列表写入.lst文件

已采纳

I am new to linux and trying to work with bash commands within a PHP script that I'm attempting to write, I am needing to write each PDF file name from a directory that I'm iterating through using the DirectoryIterator () in PHP to a .lst file that I have sitting in another folder and have each file name listed on a new line in the text file.

The .lst file should look like this:

1.pdf
2.pdf
3.pdf
...

I hope this makes sense. Any HELP/DIRECTION would be greatly appreciated.

Here is the code that I've come up with so far:

// 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++;
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • du77887 du77887 9年前
    $fh = fopen("foo.lst", "w");
    
    foreach ($srcDir as $entity)
        fwrite($fh, $entity->getFilename() . "
    ");
    
    fclose($fh);
    
    点赞 评论 复制链接分享