2 * REN.C - rename internal command.
7 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
8 * added config.h include
10 * 18-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>
11 * Added support for quoted long file names with spaces.
13 * 20-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>
14 * Unicode and redirection safe!
16 * 17-Oct-2001 (Eric Kohl <ekohl@rz.online.de>
17 * Implemented basic rename code.
22 #ifdef INCLUDE_CMD_RENAME
34 REN_ATTRIBUTES = 0x001, /* /A : not implemented */
35 REN_ERROR = 0x002, /* /E */
36 REN_NOTHING = 0x004, /* /N */
37 REN_PROMPT = 0x008, /* /P : not implemented */
38 REN_QUIET = 0x010, /* /Q */
39 REN_SUBDIR = 0x020, /* /S */
40 REN_TOTAL = 0x040, /* /T */
45 * file rename internal command.
48 INT cmd_rename (LPTSTR cmd, LPTSTR param)
52 INT nEvalArgs = 0; /* nunber of evaluated arguments */
54 DWORD dwFiles = 0; /* number of renamedd files */
56 LPTSTR srcPattern = NULL;
57 LPTSTR dstPattern = NULL;
58 TCHAR dstFile[MAX_PATH];
59 BOOL bDstWildcard = FALSE;
66 if (!_tcsncmp(param, _T("/?"), 2))
68 ConOutPuts(_T("Renames a file/directory or files/directories.\n"
70 "RENAME [/E /N /P /Q /S /T] old_name ... new_name\n"
71 "REN [/E /N /P /Q /S /T] old_name ... new_name\n"
73 " /E No eror messages.\n"
75 " /P Prompts for confirmation before renaming each file.\n"
76 " (Not implemented yet!)\n"
78 " /S Rename subdirectories.\n"
79 " /T Display total number of renamed files.\n"
81 "Note that you cannot specify a new drive or path for your destination. Use\n"
82 "the MOVE command for that purpose."));
86 /* split the argument list */
87 arg = split(param, &args, FALSE);
91 if (!(dwFlags & REN_ERROR))
92 error_req_param_missing();
98 for (i = 0; i < args; i++)
100 if (*arg[i] == _T('/'))
102 if (_tcslen(arg[i]) >= 2)
104 switch (_totupper(arg[i][1]))
107 dwFlags |= REN_ERROR;
111 dwFlags |= REN_NOTHING;
115 dwFlags |= REN_PROMPT;
119 dwFlags |= REN_QUIET;
123 dwFlags |= REN_SUBDIR;
127 dwFlags |= REN_TOTAL;
135 /* keep quiet within batch files */
137 dwFlags |= REN_QUIET;
139 /* there are only options on the command line --> error!!! */
140 if (args < nEvalArgs + 2)
142 if (!(dwFlags & REN_ERROR))
143 error_req_param_missing();
148 /* get destination pattern */
149 for (i = 0; i < args; i++)
151 if (*arg[i] == _T('/'))
156 if (_tcschr(dstPattern, _T('*')) || _tcschr(dstPattern, _T('?')))
159 /* enumerate source patterns */
160 for (i = 0; i < args; i++)
162 if (*arg[i] == _T('/') || arg[i] == dstPattern)
168 ConErrPrintf(_T("\n\nSourcePattern: %s\n"), srcPattern);
169 ConErrPrintf(_T("DestinationPattern: %s\n"), dstPattern);
172 hFile = FindFirstFile(srcPattern, &f);
173 if (hFile == INVALID_HANDLE_VALUE)
175 if (!(dwFlags & REN_ERROR))
176 error_file_not_found();
182 /* ignore "." and ".." */
183 if (!_tcscmp (f.cFileName, _T(".")) ||
184 !_tcscmp (f.cFileName, _T("..")))
187 /* do not rename hidden or system files */
188 if (f.dwFileAttributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM))
191 /* do not rename directories when the destination pattern contains
192 * wildcards, unless option /S is used */
193 if ((f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
195 && !(dwFlags & REN_SUBDIR))
199 ConErrPrintf(_T("Found source name: %s\n"), f.cFileName);
202 /* build destination file name */
211 while (*p != 0 && *p != *q)
240 ConErrPrintf(_T("DestinationFile: %s\n"), dstFile);
243 if (!(dwFlags & REN_QUIET) && !(dwFlags & REN_TOTAL))
244 ConOutPrintf(_T("%s -> %s\n"), f.cFileName, dstFile);
246 /* rename the file */
247 if (!(dwFlags & REN_NOTHING))
249 if (MoveFile(f.cFileName, dstFile))
255 if (!(dwFlags & REN_ERROR))
256 ConErrPrintf(_T("MoveFile() failed. Error: %lu\n"), GetLastError());
260 while (FindNextFile(hFile, &f));
264 if (!(dwFlags & REN_QUIET))
267 ConOutPrintf(_T(" %lu file renamed\n"),
270 ConOutPrintf(_T(" %lu files renamed\n"),