# Thread: File exists on server

1. Beginner
Join Date
Aug 2011
Posts
14
Thanks
1

## File exists on server

Simple question:

Is there anyway to tell if a file exists on a server or network address such as //192.168.0.2/foo/bar.txt

I use QFile::exists( "\\192.168.0.2\foo\bar.txt" ) which works perfectly when connected to the server but say I was in an airport and not connected to the same network, the exists() function hangs and does not timeout.

Is there any way to detect if there is a connection to the server?

2. ## Re: File exists on server

Originally Posted by Chops211
Simple question:

Is there anyway to tell if a file exists on a server or network address such as //192.168.0.2/foo/bar.txt

I use QFile::exists( "\\192.168.0.2\foo\bar.txt" ) which works perfectly when connected to the server but say I was in an airport and not connected to the same network, the exists() function hangs and does not timeout.

Is there any way to detect if there is a connection to the server?

I believe this is what you are looking for.

Regards,

3. Beginner
Join Date
Aug 2011
Posts
14
Thanks
1

## Re: File exists on server

Thank you. I tried this before and I wanted something much simpler that doesn't require using sockets. I want to leave as much as I can to the OS. I really would like a QFile::exists() function with a timeout. or QDir()

4. Expert
Join Date
Jan 2006
Location
Belgium
Posts
1,938
Thanked 268 Times in 268 Posts
Qt products
Platforms
Wiki edits
20

## Re: File exists on server

I guess one of the easier things to do is use ifconfig or ipconfig and parse the results.

The correct way to do this is to use platform specific functions and this will require you to write some code.

There are only a few things you get for free: the sun going up, death and taxes.
So don't be lazy and start writing a little bit of code.

Join Date
Dec 2008
Location
Poland
Posts
383
Thanks
52
Thanked 42 Times in 42 Posts
Qt products
Platforms

## Re: File exists on server

To add up to what was posted You could either use QProcess with ping and check result or simply:
Qt Code:
void MainWindow::someFunction(){    QTcpSocket *sock = new QTcpSocket( this );    connect( sock, SIGNAL(hostFound()), this, SLOT( checkFile() ) );    sock->connectToHost( "192.168.0.2", 421 ); } void MainWindow::checkFile(){    qDebug() << "File Exists?:" << QFile::exists( "\\192.168.0.2\foo\bar.txt" );}
To copy to clipboard, switch view to plain text mode

or beter use SIGNAL( stateChanged(QAbstractSocket::SocketState) )
Its add a few lines to Your code and is IMHO simple.

6. ## The following user says thank you to Talei for this useful post:

Chops211 (6th August 2011)

7. Beginner
Join Date
Aug 2011
Posts
14
Thanks
1

## Re: File exists on server

Thanks everyone. Using ifconfig or ipconfig doesn't really make sense when I could just use sockets but thank you for the idea.