2 * DEL.C - del internal command.
7 * 06/29/98 (Rob Lake rlake@cs.mun.ca)
8 * rewrote del to support wildcards
9 * added my name to the contributors
12 * fixed bug that caused del not to delete file with out
13 * attribute. moved set, del, ren, and ver to there own files
15 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
16 * added config.h include
18 * 09-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeiung.de>)
19 * Fixed command line parsing bugs.
21 * 21-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeiung.de>)
22 * Started major rewrite using a new structure.
24 * 03-Feb-1999 (Eric Kohl <ekohl@abo.rhein-zeiung.de>)
25 * First working version.
27 * 30-Mar-1999 (Eric Kohl <ekohl@abo.rhein-zeiung.de>)
28 * Added quiet ("/Q"), wipe ("/W") and zap ("/Z") option.
30 * 06-Nov-1999 (Eric Kohl <ekohl@abo.rhein-zeiung.de>)
31 * Little fix to keep DEL quiet inside batch files.
36 #ifdef INCLUDE_CMD_DEL
50 DEL_ATTRIBUTES = 0x001, /* /A : not implemented */
51 DEL_ERROR = 0x002, /* /E : not implemented */
52 DEL_NOTHING = 0x004, /* /N */
53 DEL_PROMPT = 0x008, /* /P : not implemented */
54 DEL_QUIET = 0x010, /* /Q */
55 DEL_SUBDIR = 0x020, /* /S : not implemented */
56 DEL_TOTAL = 0x040, /* /T */
57 DEL_WIPE = 0x080, /* /W */
58 DEL_EMPTYDIR = 0x100, /* /X : not implemented */
59 DEL_YES = 0x200, /* /Y : not implemented */
60 DEL_ZAP = 0x400 /* /Z */
66 RemoveFile (LPTSTR lpFileName, DWORD dwFlags)
68 if (dwFlags & DEL_WIPE)
71 /* FIXME: Wipe the given file */
75 return DeleteFile (lpFileName);
79 INT CommandDelete (LPTSTR cmd, LPTSTR param)
81 TCHAR szFullPath[MAX_PATH];
86 INT nEvalArgs = 0; /* nunber of evaluated arguments */
93 if (!_tcsncmp (param, _T("/?"), 2))
95 ConOutPuts (_T("Deletes one or more files.\n"
97 "DEL [/N /P /T /Q /W /Z] file ...\n"
98 "DELETE [/N /P /T /Q /W /Z] file ...\n"
99 "ERASE [/N /P /T /Q /W /Z] file ...\n"
101 " file Specifies the file(s) to delete.\n"
104 " /P Prompts for confirmation before deleting each file.\n"
105 " (Not implemented yet!)\n"
106 " /T Display total number of deleted files and freed disk space.\n"
108 " /W Wipe. Overwrite the file with zeros before deleting it.\n"
109 " /Z Zap (delete hidden, read-only and system files).\n"));
114 arg = split (param, &args, FALSE);
118 /* check for options anywhere in command line */
119 for (i = 0; i < args; i++)
121 if (*arg[i] == _T('/'))
123 if (_tcslen (arg[i]) >= 2)
125 switch (_totupper (arg[i][1]))
128 dwFlags |= DEL_NOTHING;
132 dwFlags |= DEL_PROMPT;
136 dwFlags |= DEL_QUIET;
140 dwFlags |= DEL_SUBDIR;
144 dwFlags |= DEL_TOTAL;
162 /* there are only options on the command line --> error!!! */
163 if (args == nEvalArgs)
165 error_req_param_missing ();
170 /* keep quiet within batch files */
172 dwFlags |= DEL_QUIET;
174 /* check for filenames anywhere in command line */
175 for (i = 0; i < args; i++)
177 if (!_tcscmp (arg[i], _T("*")) ||
178 !_tcscmp (arg[i], _T("*.*")))
182 res = FilePromptYN (_T("All files in directory will be deleted!\n"
183 "Are you sure (Y/N)?"));
184 if ((res == PROMPT_NO) ||
185 (res == PROMPT_BREAK))
189 if (*arg[i] != _T('/'))
192 ConErrPrintf (_T("File: %s\n"), arg[i]);
195 if (_tcschr (arg[i], _T('*')) || _tcschr (arg[i], _T('?')))
197 /* wildcards in filespec */
199 ConErrPrintf (_T("Wildcards!\n\n"));
202 GetFullPathName (arg[i],
208 ConErrPrintf (_T("Full path: %s\n"), szFullPath);
209 ConErrPrintf (_T("File part: %s\n"), pFilePart);
212 hFile = FindFirstFile (szFullPath, &f);
213 if (hFile == INVALID_HANDLE_VALUE)
215 error_file_not_found ();
222 /* ignore "." and ".." */
223 if (!_tcscmp (f.cFileName, _T(".")) ||
224 !_tcscmp (f.cFileName, _T("..")))
227 _tcscpy (pFilePart, f.cFileName);
230 ConErrPrintf (_T("Full filename: %s\n"), szFullPath);
233 if (!(dwFlags & DEL_QUIET) && !(dwFlags & DEL_TOTAL))
234 ConErrPrintf (_T("Deleting: %s\n"), szFullPath);
236 /* delete the file */
237 if (!(dwFlags & DEL_NOTHING))
239 if (RemoveFile (szFullPath, dwFlags))
245 if (dwFlags & DEL_ZAP)
247 if (SetFileAttributes (szFullPath, 0))
249 if (RemoveFile (szFullPath, dwFlags))
255 ErrorMessage (GetLastError(), _T(""));
260 ErrorMessage (GetLastError(), _T(""));
265 ErrorMessage (GetLastError(), _T(""));
270 while (FindNextFile (hFile, &f));
275 /* no wildcards in filespec */
277 ConErrPrintf (_T("No Wildcards!\n"));
279 GetFullPathName (arg[i],
284 ConErrPrintf (_T("Full path: %s\n"), szFullPath);
286 if (!(dwFlags & DEL_QUIET) && !(dwFlags & DEL_TOTAL))
287 ConOutPrintf (_T("Deleting %s\n"), szFullPath);
289 if (!(dwFlags & DEL_NOTHING))
291 if (RemoveFile (szFullPath, dwFlags))
297 if (dwFlags & DEL_ZAP)
299 if (SetFileAttributes (szFullPath, 0))
301 if (RemoveFile (szFullPath, dwFlags))
307 ErrorMessage (GetLastError(), _T(""));
312 ErrorMessage (GetLastError(), _T(""));
317 ErrorMessage (GetLastError(), _T(""));
327 /* only command given */
328 error_req_param_missing ();
335 if (!(dwFlags & DEL_QUIET))
338 ConOutPrintf (_T(" 0 files deleted\n"));
340 ConOutPrintf (_T(" %lu file%s deleted\n"),
342 (dwFiles == 1) ? _T("") : _T("s"));