Solved

How to get the path of a file in VC++

Posted on 2004-10-27
932 Views
Last Modified: 2013-11-20
Hi,


I want to find the path of a file for example test.xml.
If test.xml is located in C:\Temp\Data\test.xml, then by giving the file name (test.xml) as input, I want the path as output i.e C:\Temp\Data
I will have only one single copy of the file.
Can anyone please help me to do this?


Thanks
Madhavi
0
Question by:osi-sys
    8 Comments
     
    LVL 6

    Expert Comment

    by:Amritpal Singh
    0
     
    LVL 6

    Expert Comment

    by:Amritpal Singh
    easy way out ,use
    GetFullPathName(string lpFileName , long nBufferLength , ref string lpBuffer , string lpFilePart )

    String ls_buffer = space(255)
    GetFullPathName('trala.txt' , 255, ls_buffer, '' )  
    MessageBox("", "full path : "+ trim( ls_buffer))
    0
     
    LVL 43

    Expert Comment

    by:AndyAinscow
    Store the path of the file (eg in registry).

    When you require this path get it from the registry.  If it doesn't exist then you can find the location of the file with CFileFind.  You will need to have it in a recursive call until it finds the location of the file and stores in registry.
    0
     

    Author Comment

    by:osi-sys
    Hi amrit_82

    My XXX.xml file is in C:\Project\LogConfig\exe\XML\
    and my .dsw project file is in  C:\Project\LogConfig\LogApp
    and my .exe is in C:\Project\LogConfig\exe

    So if I call String ls_buffer = space(255)
    GetFullPathName('XXX.xml , 255, ls_buffer, '' )  

    ls_buffer is returning C:\Project\XXX.xml instead of  C:\Project\LogConfig\exe\XML\XXX.xml

    Can you please let me know how to get the correct path

    Thanks
    Madhavi
    0
     
    LVL 43

    Expert Comment

    by:AndyAinscow
    If you look at GetFullPathName in help you will find it uses the current path and drive.
    You need to adopt an approach as I outlined.  (If, however, this file will ALWAYS be in the same directory as your app then you can use a shortcut and get the path of the exe)
    0
     

    Author Comment

    by:osi-sys
    If, however, this file will ALWAYS be in the same directory as your app then you can use a shortcut and get the path of the exe ---- My file will always be in the same directory. Can you please let me know how to get the path of the exe.
     I cannot hard code and store the path in registry.

    Thanks
    Madhavi
    0
     
    LVL 6

    Accepted Solution

    by:
    hi madhavi,
    while going through msdn articles ,what i found is that GetFullPathName just append s the filename to the current directory path.so,i think that won't work

    however if ur XML folder is always in your exe's path u can do this

    1.find the path to the exe by writing this code (this would work for sure) :))

    HINSTANCE hInst = AfxGetInstanceHandle();
    char szFileName[MAX_PATH];
    ::GetModuleFileName(hInst, szFileName, MAX_PATH);

    2. find the last position of "\" in szFileName (u can use FindReverse )
     
    3. retrieve the string upto that position.

    4.add "\XML\filename" to the resulting string

    0
     

    Author Comment

    by:osi-sys
    Hi amrit_82 & AndyAinscow

    Thanks for all your inputs and help. I am able to retrieve the path now.

    thanks
    madhavi
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Course: JavaScript Coding - Massive 12-Part Bundle

    Regardless of your programming skill level, you'll go from basics to advanced concepts in a vast array of JavaScript subjects including Sammy.js, Agility.js, Ember.js, Node.js, jQuery, AJAX, Extjs, AngularJS, Knockout.js, and JSON.

    This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
    Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now