Results 1 to 8 of 8

Thread: Checking the existence of a symbolic link file

  1. #1
    Join Date
    Mar 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Checking the existence of a symbolic link file

    Hi guys,

    Maybe I'm missing something here, but it appears there is no way to check the existence of a file that is a symbolic link (a shortcut in windows).

    I am using windows. Suppose I have the full file name : "C:/test/a.lnk", and I want to check whether the file "a.lnk" exists or not (and not whether the file it is pointing to exists or not!).
    How can I do this??
    Both QFile::exists() and QFileInfo::exists() methods don't check for the existence of 'a.lnk', but for the existence of the pointed file (bad idea if you ask me).
    Any insights?

    Thanks!
    Ofer

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Checking the existence of a symbolic link file

    A Windows shortcut is not a link. Qt does not offer any API for manipulating Windows shortcuts. Use native API for that.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Checking the existence of a symbolic link file

    Hi,

    I have the same problem and need to check the existence of a *.lnk file, not the target.

    Apparently QFile::exists(), QFileInfo::exists() and QDir::exists() all check the *.lnk target. I also think that its a bad idea how the existence check was implemented.

    Quote Originally Posted by wysota View Post
    A Windows shortcut is not a link. Qt does not offer any API for manipulating Windows shortcuts. Use native API for that.
    ofer88 did not ask how to manipulate Windows shortcuts.

    Perhaps it would be appropriate to file a bug report? Any help is welcome.

    Thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Checking the existence of a symbolic link file

    Quote Originally Posted by HellStorm View Post
    ofer88 did not ask how to manipulate Windows shortcuts.
    Yes, he did. So did you.

    http://en.wikipedia.org/wiki/File_shortcut
    http://en.wikipedia.org/wiki/Soft_link
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Checking the existence of a symbolic link file

    Quote Originally Posted by wysota View Post
    Thanks for replying. This might be offtopic but here we go. I have read those articles and am still not understanding how we are asking for shortcut manipulation.
    Shortcuts (not symbolic links) are files like any other file in Windows. Checking if a file exists is manipulating that file?

    In the meantime I am using native APIs:

    Qt Code:
    1. bool fileExists(LPCTSTR szPath)
    2. {
    3. DWORD dwAttrib = GetFileAttributes(szPath);
    4.  
    5. return (dwAttrib != INVALID_FILE_ATTRIBUTES && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
    6. }
    To copy to clipboard, switch view to plain text mode 

    For reasons why I am using GetFileAttributes() and not FindFirstFile(), resort to the following post:
    http://blogs.msdn.com/b/oldnewthing/...3/5612082.aspx

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Checking the existence of a symbolic link file

    You can try with QFileInfo. QFileInfo::isSymLink() docs seem to suggest the class can differentiate between a shortcut and its target. However QFileInfo::exists() will again work on the link target.

    You should be able to do this:

    Qt Code:
    1. bool shortcutExists(const QString &filePath) {
    2. return !QFileInfo(filePath).symLinkTarget().isEmpty();
    3. }
    To copy to clipboard, switch view to plain text mode 

    Note, if the file is a real symlink (as in NTFS symlink), you might get a false positive.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Feb 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Checking the existence of a symbolic link file

    Quote Originally Posted by wysota View Post
    You should be able to do this:

    Qt Code:
    1. bool shortcutExists(const QString &filePath) {
    2. return !QFileInfo(filePath).symLinkTarget().isEmpty();
    3. }
    To copy to clipboard, switch view to plain text mode 
    That is a nice idea which led to this:

    Qt Code:
    1. bool fileExists(const QString &path)
    2. {
    3. QFileInfo fileInfo(path);
    4. return fileInfo.exists() || fileInfo.isSymLink();
    5. }
    To copy to clipboard, switch view to plain text mode 

    I can't use QFileInfo::symLinkTarget() in a 64bit Windows environment because QtCreator only compiles to 32bit. Win x64 redirects target resolving of links from x86 programs to x86 directories even if they point to the x64 version ("Program Files" vs "Program Files (x86)"). The native Windows functions do this, so there is no workaround apart from writing a small x64 translation program and using IPC for example, which I did but thats another topic.

  8. #8
    Join Date
    Mar 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Checking the existence of a symbolic link file

    Thank you all! That was very helpful.
    And I agree with HellStorm, perhaps it is appropriate to file a bug report regarding the behavior of QFileInfo::exists() etc.

Similar Threads

  1. Qt version checking at the project-file
    By roxton in forum Qt Programming
    Replies: 5
    Last Post: 27th June 2013, 23:43
  2. check IPAddress existence
    By navi1084 in forum Qt Programming
    Replies: 8
    Last Post: 16th May 2013, 21:26
  3. Test for existence of SQLite db
    By scott_hollen in forum Newbie
    Replies: 4
    Last Post: 3rd February 2011, 06:14
  4. QSapecNG Symbolic circuit simulator
    By MySchizoBuddy in forum Qt-based Software
    Replies: 0
    Last Post: 6th December 2010, 13:54
  5. create symbolic link with Qt4
    By alan in forum Qt Programming
    Replies: 2
    Last Post: 27th March 2008, 17:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.