How to check if string is a valid directory (if it is a drive letter)

StackOverflow https://stackoverflow.com/questions/23513651

  •  16-07-2023
  •  | 
  •  

문제

I've stumbled upon an oddity when dealing with cmake.

When I try to check whether a string is a valid directory or not I am using IS_DIRECTORY however if the string I am testing a drive I get a false.

Repro code:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8.3)
IF( IS_DIRECTORY "C:/") # "C:/Windows" works
    MESSAGE(STATUS "Is a dir")
ELSE()
    MESSAGE(STATUS "Is not a dir")
ENDIF()

Which always yield false for C:/ and true for C:/Windows

Is there a way to check whether C:/ is a valid 'directory' without having the pain to create a file on this location and check whether this succeeds and then delete it?

The context in which the dir check is performed is to obtain a read only location and creating a file might entirely fail or require elevated rights (which are not desired in this situation)

도움이 되었습니까?

해결책

Thanks to @Fraser I tracked this behaviour being present in CMake version 2.8.11.2 but not in 2.8.12.2. However AFAIK there is no changelog entry in http://www.cmake.org/files/v2.8/CMakeChangeLog-2.8.12.1 mentioning any fixes to IS_DIRECTORY

I recommend using the min version to ensure consistent behaviour then

 CMAKE_MINIMUM_REQUIRED(VERSION 2.8.12.2)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top