Testing File Path Equivalence

The program has two lines. Each of them contains a path to some file or folder. How can I check in C ++ whether these paths are in the same file / folder? Can I use the Windows API to do this?

+4
source share
5 answers

You can use the Boost Filesystem .

He has the extra weight of cross-platform; this is obviously also a potential advantage. Check out the biased API link below if you want to check it out: GetFileInformationByHandle.

equivalent

bool equivalent(const path& p1, const path& p2); bool equivalent(const path& p1, const path& p2, system::error_code& ec); 

Effects : Defines the file_files s1 and s2, as if by status (p1) and status (p2), respectively.

Returns : true if sf1 == sf2 and p1 and p2 are allowed for the same file system object, otherwise false.

Two paths are considered allowed for the same file system object if two candidate objects are on the same device in the same place. This is defined as if the values ​​of the POSIX stat structure were obtained as by stat () for two paths having equal st_dev values ​​and equal st_ino values.

[ Note : POSIX requires "st_dev to be unique on the local network." Conservative POSIX implementations can also check for equal st_size and st_mtime values. Windows implementations can use GetFileInformationByHandle () as a surrogate for stat () and assume that the "same" are equal for dwVolumeSerialNumber, nFileIndexHigh, nFileIndexLow, nFileSizeHigh, nFileSizeLow, ftLastWriteTime.dwLowDateTimeTimeTimeTime.imewimeTimeTimeTime.imewtimeTimeTimeTime.imewtimeTimeTimeTime.imewtowTimeTimeTime.imewtowTimeTimeTime.dwLowDateTimeTimeTimeTime.imewimeTimeTime.ime

Throws : filesystem_error if (! Exists (s1) & &! Exists (s2)) || (is_other (s1) & is_other (s2)), otherwise, as indicated in the error report.

+6
source

You may be looking for GetFinalPathNameByHandle(hFile, outPath, outSize, FILE_NAME_NORMALIZED | VOLUME_NAME_GUID)

+1
source

Convert the path strings to PIDL using IShellFolder::ParseDisplayName() , SHParseDisplayName() or ILCreateFromPath() , then compare the PIDL with each other using IShellFolder::CompareIDs() or ILIsEqual() .

+1
source

You can use substr for the String library. First, you must correctly place #include to call substr. Have a look at this link: http://www.cplusplus.com/reference/string/string/substr/

Good luck.

-3
source

All Articles