2 * MOVE.C - move internal command.
7 * 14-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
10 * 18-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
12 * Preliminary version!!!
14 * 20-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
17 * 27-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
18 * Added help text ("/?").
19 * Added more error checks.
21 * 03-Feb-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
24 * 30-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
25 * Remove all hardcode string to En.rc
27 * 24-Jun-2005 (Brandon Turner) <turnerb7@msu.edu>)
28 * Fixed bug to allow MS style wildcards + code clean up
35 #ifdef INCLUDE_CMD_MOVE
40 MOVE_NOTHING
= 0x001, /* /N */
41 MOVE_OVER_YES
= 0x002, /* /Y */
42 MOVE_OVER_NO
= 0x004, /* /-Y */
45 static INT
Overwrite (LPTSTR fn
)
47 /*ask the user if they want to override*/
48 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
50 LoadString(CMD_ModuleHandle
, STRING_MOVE_HELP1
, szMsg
, RC_STRING_MAX_SIZE
);
51 ConOutPrintf(szMsg
,fn
);
52 res
= FilePromptYNA ("");
58 INT
cmd_move (LPTSTR cmd
, LPTSTR param
)
60 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
63 TCHAR szDestPath
[MAX_PATH
];
64 TCHAR szSrcPath
[MAX_PATH
];
67 WIN32_FIND_DATA findBuffer
;
72 if (!_tcsncmp (param
, _T("/?"), 2))
75 ConOutPuts (_T("Moves files and renames files and directories.\n\n"
76 "To move one or more files:\n"
77 "MOVE [/N][/Y|/-Y][drive:][path]filename1[,...] destination\n"
79 "To rename a directory:\n"
80 "MOVE [/N][/Y|/-Y][drive:][path]dirname1 dirname2\n"
82 " [drive:][path]filename1 Specifies the location and name of the file\n"
83 " or files you want to move.\n"
84 " /N Nothing. Don everthing but move files or direcories.\n"
89 ConOutResPaging(TRUE
,STRING_MOVE_HELP2
);
94 arg
= split (param
, &argc
, FALSE
);
98 for (i
= 0; i
< argc
; i
++)
100 if (*arg
[i
] == _T('/'))
102 if (_tcslen(arg
[i
]) >= 2)
104 switch (_totupper(arg
[i
][1]))
107 dwFlags
|= MOVE_NOTHING
;
111 dwFlags
|= MOVE_OVER_YES
;
115 dwFlags
|= MOVE_OVER_NO
;
125 /* there must be at least two pathspecs */
126 error_req_param_missing ();
130 if (_tcschr (arg
[argc
- 1], _T('*')) != NULL
)
132 /*'*' in dest, this doesnt happen. give folder name instead*/
133 error_parameter_format('2');
137 /* get destination */
138 GetFullPathName (arg
[argc
- 1], MAX_PATH
, szDestPath
, NULL
);
140 DebugPrintf (_T("Destination: %s\n"), szDestPath
);
144 for (i
= 0; i
< argc
- 1; i
++)
146 if (*arg
[i
] == _T('/'))
149 hFile
= FindFirstFile (arg
[i
], &findBuffer
);
150 if (hFile
== INVALID_HANDLE_VALUE
)
152 ErrorMessage (GetLastError (), arg
[i
]);
161 GetFullPathName (findBuffer
.cFileName
, MAX_PATH
, szSrcPath
, &pszFile
);
163 if (GetFileAttributes (szSrcPath
) & FILE_ATTRIBUTE_DIRECTORY
)
165 /* source is directory */
168 DebugPrintf (_T("Move directory \'%s\' to \'%s\'\n"),
169 szSrcPath
, szDestPath
);
171 if (!(dwFlags
& MOVE_NOTHING
))
173 MoveFileEx (szSrcPath
, szDestPath
, MOVEFILE_REPLACE_EXISTING
| MOVEFILE_WRITE_THROUGH
| MOVEFILE_COPY_ALLOWED
);
178 if (GetFileAttributes (szDestPath
) & FILE_ATTRIBUTE_DIRECTORY
)
180 /* destination is existing directory */
182 /*build the dest string(accounts for *)*/
183 TCHAR szFullDestPath
[MAX_PATH
];
184 _tcscpy (szFullDestPath
, szDestPath
);
185 /*check to see if there is an ending slash, if not add one*/
186 if(szFullDestPath
[_tcslen(szFullDestPath
) - 1] != _T('\\'))
187 _tcscat (szFullDestPath
, _T("\\"));
189 _tcscat (szFullDestPath
, findBuffer
.cFileName
);
191 /*checks to make sure user wanted/wants the override*/
192 if((dwFlags
& MOVE_OVER_NO
) && IsExistingFile (szFullDestPath
))
194 if(!(dwFlags
& MOVE_OVER_YES
) && IsExistingFile (szFullDestPath
))
195 nOverwrite
= Overwrite (szFullDestPath
);
196 if (nOverwrite
== PROMPT_NO
|| nOverwrite
== PROMPT_BREAK
)
198 if (nOverwrite
== PROMPT_ALL
)
199 dwFlags
|= MOVE_OVER_YES
;
201 /*delete the file that might be there first*/
202 DeleteFile(szFullDestPath
);
203 ConOutPrintf (_T("%s => %s"), szSrcPath
, szFullDestPath
);
205 if ((dwFlags
& MOVE_NOTHING
))
208 /*delete the file that might be there first*/
209 DeleteFile(szFullDestPath
);
211 if (MoveFileEx (szSrcPath
, szFullDestPath
, MOVEFILE_REPLACE_EXISTING
| MOVEFILE_WRITE_THROUGH
| MOVEFILE_COPY_ALLOWED
))
212 LoadString(CMD_ModuleHandle
, STRING_MOVE_ERROR1
, szMsg
, RC_STRING_MAX_SIZE
);
214 LoadString(CMD_ModuleHandle
, STRING_MOVE_ERROR2
, szMsg
, RC_STRING_MAX_SIZE
);
220 /* destination is a file */
222 if (_tcschr (arg
[argc
- 2], _T('*')) != NULL
)
224 /*'*' in src but there should't be one in the dest*/
225 error_parameter_format('1');
229 /*bunch of checks to see if we the user wanted/wants
230 to really override the files*/
231 if((dwFlags
& MOVE_OVER_NO
) && IsExistingFile (szDestPath
))
233 if(!(dwFlags
& MOVE_OVER_YES
) && IsExistingFile (szDestPath
))
234 nOverwrite
= Overwrite (szDestPath
);
235 if (nOverwrite
== PROMPT_NO
|| nOverwrite
== PROMPT_BREAK
)
237 if (nOverwrite
== PROMPT_ALL
)
238 dwFlags
|= MOVE_OVER_YES
;
240 ConOutPrintf (_T("%s => %s"), szSrcPath
, szDestPath
);
242 if ((dwFlags
& MOVE_NOTHING
))
245 /*delete the file first just to get ride of it
246 if it was already there*/
247 DeleteFile(szDestPath
);
249 if (MoveFileEx (szSrcPath
, szDestPath
, MOVEFILE_REPLACE_EXISTING
| MOVEFILE_WRITE_THROUGH
| MOVEFILE_COPY_ALLOWED
))
250 LoadString(CMD_ModuleHandle
, STRING_MOVE_ERROR1
, szMsg
, RC_STRING_MAX_SIZE
);
252 LoadString(CMD_ModuleHandle
, STRING_MOVE_ERROR2
, szMsg
, RC_STRING_MAX_SIZE
);
259 while (FindNextFile (hFile
, &findBuffer
));
267 #endif /* INCLUDE_CMD_MOVE */