How to solve the problem that the file name exceeds 256, and the file name exceeds 256

Source: Internet
Author: User

How to solve the problem that the file name exceeds 256, and the file name exceeds 256

It has been based on basic middleware development and product design, with C language as the foundation, but with the technical characteristics of users' business

And other factors, causing problems that previously products could support less than 256 bytes (that is, 128 Chinese characters), and gradually become a burden or criticism of users or integrators, how to deal with it?

After a thorough investigation, I also collected some comments on the Internet.

If so, isn't the product or software that uses the original C function miserable? Cannot keep up with the times

How can this problem be solved! After investigation, we found that the getextension pathname function can convert a long file name to a 8.3 format, which is used by functions such as _ open. It is straightforward, that is, It shortens the file name, _ open and other functions are recognized. Really, this is the same root (Windows), it's so urgent!

After verification, the following functions are provided to shorten the long file name to support a long file name larger than 256 bytes. There are several key points to remember

After the following conversion, d: \ LongWMY local demo environment \ files \ Alibaba \ db \ 2A384-T-1030 test \ 2A384-T-1033 + test \ 2A384-T-1030 test \ 2A384-T-1033 + test \ 20 330 characters path will be shortened d: \ LONGWM ~ 2 \ files \ DIR1HI ~ 1 \ DIR2HI ~ 1 \ DIE187 ~ 1 \ db \ 2A384 -~ 1 \ 2A384 -~ 1 \ 2A384 -~ 1 \ 2A384 -~ 1 \ 20, 86 characters. In this directory, all files created and operated are OK!

Of course, you certainly do not want to shorten or shorten the Directory and file creation, so the last level of information of the following function input parameters should not be shortened.

<Pre class = "cpp" name = "code"> # define WMY_FULL_FILENAME_LEN512void shortedFileName (char * inFileName, char * outFileName) {int sRet = 0; WCHAR wLongFilePath [bytes + 1] = {0}; WCHAR wPrefixLongFilePath [WMY_FULL_FILENAME_LEN + 1] = {0}; WCHAR w1_filepath [WMY_FULL_FILENAME_LEN + 1] = {0 }; char newFilePath [WMY_FULL_FILENAME_LEN + 1] = {0};/* convert the directory width to byte */char * p = strrchr (inFileName, T_PATHCONNCHR); if (p) {SRet = MultiByteToWideChar (CP_ACP, 0, inFileName, p-inFileName, wLongFilePath, callback);} else {sRet = MultiByteToWideChar (CP_ACP, 0, inFileName,-1, wLongFilePath, WMY_FULL_FILENAME_LEN);}/* added \\? \ Prefix */if (strlen (inFileName)> = 250) {/* is broader than wcslen (inFileName)> = MAX_PATH */sRet = wcscat (wPrefixLongFilePath, L "\\\\\? \ "); SRet = wcscat (wPrefixLongFilePath, wLongFilePath);} else {sRet = swprintf (wPrefixLongFilePath, L" % s ", wLongFilePath );} /* shorten the wide byte directory */sRet = getw.pathnamew (wPrefixLongFilePath, ww.filepath, WMY_FULL_FILENAME_LEN); if (0! = SRet) {sRet = WideCharToMultiByte (CP_OEMCP, NULL, ww.filepath,-1, newFilePath, WMY_FULL_FILENAME_LEN, NULL, FALSE);} else {sRet = GetLastError (); sRet = WideCharToMultiByte (CP_OEMCP, NULL, wLongFilePath,-1, newFilePath, WMY_FULL_FILENAME_LEN, NULL, FALSE);}/* splice the output file name */if (p) sRet = sprintf, "% s % c % s", newFilePath, T_PATHCONNCHR, p + 1); elsesRet = sprintf (outFileName, "% s", newFilePath );}
 

Appendix reference:

Getmediapathname

TheGetmediapathnameFunction retrieves the short path form of a specified input path.

DWORD GetShortPathName(  LPCTSTR lpszLongPath,  // null-terminated path string  LPTSTR lpszShortPath,  // short form buffer  DWORD cchBuffer        // size of short form buffer);

Parameters

LpszLongPath
[In] Pointer to a null-terminated path string. The function retrieves the short form of this path.

Windows NT/2000/XP:In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to nearly 32,000 wide characters, call the Unicode version of the function and prepend "\\? \ "To the path. For more information, see File Name Conventions.

Windows 95/98/Me:This string must not exceed MAX_PATH characters.

Lpsz1_path
[Out] Pointer to a buffer to receive the null-terminated short form of the path specified LpszLongPath.
CchBuffer
[In] Specifies the size, in TCHARs, Of the buffer pointed to Lpsz1_path.
Return Values

If the function succeeds, the return value is the length, inTCHARs, Of the string copiedLpsz1_path, Not including the terminating null character.

IfLpsz1_pathBuffer is too small to contain the path, the return value is the size of the buffer, inTCHARs, Required to hold the path. Therefore, if the return value is greaterCchBuffer, Call the function again with a buffer that is large enough to hold the path.

If the function fails for any other reason, the return value is zero. To get extended error information, callGetLastError.

Remarks

When an application CILS this function and specifies a path on a volume that does not support 8.3 aliases, the function fails with ERROR_INVALID_PARAMETER if the path is longer than 67 bytes.

The path specifiedLpszLongPathDoes not have to be a full or a long path. The short form may be longer than the specifed path.

If the specified path is already in its short form, there is no need for any conversion, and the function simply copies the specified path to the buffer for the short path.

You can setLpsz1_pathTo the same valueLpszLongPath; In other words, you can set the buffer for the short path to the address of the input path string.

You can obtain the long name of a file from the short name by callingGetLongPathNameFunction. Alternatively, whereGetLongPathNameIs not available, you can callFindFirstFileOn each component of the path to get the corresponding long name.

Windows 95/98/Me: getw.pathnamewIs supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined inMicrosoft Layer for Unicode on Windows 95/98/Me Systems.

Requirements

Windows NT/2000/XP:Supported ded in Windows NT 3.5 and later.
Windows 95/98/Me:Supported ded in Windows 95 and later.
Header:Declared in Winbase. h; include Windows. h.
Library:Use Kernel32.lib.
Unicode:Implemented as Unicode and ANSI versions on Windows NT/2000/XP. Also supported by Microsoft Layer for Unicode.

See Also

File I/O Overview, File I/O Functions,FindFirstFile,GetFullPathName,GetLongPathName,SetFileShortName

The old tree (old product) is new, and it depends on how to deal with it.

If you are interested, provide related product solutions to improve the product application scope and benefits, extend the life cycle, and reduce costs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.