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. File Path error [SOLVED]

File Path error [SOLVED]

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
8 Posts 5 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.
  • G Offline
    G Offline
    goldenrose9
    wrote on last edited by
    #1

    i am creating a function IsFolderExists(LPTSTR str) str = "C:\New Folder"; //It Fails to get the Window Folder but when i str = "C:\\New Folder"; //It gets the Windows Folder am i doing wrong, or \\ is required. If required then how to build a path Example : C:\New Folder into C:\\New Folder. Help.....:confused::confused:

    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
    IsFolderExists(lpCmdLine)
    return TRUE;
    }

    void IsFolderExists(LPTSTR str)
    {

    WIN32\_FIND\_DATA wfd;
    HANDLE hFile;
    
    hFile = FindFirstFile(str,&wfd);
    if ( hFile != INVALID\_HANDLE\_VALUE )
    {
    	if ((wfd.dwFileAttributes & FILE\_ATTRIBUTE\_DIRECTORY)!=0)  
    		MessageBox(NULL,\_T("Is Directory"),\_T(""),MB\_OK);  
    	else
    		
    		MessageBox(NULL,\_T("Is File"),\_T(""),MB\_OK);  
    }
    else
    {
    	MessageBox(NULL,\_T("Neither File or Folder"),\_T(""),MB\_OK);  
    }
    

    }

    When i drag a folder [ NewFolder] on myapp.exe it works but when i drag a folder having space [ New Folder ] it doesn't work. please help...

    Some Day I Will Prove MySelf :: GOLD

    modified on Saturday, January 22, 2011 12:42 AM

    M 1 Reply Last reply
    0
    • G goldenrose9

      i am creating a function IsFolderExists(LPTSTR str) str = "C:\New Folder"; //It Fails to get the Window Folder but when i str = "C:\\New Folder"; //It gets the Windows Folder am i doing wrong, or \\ is required. If required then how to build a path Example : C:\New Folder into C:\\New Folder. Help.....:confused::confused:

      int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
      {
      IsFolderExists(lpCmdLine)
      return TRUE;
      }

      void IsFolderExists(LPTSTR str)
      {

      WIN32\_FIND\_DATA wfd;
      HANDLE hFile;
      
      hFile = FindFirstFile(str,&wfd);
      if ( hFile != INVALID\_HANDLE\_VALUE )
      {
      	if ((wfd.dwFileAttributes & FILE\_ATTRIBUTE\_DIRECTORY)!=0)  
      		MessageBox(NULL,\_T("Is Directory"),\_T(""),MB\_OK);  
      	else
      		
      		MessageBox(NULL,\_T("Is File"),\_T(""),MB\_OK);  
      }
      else
      {
      	MessageBox(NULL,\_T("Neither File or Folder"),\_T(""),MB\_OK);  
      }
      

      }

      When i drag a folder [ NewFolder] on myapp.exe it works but when i drag a folder having space [ New Folder ] it doesn't work. please help...

      Some Day I Will Prove MySelf :: GOLD

      modified on Saturday, January 22, 2011 12:42 AM

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      The "\" is known as the (or one of?) "escape" character. that means that if you want to put a tab, for example in a string you would put "\t" to indicate a tablature; that means that the string will look at the "\" and the next character after that and see a "t" and will understand it as a "tablature" (same thing for quotes and double quotes) so, in general, when a string (CString) encounters a "\" in a string it will check the next character and see if it means something. the question is how can I put a literal "\" in string without being recognize as an escape character ? the anwser is to double the "\" ... str = "C:\\Windows"; When hard-coding paths in string one should always put "\\", but, for example, if you have a UI with an edit box to type in the path, you don't need to use the "\\" you can just use the "\".

      Watched code never compiles.

      CPalliniC G 3 Replies Last reply
      0
      • M Maximilien

        The "\" is known as the (or one of?) "escape" character. that means that if you want to put a tab, for example in a string you would put "\t" to indicate a tablature; that means that the string will look at the "\" and the next character after that and see a "t" and will understand it as a "tablature" (same thing for quotes and double quotes) so, in general, when a string (CString) encounters a "\" in a string it will check the next character and see if it means something. the question is how can I put a literal "\" in string without being recognize as an escape character ? the anwser is to double the "\" ... str = "C:\\Windows"; When hard-coding paths in string one should always put "\\", but, for example, if you have a UI with an edit box to type in the path, you don't need to use the "\\" you can just use the "\".

        Watched code never compiles.

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        Of course '\' is the only C/C++ escape character for literal strings. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • M Maximilien

          The "\" is known as the (or one of?) "escape" character. that means that if you want to put a tab, for example in a string you would put "\t" to indicate a tablature; that means that the string will look at the "\" and the next character after that and see a "t" and will understand it as a "tablature" (same thing for quotes and double quotes) so, in general, when a string (CString) encounters a "\" in a string it will check the next character and see if it means something. the question is how can I put a literal "\" in string without being recognize as an escape character ? the anwser is to double the "\" ... str = "C:\\Windows"; When hard-coding paths in string one should always put "\\", but, for example, if you have a UI with an edit box to type in the path, you don't need to use the "\\" you can just use the "\".

          Watched code never compiles.

          G Offline
          G Offline
          goldenrose9
          wrote on last edited by
          #4

          int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
          {
          IsFolderExists(lpCmdLine)
          return TRUE;
          }

          void IsFolderExists(LPTSTR str)
          {

          WIN32\_FIND\_DATA wfd;
          HANDLE hFile;
          
          hFile = FindFirstFile(str,&wfd);
          if ( hFile != INVALID\_HANDLE\_VALUE )
          {
          	if ((wfd.dwFileAttributes & FILE\_ATTRIBUTE\_DIRECTORY)!=0)  
          		MessageBox(NULL,\_T("Is Directory"),\_T(""),MB\_OK);  
          	else
          		
          		MessageBox(NULL,\_T("Is File"),\_T(""),MB\_OK);  
          }
          else
          {
          	MessageBox(NULL,\_T("Neither File or Folder"),\_T(""),MB\_OK);  
          }
          

          }

          When i pass the Folder name from command line like, myapp.exe C:\Windows -result is neither file or folder but when used as myapp.exe C:\\Windows -result is Is Directory. How to solve this. Help.

          Some Day I Will Prove MySelf :: GOLD

          L 1 Reply Last reply
          0
          • G goldenrose9

            int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
            {
            IsFolderExists(lpCmdLine)
            return TRUE;
            }

            void IsFolderExists(LPTSTR str)
            {

            WIN32\_FIND\_DATA wfd;
            HANDLE hFile;
            
            hFile = FindFirstFile(str,&wfd);
            if ( hFile != INVALID\_HANDLE\_VALUE )
            {
            	if ((wfd.dwFileAttributes & FILE\_ATTRIBUTE\_DIRECTORY)!=0)  
            		MessageBox(NULL,\_T("Is Directory"),\_T(""),MB\_OK);  
            	else
            		
            		MessageBox(NULL,\_T("Is File"),\_T(""),MB\_OK);  
            }
            else
            {
            	MessageBox(NULL,\_T("Neither File or Folder"),\_T(""),MB\_OK);  
            }
            

            }

            When i pass the Folder name from command line like, myapp.exe C:\Windows -result is neither file or folder but when used as myapp.exe C:\\Windows -result is Is Directory. How to solve this. Help.

            Some Day I Will Prove MySelf :: GOLD

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            You should also print out the contents of wfd.cFileName to see exactly which file or directory you are displaying attributes for.

            I must get a clever new signature for 2011.

            1 Reply Last reply
            0
            • M Maximilien

              The "\" is known as the (or one of?) "escape" character. that means that if you want to put a tab, for example in a string you would put "\t" to indicate a tablature; that means that the string will look at the "\" and the next character after that and see a "t" and will understand it as a "tablature" (same thing for quotes and double quotes) so, in general, when a string (CString) encounters a "\" in a string it will check the next character and see if it means something. the question is how can I put a literal "\" in string without being recognize as an escape character ? the anwser is to double the "\" ... str = "C:\\Windows"; When hard-coding paths in string one should always put "\\", but, for example, if you have a UI with an edit box to type in the path, you don't need to use the "\\" you can just use the "\".

              Watched code never compiles.

              G Offline
              G Offline
              goldenrose9
              wrote on last edited by
              #6

              how can I put a literal "\" in string without being recognize as an escape character ? the anwser is to double the "\" ... s t r = " C : \ \ W i n d o w s " ; i agree with you. But i want to do it programatically. I want to replace \ with ''\\''. Please help

              Some Day I Will Prove MySelf :: GOLD

              A 1 Reply Last reply
              0
              • G goldenrose9

                how can I put a literal "\" in string without being recognize as an escape character ? the anwser is to double the "\" ... s t r = " C : \ \ W i n d o w s " ; i agree with you. But i want to do it programatically. I want to replace \ with ''\\''. Please help

                Some Day I Will Prove MySelf :: GOLD

                A Offline
                A Offline
                Andrew Brock
                wrote on last edited by
                #7

                The '\\' notation is only needed for the compiler. It isn't needed in the runtime. (when you select a file from the open file dialog, the path is returned as "C:\New Folder" If you want to replace them for other reasons, then use 4 '\'. i.e. to specify a SMB server, you would use CString strServer = "\\\\server_name\\folder";. To replace it you just need a replace function. If you are using something liks CString, it as a Replace() method.

                CString strName = "\\hello";
                strName.Replace("\\", "\\\\");
                //strName is now "\\hello" if it were printed to screen, or "\\\\hello" from the compilers view

                G 1 Reply Last reply
                0
                • A Andrew Brock

                  The '\\' notation is only needed for the compiler. It isn't needed in the runtime. (when you select a file from the open file dialog, the path is returned as "C:\New Folder" If you want to replace them for other reasons, then use 4 '\'. i.e. to specify a SMB server, you would use CString strServer = "\\\\server_name\\folder";. To replace it you just need a replace function. If you are using something liks CString, it as a Replace() method.

                  CString strName = "\\hello";
                  strName.Replace("\\", "\\\\");
                  //strName is now "\\hello" if it were printed to screen, or "\\\\hello" from the compilers view

                  G Offline
                  G Offline
                  goldenrose9
                  wrote on last edited by
                  #8

                  Thanx a lot.:thumbsup:

                  Some Day I Will Prove MySelf :: GOLD

                  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