*
*
* History:
- *
+ *
*
* 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
* added config.h include
* 30-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
* Remove all hardcode string to En.rc
* 25-Nov-2008 (Victor Martinez) <vicmarcal@hotmail.com> Patch dedicated to Myrjala because her comprenhension and love :D
- * Fixing following Bugs:
+ * Fixing following Bugs:
* -Wrong behavior with wildcards when Source and Destiny are Paths(FIXED).
* -Wrong general behavior (MSDN:"Rename cant move files between subdirectories")(FIXED)
* -Wrong behavior when renaming without path in destiny:(i.e) "ren C:\text\as.txt list.txt" it moves as.txt and then rename it(FIXED)
* (MSDN: If there is a Path in Source and no Path in Destiny, then Destiny Path is Source Path,because never Ren has to be used to move.)
* -Implemented checkings if SourcePath and DestinyPath are differents.
- *
+ *
*/
#include <precomp.h>
DWORD dwFiles = 0; /* number of renamedd files */
INT i;
-
+
LPTSTR srcPattern = NULL; /* Source Argument*/
TCHAR srcPath[MAX_PATH]; /*Source Path Directories*/
LPTSTR srcFILE = NULL; /*Contains the files name(s)*/
- TCHAR srcFinal[MAX_PATH];
-
-
+ TCHAR srcFinal[MAX_PATH];
+
+
LPTSTR dstPattern = NULL; /*Destiny Argument*/
TCHAR dstPath[MAX_PATH]; /*Source Path Directories*/
LPTSTR dstFILE = NULL; /*Contains the files name(s)*/
BOOL bDstWildcard = FALSE;
BOOL bPath = FALSE;
-
-
-
-
-
-
+
+
+
+
+
+
LPTSTR p,q,r;
HANDLE hFile;
/*If the PARAM=/? then show the help*/
if (!_tcsncmp(param, _T("/?"), 2))
{
-
-
+
+
ConOutResPaging(TRUE,STRING_REN_HELP1);
return 0;
}
nErrorLevel = 0;
/* Split the argument list.Args will be saved in arg vector*/
- arg = split(param, &args, FALSE);
-
+ arg = split(param, &args, FALSE, FALSE);
+
if (args < 2)
{
if (!(dwFlags & REN_ERROR))
case _T('N'):
dwFlags |= REN_NOTHING;
break;
-
+
case _T('P'):
dwFlags |= REN_PROMPT;
break;
freep(arg);
return 1;
}
-
-
+
+
/* Get destination pattern and source pattern*/
for (i = 0; i < args; i++)
{
continue;
dstPattern = arg[i]; //we save the Last argument as dstPattern
srcPattern = arg[i-1];
-
+
}
-
-
-
-
+
+
+
+
if (_tcschr(srcPattern, _T('\\'))) //Checking if the Source (srcPattern) is a Path to the file
- {
-
+ {
+
bPath= TRUE;
//Splitting srcPath and srcFile.
if(!_tcschr(srcFILE, _T('\\'))) break;
}
_tcsncpy(srcPath,srcPattern,_tcslen(srcPattern)-_tcslen(srcFILE));
-
-
-
+
+
+
if(_tcschr(dstPattern, _T('\\'))) //Checking if the Destiny (dstPattern)is also a Path.And splitting dstPattern in dstPath and srcPath.
- {
+ {
dstFILE = _tcschr(dstPattern, _T('\\'));
nSlash=0;
while(_tcschr(dstFILE, _T('\\')))
if(!_tcschr(dstFILE, _T('\\'))) break;
}
_tcsncpy(dstPath,dstPattern,_tcslen(dstPattern)-_tcslen(dstFILE));
-
+
if((_tcslen(dstPath)!=_tcslen(srcPath))||(_tcsncmp(srcPath,dstPath,_tcslen(srcPath))!=0)) //If it has a Path,then MUST be equal than srcPath
{
error_syntax(dstPath);
freep(arg);
return 1;
}
- }else { //If Destiny hasnt a Path,then (MSDN says) srcPath is its Path.
-
+ }else { //If Destiny hasnt a Path,then (MSDN says) srcPath is its Path.
+
_tcscpy(dstPath,srcPath);
-
+
dstFILE=dstPattern;
-
+
}
-
-
+
+
}
-
+
if (!_tcschr(srcPattern, _T('\\'))) //If srcPattern isnt a Path but a name:
- {
+ {
srcFILE=srcPattern;
if(_tcschr(dstPattern, _T('\\')))
- {
+ {
error_syntax(dstPattern);
-
+
freep(arg);
return 1;
}else dstFILE=dstPattern;
}
-
+
//Checking Wildcards.
if (_tcschr(dstFILE, _T('*')) || _tcschr(dstFILE, _T('?')))
bDstWildcard = TRUE;
-
-
-
+
+
+
TRACE("\n\nSourcePattern: %s SourcePath: %s SourceFile: %s", debugstr_aw(srcPattern),debugstr_aw(srcPath),debugstr_aw(srcFILE));
TRACE("\n\nDestinationPattern: %s Destination Path:%s Destination File: %s\n", debugstr_aw(dstPattern),debugstr_aw(dstPath),debugstr_aw(dstFILE));
-
+
hFile = FindFirstFile(srcPattern, &f);
-
+
if (hFile == INVALID_HANDLE_VALUE)
{
if (!(dwFlags & REN_ERROR))
error_file_not_found();
-
+
}
do
{
continue;
TRACE("Found source name: %s\n", debugstr_aw(f.cFileName));
- /* So here we have splitted the dstFILE and we have find a f.cFileName(thanks to srcPattern)
+ /* So here we have splitted the dstFILE and we have find a f.cFileName(thanks to srcPattern)
* Now we have to use the mask (dstFILE) (which can have Wildcards) with f.cFileName to find destination file name(dstLast) */
p = f.cFileName;
q = dstFILE;
*r = 0;
//Well we have splitted the Paths,so now we have to paste them again(if needed),thanks bPath.
if( bPath == TRUE)
- {
-
+ {
+
_tcscpy(srcFinal,srcPath);
-
+
_tcscat(srcFinal,f.cFileName);
-
+
_tcscpy(dstFinal,dstPath);
_tcscat(dstFinal,dstLast);
-
-
+
+
}else{
_tcscpy(srcFinal,f.cFileName);
_tcscpy(dstFinal,dstLast);
}
-
+
TRACE("DestinationPath: %s\n", debugstr_aw(dstFinal));
-
-
+
+
if (!(dwFlags & REN_QUIET) && !(dwFlags & REN_TOTAL))
-
+
ConOutPrintf(_T("%s -> %s\n"),srcFinal , dstFinal);
/* Rename the file */
if (!(dwFlags & REN_NOTHING))
- {
-
-
-
+ {
+
+
+
if (MoveFile(srcFinal, dstFinal))
{
dwFiles++;
}
}
}
-
+
while (FindNextFile(hFile, &f));
//Closing and Printing errors.
-
+
FindClose(hFile);
-
+
if (!(dwFlags & REN_QUIET))
{
else
ConOutResPrintf(STRING_REN_HELP3, dwFiles);
}
-
+
freep(arg);
-
+
return 0;
}