if(NOT EXISTS "${Data2}") message(SEND_ERROR "Input file:\n ${Data2}\ndoes not exist!") endif() foreach(n 1 2 3) string(REGEX REPLACE "_1_\\.my\\.dat$" "_${n}_.my.dat" SeriesCFile "${SeriesC}") if(NOT EXISTS "${SeriesCFile}") message(SEND_ERROR "Input file:\n ${SeriesCFile}\ndoes not exist!") endif() endforeach()