[CMD] Fix the del /s command, reported by Pablo De Napoli. CORE-10460
authorHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Sun, 3 Dec 2017 15:01:44 +0000 (16:01 +0100)
committerHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Sun, 3 Dec 2017 15:01:44 +0000 (16:01 +0100)
The command should delete files in the specified directory and all of its
sub-directories, using any file pattern specified.

For example, the command:

    del /S .\my_directory

should delete all the files inside my_directory and its sub-directories,
and as such should also detect that "my_directory" is indeed a directory,
while doing:

    del /S .\my_file

should of course detect that "my_file" is indeed a file pattern, and thus,
delete all "my_file" files from the current directory and its sub-directories.

The command:

    del /S some_directory\file_pattern

should delete "file_pattern" files from some_directory and its sub-directories.

base/shell/cmd/del.c

index 5d3d3ca..4c0dc3a 100644 (file)
@@ -284,39 +284,46 @@ ProcessDirectory(LPTSTR FileName, DWORD* dwFlags, DWORD dwAttrFlags)
     WIN32_FIND_DATA f;
     DWORD dwFiles = 0;
 
+    /* Get the full path to the file */
     GetFullPathName(FileName,
                     MAX_PATH,
                     szFullPath,
                     &pFilePart);
 
+    /* Delete all the files in this directory */
     dwFiles = DeleteFiles(szFullPath, dwFlags, dwAttrFlags);
-    if (dwFiles & 0x80000000) return dwFiles;
+    if (dwFiles & 0x80000000)
+        return dwFiles;
 
     if (*dwFlags & DEL_SUBDIR)
     {
         /* Get just the file name */
-        pSearchPart = _tcsrchr(FileName,_T('\\'));
-        if (pSearchPart != NULL)
-            pSearchPart++;
-        else
-            pSearchPart = FileName;
-
-        /* Get the full path to the file */
-        GetFullPathName (FileName,MAX_PATH,szFullPath,NULL);
+        pSearchPart = _T("*");
+        if (!IsExistingDirectory(szFullPath))
+        {
+            pSearchPart = _tcsrchr(FileName, _T('\\'));
+            if (pSearchPart != NULL)
+                pSearchPart++;
+            else
+                pSearchPart = FileName;
+        }
 
-        /* strip the filename off of it */
-        pFilePart = _tcsrchr(szFullPath, _T('\\'));
-        if (pFilePart == NULL)
+        /* If no wildcard or file was specified and this is a directory, then
+           display all files in it */
+        if (pFilePart == NULL || IsExistingDirectory(szFullPath))
         {
-            pFilePart = szFullPath;
+            pFilePart = &szFullPath[_tcslen(szFullPath)];
+            if (*(pFilePart-1) != _T('\\'))
+                *pFilePart++ = _T('\\');
+            _tcscpy(pFilePart, _T("*"));
         }
         else
         {
-            pFilePart++;
+            /* strip the filename off of it */
+            _tcscpy(pFilePart, _T("*"));
         }
 
-        _tcscpy(pFilePart, _T("*"));
-
+        /* Enumerate all the sub-directories */
         hFile = FindFirstFile(szFullPath, &f);
         if (hFile != INVALID_HANDLE_VALUE)
         {
@@ -341,6 +348,7 @@ ProcessDirectory(LPTSTR FileName, DWORD* dwFlags, DWORD dwAttrFlags)
             FindClose (hFile);
         }
     }
+
     return dwFiles;
 }