--- src/vfile.c
+++ src/vfile.c
@@ -310,12 +310,12 @@
       }else if( file_isfile(zPath) && !db_exists(zSql, &zPath[nPrefix+1]) ){
         db_multi_exec("INSERT INTO sfile VALUES(%Q)", &zPath[nPrefix+1]);
       }
       blob_resize(pPath, origSize);
     }
+    closedir(d);
   }
-  closedir(d);
 }
 
 /*
 ** Compute an aggregate MD5 checksum over the disk image of every
 ** file in vid.  The file names are part of the checksum.  The resulting

