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)
11 * Added support for quoted long file names with spaces.
13 * 20-Jan-1999 (Eric Kohl)
14 * Unicode and redirection safe!
16 * 17-Oct-2001 (Eric Kohl)
17 * Implemented basic rename code.
19 * 30-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
20 * Remove all hardcode string to En.rc
25 #ifdef INCLUDE_CMD_RENAME
29 REN_ATTRIBUTES
= 0x001, /* /A : not implemented */
30 REN_ERROR
= 0x002, /* /E */
31 REN_NOTHING
= 0x004, /* /N */
32 REN_PROMPT
= 0x008, /* /P : not implemented */
33 REN_QUIET
= 0x010, /* /Q */
34 REN_SUBDIR
= 0x020, /* /S */
35 REN_TOTAL
= 0x040, /* /T */
40 * file rename internal command.
43 INT
cmd_rename (LPTSTR cmd
, LPTSTR param
)
45 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
48 INT nEvalArgs
= 0; /* nunber of evaluated arguments */
50 DWORD dwFiles
= 0; /* number of renamedd files */
52 LPTSTR srcPattern
= NULL
;
53 LPTSTR dstPattern
= NULL
;
54 TCHAR dstFile
[MAX_PATH
];
55 BOOL bDstWildcard
= FALSE
;
62 if (!_tcsncmp(param
, _T("/?"), 2))
64 ConOutResPaging(TRUE
,STRING_REN_HELP1
);
70 /* split the argument list */
71 arg
= split(param
, &args
, FALSE
);
75 if (!(dwFlags
& REN_ERROR
))
76 error_req_param_missing();
82 for (i
= 0; i
< args
; i
++)
84 if (*arg
[i
] == _T('/'))
86 if (_tcslen(arg
[i
]) >= 2)
88 switch (_totupper(arg
[i
][1]))
95 dwFlags
|= REN_NOTHING
;
99 dwFlags
|= REN_PROMPT
;
103 dwFlags
|= REN_QUIET
;
107 dwFlags
|= REN_SUBDIR
;
111 dwFlags
|= REN_TOTAL
;
119 /* keep quiet within batch files */
121 dwFlags
|= REN_QUIET
;
123 /* there are only options on the command line --> error!!! */
124 if (args
< nEvalArgs
+ 2)
126 if (!(dwFlags
& REN_ERROR
))
127 error_req_param_missing();
132 /* get destination pattern */
133 for (i
= 0; i
< args
; i
++)
135 if (*arg
[i
] == _T('/'))
140 if (_tcschr(dstPattern
, _T('*')) || _tcschr(dstPattern
, _T('?')))
143 /* enumerate source patterns */
144 for (i
= 0; i
< args
; i
++)
146 if (*arg
[i
] == _T('/') || arg
[i
] == dstPattern
)
151 TRACE("\n\nSourcePattern: %s\n", debugstr_aw(srcPattern
));
152 TRACE("DestinationPattern: %s\n", debugstr_aw(dstPattern
));
154 hFile
= FindFirstFile(srcPattern
, &f
);
155 if (hFile
== INVALID_HANDLE_VALUE
)
157 if (!(dwFlags
& REN_ERROR
))
158 error_file_not_found();
164 /* ignore "." and ".." */
165 if (!_tcscmp (f
.cFileName
, _T(".")) ||
166 !_tcscmp (f
.cFileName
, _T("..")))
169 /* do not rename hidden or system files */
170 if (f
.dwFileAttributes
& (FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
))
173 /* do not rename directories when the destination pattern contains
174 * wildcards, unless option /S is used */
175 if ((f
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
177 && !(dwFlags
& REN_SUBDIR
))
180 TRACE("Found source name: %s\n", debugstr_aw(f
.cFileName
));
182 /* build destination file name */
191 while (*p
!= 0 && *p
!= *q
)
219 TRACE("DestinationFile: %s\n", debugstr_aw(dstFile
));
221 if (!(dwFlags
& REN_QUIET
) && !(dwFlags
& REN_TOTAL
))
222 ConOutPrintf(_T("%s -> %s\n"), f
.cFileName
, dstFile
);
224 /* rename the file */
225 if (!(dwFlags
& REN_NOTHING
))
227 if (MoveFile(f
.cFileName
, dstFile
))
233 if (!(dwFlags
& REN_ERROR
))
235 LoadString(CMD_ModuleHandle
, STRING_REN_ERROR1
, szMsg
, RC_STRING_MAX_SIZE
);
236 ConErrPrintf(szMsg
, GetLastError());
241 while (FindNextFile(hFile
, &f
));
245 if (!(dwFlags
& REN_QUIET
))
248 LoadString( CMD_ModuleHandle
, STRING_REN_HELP2
, szMsg
, RC_STRING_MAX_SIZE
);
250 LoadString( CMD_ModuleHandle
, STRING_REN_HELP3
, szMsg
, RC_STRING_MAX_SIZE
);
251 ConOutPrintf(szMsg
,dwFiles
);