如何使用QFtp :: list和QFtp :: listInfo检查目录是否存在

时间:2022-10-12 23:15:33

I try to make ftp uploader which creates automatically subdirectories and put files in them.

我尝试制作ftp上传器,自动创建子目录并将文件放入其中。

My problem is that:

我的问题是:

  • my uploader doesn't upload files if directory already exists.
  • 如果目录已存在,我的上传器不会上传文件。

  • it fails on next mkdir (no error just it doesnt upload file).
  • 它在下一个mkdir上失败(没有错误只是它没有上传文件)。

I know QFtp have list() and listInfo() but I don't understand how I should use in my code.

我知道QFtp有list()和listInfo(),但我不明白我应该如何在我的代码中使用。

Here is how I implemented my uploader:

以下是我实现上传器的方法:

ftp = new QFtp();
ftp->setTransferMode(QFtp::Passive);

ftp->connectToHost(g.ftp_host);
ftp->login(g.ftp_user,g.ftp_password);
ftp->list();
ftp->cd(g.ftp_defaultdir+g.phocadir);

for (int i = 0; i<FulluploadFilenames->size(); i++){
    ftp->mkdir(_taj->at(i)); // should check if exists
    ftp->cd(_taj->at(i));
    ftp->mkdir(_year->at(i));
    ftp->cd(_year->at(i));
    ftp->mkdir(_month->at(i));
    ftp->cd(_month->at(i));

    qdata = new QFile(FulluploadFilenames->at(i),this);
    if (qdata->open(QIODevice::ReadOnly)) {
        ftp->put(qdata,uploadFilenames->at(i));
        ftp->list();
    }

    ftp->cd("../");
    ftp->cd("../");
    ftp->cd("../");
}
ftp->close();

This make and cd to something like this: /123123123/2015/05/ and uploads file here.

这使得和CD成为这样的东西:/ 123123123 / 2015/05 /并在这里上传文件。

Could somebody please help how to check if directory exists?

有人可以帮助如何检查目录是否存在?

UPDATE:

Instead of checking if directories exists I forced mkdir to work in sync so it doesn't matter if fails.

我没有检查目录是否存在,而是强制mkdir同步工作,所以如果失败则无关紧要。

void MainWindow::doUpload(){ 
ftp = new QFtp();
ftp->connectToHost(g.ftp_host);
ftp->login(g.ftp_user,g.ftp_password);
ftp->cd(g.ftp_defaultdir+g.phocadir);
ftp->list();
// making directories
for (int i = 0; i<FulluploadFilenames->size(); i++){
    runCommand(ftp, ftp->mkdir(_taj->at(i)));
    runCommand(ftp, ftp->mkdir(_taj->at(i)+"/"+_year->at(i)));
    runCommand(ftp, ftp->mkdir(_taj->at(i)+"/"+_year->at(i)+"/"+_month->at(i)));
}
// uploading files
for (int i = 0; i<FulluploadFilenames->size(); i++){
    ftp->cd(_taj->at(i)+"/"+_year->at(i)+"/"+_month->at(i));

    qdata = new QFile(FulluploadFilenames->at(i),this);
    if (qdata->open(QIODevice::ReadOnly)) {
        ftp->put(qdata,uploadFilenames->at(i));
    }

    ftp->cd("../../../");

    ui->progressBar->setValue(i+1);
    ui->progressBar->update();
    QCoreApplication::processEvents();
}
ftp->close();
}

...

void MainWindow::runCommand(QFtp *ftp, int commandId){
QEventLoop loop;
connect(ftp, SIGNAL(commandFinished(int, bool)), &loop, SLOT(quit()));
loop.exec();
}

1 个解决方案

#1


Function list() work asynchronously. After you call this function it immediately returns unique identifier. This id passed in commandStarted() signal when command started and in commandFinished() signal when list command finished. Between this two signals listInfo() signal is emitted for each directory entry found.

函数列表()异步工作。调用此函数后,它会立即返回唯一标识符。当命令启动时,此id传入commandStarted()信号,当list命令完成时,传入commandFinished()信号。在这两个信号之间,为每个找到的目录条目发出listInfo()信号。

So before upload to directory you must check all QUrlInfo objects, passed from listInfo() signals until commandFinished() signal emitted.

因此,在上传到目录之前,必须检查从listInfo()信号传递的所有QUrlInfo对象,直到发出commandFinished()信号。

#1


Function list() work asynchronously. After you call this function it immediately returns unique identifier. This id passed in commandStarted() signal when command started and in commandFinished() signal when list command finished. Between this two signals listInfo() signal is emitted for each directory entry found.

函数列表()异步工作。调用此函数后,它会立即返回唯一标识符。当命令启动时,此id传入commandStarted()信号,当list命令完成时,传入commandFinished()信号。在这两个信号之间,为每个找到的目录条目发出listInfo()信号。

So before upload to directory you must check all QUrlInfo objects, passed from listInfo() signals until commandFinished() signal emitted.

因此,在上传到目录之前,必须检查从listInfo()信号传递的所有QUrlInfo对象,直到发出commandFinished()信号。