Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Code snippet for searching a file in directory (directory can have subdirectories in it)

Code snippet for searching a file in directory (directory can have subdirectories in it)

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmsjson
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    V K 2
    wrote on last edited by
    #1

    Hi, I need to search for a file in a specific directory. This directory would also have subdirectories in it. Can I have a API / code snippet for this. Thanks in advance.

    2 B T 3 Replies Last reply
    0
    • V V K 2

      Hi, I need to search for a file in a specific directory. This directory would also have subdirectories in it. Can I have a API / code snippet for this. Thanks in advance.

      2 Offline
      2 Offline
      224917
      wrote on last edited by
      #2

      There is no single API to do that, however this[^] MSDN sample may help.

      -Suhredayan

      1 Reply Last reply
      0
      • V V K 2

        Hi, I need to search for a file in a specific directory. This directory would also have subdirectories in it. Can I have a API / code snippet for this. Thanks in advance.

        B Offline
        B Offline
        breezes2008_
        wrote on last edited by
        #3

        FindFirstFile、FindNextFile

        1 Reply Last reply
        0
        • V V K 2

          Hi, I need to search for a file in a specific directory. This directory would also have subdirectories in it. Can I have a API / code snippet for this. Thanks in advance.

          T Offline
          T Offline
          T2102
          wrote on last edited by
          #4

          In addition to FindFirstFile, you could use FileExists to search each directory for a match. If you intend to search subdirectories, you still need to to identify them. #include //for FileExists bool FileExists(char * const strFilename) { struct stat stFileInfo; bool blnReturn; int intStat; // Attempt to get the file attributes intStat = stat(strFilename,&stFileInfo); if(intStat == 0) { // We were able to get the file attributes // so the file obviously exists. blnReturn = true; } else { // We were not able to get the file attributes. // This may mean that we don't have permission to // access the folder which contains this file. If you // need to do that level of checking, lookup the // return values of stat which will give you // more details on why stat failed. blnReturn = false; } return(blnReturn); }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups