doujuchuan9915 2012-09-19 17:07
浏览 231
已采纳

如何使用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 2012-09-19 17:17
    关注
    $fh = fopen("foo.lst", "w");
    
    foreach ($srcDir as $entity)
        fwrite($fh, $entity->getFilename() . "
    ");
    
    fclose($fh);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用