diff --git a/SuperBuild/CMake/post_install.cmake b/SuperBuild/CMake/post_install.cmake
index e211e653fdbdc1cd8547569ff119dd4cc35d76b0..75572f4f7436d4c2054828d059e58db674058e39 100644
--- a/SuperBuild/CMake/post_install.cmake
+++ b/SuperBuild/CMake/post_install.cmake
@@ -3,9 +3,12 @@ string(REPLACE "|" ";" P_DIRS ${P_DIRS})
 foreach( p_dir ${P_DIRS} )
 set(file_list)
 file( GLOB file_list "${p_dir}/*.cmake" )
+list(SORT file_list)
 foreach( cmake_file ${file_list} )
-  file(STRINGS "${cmake_file}" MATCH_FOUND REGEX "${MATCH}")
+
+  file(STRINGS "${cmake_file}" MATCH_FOUND REGEX "${P_MATCH}")
   if(MATCH_FOUND)
+    message("Replacing '${P_MATCH}' with '${P_REPLACE}' in ${cmake_file}")
     file(STRINGS "${cmake_file}" cmake_file_CONTENTS NEWLINE_CONSUME)
     string(REPLACE "${P_MATCH}" "$${}{${P_REPLACE}}" cmake_file_CONTENTS ${cmake_file_CONTENTS})
     file(WRITE "${cmake_file}"  "# This file is modified by OTB after installation.