📄 move.c
字号:
/*
* MOVE.C - move internal command.
*
*
* History:
*
* 14-Dec-1998 (Eric Kohl)
* Started.
*
* 18-Jan-1999 (Eric Kohl)
* Unicode safe!
* Preliminary version!!!
*
* 20-Jan-1999 (Eric Kohl)
* Redirection safe!
*
* 27-Jan-1999 (Eric Kohl)
* Added help text ("/?").
* Added more error checks.
*
* 03-Feb-1999 (Eric Kohl)
* Added "/N" option.
*
* 30-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
* Remove all hardcode string to En.rc
*
* 24-Jun-2005 (Brandon Turner) <turnerb7@msu.edu>)
* Fixed bug to allow MS style wildcards + code clean up
* added /y and /-y
*/
#include <precomp.h>
#ifdef INCLUDE_CMD_MOVE
enum
{
MOVE_NOTHING = 0x001, /* /N */
MOVE_OVER_YES = 0x002, /* /Y */
MOVE_OVER_NO = 0x004, /* /-Y */
};
enum
{ /* Move status flags */
MOVE_SOURCE_IS_DIR = 0x001,
MOVE_SOURCE_IS_FILE = 0x002,
MOVE_DEST_IS_DIR = 0x004,
MOVE_DEST_IS_FILE = 0x008,
MOVE_SOURCE_HAS_WILD = 0x010, /* source has wildcard */
MOVE_SRC_CURRENT_IS_DIR = 0x020, /* source is file but at the current round we found a directory */
MOVE_DEST_EXISTS = 0x040,
MOVE_PATHS_ON_DIF_VOL = 0x080 /* source and destination paths are on different volume */
};
static INT MoveOverwrite (LPTSTR fn)
{
/*ask the user if they want to override*/
TCHAR szMsg[RC_STRING_MAX_SIZE];
INT res;
LoadString(CMD_ModuleHandle, STRING_MOVE_HELP1, szMsg, RC_STRING_MAX_SIZE);
ConOutPrintf(szMsg,fn);
res = FilePromptYNA (_T(""));
return res;
}
void GetDirectory (LPTSTR wholepath, LPTSTR directory, BOOL CheckExisting)
{
/* returns only directory part of path with backslash */
/* TODO: make code unc aware */
/* Is there a better alternative to this? */
LPTSTR last;
if (CheckExisting && IsExistingDirectory(wholepath))
{
_tcscpy(directory, wholepath);
}
else if ((last = _tcsrchr(wholepath,_T('\\'))) != NULL)
{
_tcsncpy(directory, wholepath, last - wholepath + 1);
directory[last - wholepath + 1] = 0;
}
else
{
GetRootPath(wholepath,directory, MAX_PATH);
}
}
INT cmd_move (LPTSTR cmd, LPTSTR param)
{
TCHAR szMsg[RC_STRING_MAX_SIZE];
LPTSTR *arg;
INT argc, i, nFiles;
TCHAR szDestPath[MAX_PATH];
TCHAR szFullDestPath[MAX_PATH];
TCHAR szSrcDirPath[MAX_PATH];
TCHAR szSrcPath[MAX_PATH];
TCHAR szFullSrcPath[MAX_PATH];
DWORD dwFlags = 0;
INT nOverwrite = 0;
WIN32_FIND_DATA findBuffer;
HANDLE hFile;
/* used only when source and destination directories are on different volume*/
HANDLE hDestFile;
WIN32_FIND_DATA findDestBuffer;
TCHAR szMoveDest[MAX_PATH];
TCHAR szMoveSrc[MAX_PATH];
LPTSTR pszDestDirPointer;
LPTSTR pszSrcDirPointer;
INT nDirLevel = 0;
LPTSTR pszFile;
BOOL OnlyOneFile;
BOOL FoundFile;
BOOL MoveStatus;
DWORD dwMoveFlags = 0;
DWORD dwMoveStatusFlags = 0;
if (!_tcsncmp (param, _T("/?"), 2))
{
#if 0
ConOutPuts (_T("Moves files and renames files and directories.\n\n"
"To move one or more files:\n"
"MOVE [/N][/Y|/-Y][drive:][path]filename1[,...] destination\n"
"\n"
"To rename a directory:\n"
"MOVE [/N][/Y|/-Y][drive:][path]dirname1 dirname2\n"
"\n"
" [drive:][path]filename1 Specifies the location and name of the file\n"
" or files you want to move.\n"
" /N Nothing. Don everthing but move files or direcories.\n"
" /Y\n"
" /-Y\n"
"..."));
#else
ConOutResPaging(TRUE,STRING_MOVE_HELP2);
#endif
return 0;
}
nErrorLevel = 0;
arg = split (param, &argc, FALSE);
nFiles = argc;
/* read options */
for (i = 0; i < argc; i++)
{
if (*arg[i] == _T('/'))
{
if (_tcslen(arg[i]) >= 2)
{
switch (_totupper(arg[i][1]))
{
case _T('N'):
dwFlags |= MOVE_NOTHING;
break;
case _T('Y'):
dwFlags |= MOVE_OVER_YES;
break;
case _T('-'):
dwFlags |= MOVE_OVER_NO;
break;
}
}
nFiles--;
}
}
if (nFiles < 2)
{
/* there must be at least two pathspecs */
error_req_param_missing ();
return 1;
}
/* check for wildcards in source and destination */
if (_tcschr (arg[argc - 1], _T('*')) != NULL || _tcschr (arg[argc - 1], _T('?')) != NULL)
{
/* '*'/'?' in dest, this doesnt happen. give folder name instead*/
error_invalid_parameter_format(arg[argc - 1]);
return 1;
}
if (_tcschr (arg[argc - 2], _T('*')) != NULL || _tcschr (arg[argc - 2], _T('?')) != NULL)
{
dwMoveStatusFlags |= MOVE_SOURCE_HAS_WILD;
}
/* get destination */
GetFullPathName (arg[argc - 1], MAX_PATH, szDestPath, NULL);
#ifdef _DEBUG
DebugPrintf (_T("Destination: %s\n"), szDestPath);
#endif
/* get source folder */
GetDirectory(arg[argc - 2], szSrcDirPath, 1);
GetFullPathName(szSrcDirPath, MAX_PATH, szSrcPath, &pszFile);
_tcscpy(szSrcDirPath,szSrcPath);
/* we need following check to see if source happens to be directly given directory
and if it is then rip off last directory part so that there won't be any clashes with codes after this point */
GetFullPathName(arg[argc - 2], MAX_PATH, szSrcPath, &pszFile);
if (_tcscmp(szSrcDirPath,szSrcPath) == 0)
szSrcDirPath[pszFile - szSrcPath] = _T('\0');
#ifdef _DEBUG
DebugPrintf (_T("Source Folder: %s\n"), szSrcDirPath);
#endif
hFile = FindFirstFile (arg[argc - 2], &findBuffer);
if (hFile == INVALID_HANDLE_VALUE)
{
ErrorMessage (GetLastError (), arg[argc - 2]);
freep (arg);
return 1;
}
/* check for special cases "." and ".." and if found skip them */
FoundFile = TRUE;
while(FoundFile &&
(_tcscmp(findBuffer.cFileName,_T(".")) == 0 ||
_tcscmp(findBuffer.cFileName,_T("..")) == 0))
FoundFile = FindNextFile (hFile, &findBuffer);
if (!FoundFile)
{
/* what? we don't have anything to move? */
error_file_not_found();
FindClose(hFile);
freep(arg);
return 1;
}
OnlyOneFile = TRUE;
_tcscpy(szSrcPath,szSrcDirPath);
/*check to see if there is an ending slash, if not add one*/
if(szSrcPath[_tcslen(szSrcPath) - 1] != _T('\\'))
_tcscat (szSrcPath, _T("\\"));
_tcscat(szSrcPath,findBuffer.cFileName);
#ifdef _DEBUG
DebugPrintf (_T("Source Path: %s\n"), szSrcPath);
#endif
/* check if there can be found files as files have first priority */
if (IsExistingFile(szSrcPath)) dwMoveStatusFlags |= MOVE_SOURCE_IS_FILE;
else dwMoveStatusFlags |= MOVE_SOURCE_IS_DIR;
while(OnlyOneFile && FindNextFile(hFile,&findBuffer))
{
_tcscpy(szSrcPath,szSrcDirPath);
if(szSrcPath[_tcslen(szSrcPath) - 1] != _T('\\'))
_tcscat (szSrcPath, _T("\\"));
_tcscat(szSrcPath,findBuffer.cFileName);
if (IsExistingFile(szSrcPath))
{
ConOutPrintf(_T(""));
if (dwMoveStatusFlags & MOVE_SOURCE_IS_FILE) OnlyOneFile = FALSE;
else
{ /* this has been done this way so that we don't disturb other settings if they have been set before this */
dwMoveStatusFlags |= MOVE_SOURCE_IS_FILE;
dwMoveStatusFlags &= ~MOVE_SOURCE_IS_DIR;
}
}
}
FindClose(hFile);
#ifdef _DEBUG
DebugPrintf(_T("Do we have only one file: %s\n"), OnlyOneFile ? _T("TRUE") : _T("FALSE"));
#endif
/* we have to start again to be sure we don't miss any files or folders*/
hFile = FindFirstFile (arg[argc - 2], &findBuffer);
if (hFile == INVALID_HANDLE_VALUE)
{
ErrorMessage (GetLastError (), arg[argc - 2]);
freep (arg);
return 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -