[PATCH 14/41] robocopy: add move files switch (/MOV)

Florian Eder others.meder at gmail.com
Mon Sep 6 09:54:51 CDT 2021


Implements the /MOV switch, which causes the files to be moved
instead of being copied to the destination

Signed-off-by: Florian Eder <others.meder at gmail.com>
---
 programs/robocopy/main.c      | 28 +++++++++++++++++++++++-----
 programs/robocopy/robocopy.h  |  4 +++-
 programs/robocopy/robocopy.rc |  1 +
 3 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/programs/robocopy/main.c b/programs/robocopy/main.c
index 96fbdade6de..b9356b4d104 100644
--- a/programs/robocopy/main.c
+++ b/programs/robocopy/main.c
@@ -157,6 +157,11 @@ static void parse_arguments(int argc, WCHAR *argv[])
                 if (!options.user_limited_subdirectories_depth)
                     options.max_subdirectories_depth = 0;
             }
+            /* mov - Delete files (but not folders) after copying them */
+            else if (!wcsicmp(argv[i], L"/mov"))
+            {
+                options.purge_source_files = TRUE;
+            }
             /* lev - Limit depth of subdirectories */
             else if (!wcsnicmp(argv[i], L"/lev:", 5))
             {
@@ -252,6 +257,18 @@ static BOOL create_directory_path(WCHAR *path)
     return TRUE;
 }
 
+static BOOL copy_or_move_file(WCHAR *source, WCHAR *destination, BOOL do_move)
+{
+    if (!create_directory_path(destination)) return FALSE;
+    if (do_move ? !MoveFileExW(source, destination, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING)
+                : !CopyFileW(source, destination, FALSE))
+    {
+        output_error(STRING_ERROR_WRITE_FILE, GetLastError(), strip_path_prefix(destination));
+        return FALSE;
+    }
+    else return TRUE;
+}
+
 static void get_file_paths_in_folder(WCHAR *directory_path, struct list *paths, UINT depth)
 {
     HANDLE temp_handle;
@@ -344,12 +361,9 @@ static BOOL perform_copy(void)
         }
         else
         {
-            create_directory_path(target_path);
-            if (!CopyFileW(current_absolute_path, target_path, FALSE))
-                output_error(STRING_ERROR_WRITE_FILE, GetLastError(), strip_path_prefix(target_path));
-            else
+            if (copy_or_move_file(current_absolute_path, target_path, options.purge_source_files))
             {
-                output_message(STRING_CREATE_FILE, strip_path_prefix(target_path));
+                output_message(options.purge_source_files ? STRING_MOVE_FILE : STRING_CREATE_FILE, strip_path_prefix(target_path));
             }
         }
 
@@ -380,6 +394,10 @@ static WCHAR *get_option_string(void)
     if (options.copy_empty_subdirectories)
         wcscat(temp_string, L"/E ");
 
+    /* Move files */
+    if (options.purge_source_files)
+        wcscat(temp_string, L"/MOV ");
+
     string = wcsdup(temp_string);
     return string;
 }
diff --git a/programs/robocopy/robocopy.h b/programs/robocopy/robocopy.h
index bdce05fdcde..b7f5c3db89c 100644
--- a/programs/robocopy/robocopy.h
+++ b/programs/robocopy/robocopy.h
@@ -39,6 +39,7 @@ struct robocopy_options {
     BOOL user_limited_subdirectories_depth;
     BOOL copy_subdirectories;
     BOOL copy_empty_subdirectories;
+    BOOL purge_source_files;
 };
 
 /* Exit codes */
@@ -57,4 +58,5 @@ struct robocopy_options {
 #define STRING_ERROR_WRITE_DIRECTORY          1012
 #define STRING_ERROR_WRITE_FILE               1014
 #define STRING_CREATE_DIRECTORY               1019
-#define STRING_CREATE_FILE                    1022
\ No newline at end of file
+#define STRING_CREATE_FILE                    1022
+#define STRING_MOVE_FILE                      1024
\ No newline at end of file
diff --git a/programs/robocopy/robocopy.rc b/programs/robocopy/robocopy.rc
index 7c025b62b11..b4a6f8ab218 100644
--- a/programs/robocopy/robocopy.rc
+++ b/programs/robocopy/robocopy.rc
@@ -37,6 +37,7 @@ STRINGTABLE
     STRING_ERROR_WRITE_FILE, "[%1] Error %2 (%3) occurred writing file \"%4\":\n%5\n"
     STRING_CREATE_DIRECTORY, " Created Dir: %1\n"
     STRING_CREATE_FILE, " Copied File: %1\n"
+    STRING_MOVE_FILE, "  Moved File: %1\n"
 }
 
 LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
-- 
2.32.0




More information about the wine-devel mailing list