Solved

How do I use CFIle with path lengths greater than MAX_PATH?

Posted on 2004-04-01
4
881 Views
Last Modified: 2013-11-20
I need to be able to save files into an absolute location whose full path is more than MAX_PATH characters.  My application is not (and can not easily be) in Unicode.  Could I use a relative path name of fewer than MAX_PATH characters and if so, what is the best way to set the desired current directory.  My appication is currently using CFile for file handling.  Please provide a few lines of code by way of an example if not very straightforward.
0
Comment
Question by:PhilipG
4 Comments
 
LVL 15

Assisted Solution

by:lakshman_ce
lakshman_ce earned 100 total points
Comment Utility
Plz have a look at this for obtaining relative file names,
http://www.codeguru.com/Cpp/misc/misc/fileanddirectorynaming/print.php/c263/

-Lakshman
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
You could try _chdir to set the current directory in stages
eg.
_chdir("C:\\temp");
_chdir(".\\subDir1");
_chdir(".\\SubDir2");
and so on where you split your path (recursive function) and move down the directory chain for each directory.
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 150 total points
Comment Utility
You'll still run into problems because CFile::Open() calls a function named AfxFullPath() that expands the file name to the full path with the limitation of _MAX_PATH. Look at FILECORE.CPP in the MFC\SRC directory.

What you might try is to derive a new class from CFile and override the Open method fixing the _MAX_PATH limitations. You'll want to look at the FILECORE.CPP to see what else you'd have to override/fix.

Then you could derive from your new class and attempt to open the long file name.

Good Luck,
Steve
0
 

Author Comment

by:PhilipG
Comment Utility
Thanks all for the very quick response.  I have awarded the points although in the end I adopted a different solution.  The reason for the high point was that I wanted ideas very quickly - it worked.

The solution I used was to use CreateFileW and to convert my path to Unicode using the A2W macro.  I was also able to set the attributes by using SetFileAttributesW and the Unicode path.  The Microsoft info is:

HANDLE CreateFile(
  LPCTSTR lpFileName,
  DWORD dwDesiredAccess,
  DWORD dwShareMode,
  LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  DWORD dwCreationDisposition,
  DWORD dwFlagsAndAttributes,
  HANDLE hTemplateFile
);

Parameters
lpFileName
[in] Pointer to a null-terminated string that specifies the name of the object to create or open.
In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming a File.

Thanks for the quick response.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Separate files macro - Errors on larger data set 18 56
maxBlock challenge 30 99
matchUp  challenge 9 71
wordappend challenge 8 82
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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 tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

7 Experts available now in Live!

Get 1:1 Help Now