diff --git a/SystemTools.cxx b/SystemTools.cxx
index 39873e630d9cbab41d8a689f25a2b6db543ac035..d27081b8c68c006a3f57f7610c7097031829cb09 100644
--- a/SystemTools.cxx
+++ b/SystemTools.cxx
@@ -2186,12 +2186,15 @@ bool SystemTools::CopyFileIfDifferent(const std::string& source,
   // FilesDiffer does not handle file to directory compare
   if (SystemTools::FileIsDirectory(destination)) {
     const std::string new_destination = FileInDir(source, destination);
-    return SystemTools::CopyFileIfDifferent(source, new_destination);
-  }
-  // source and destination are files so do a copy if they
-  // are different
-  if (SystemTools::FilesDiffer(source, destination)) {
-    return SystemTools::CopyFileAlways(source, destination);
+    if (!SystemTools::ComparePath(new_destination, destination)) {
+      return SystemTools::CopyFileIfDifferent(source, new_destination);
+    }
+  } else {
+    // source and destination are files so do a copy if they
+    // are different
+    if (SystemTools::FilesDiffer(source, destination)) {
+      return SystemTools::CopyFileAlways(source, destination);
+    }
   }
   // at this point the files must be the same so return true
   return true;
diff --git a/testSystemTools.cxx b/testSystemTools.cxx
index 1f3a15b59129a127ab56a4e31a33c6eade8bc3fb..3f6eeb8c1d98ad202e556b327efd91f4a7ed527c 100644
--- a/testSystemTools.cxx
+++ b/testSystemTools.cxx
@@ -1074,6 +1074,15 @@ static bool CheckCopyFileIfDifferent()
     }
   }
 
+  if (!kwsys::SystemTools::MakeDirectory("dir_a") ||
+      !kwsys::SystemTools::MakeDirectory("dir_b")) {
+    return false;
+  }
+
+  if (!kwsys::SystemTools::CopyFileIfDifferent("dir_a/", "dir_b")) {
+    ret = false;
+  }
+
   return ret;
 }