From c68aaea3e1e7e8ac54cdb490f52f349f1245d536 Mon Sep 17 00:00:00 2001 From: Katayama Hirofumi MZ Date: Thu, 7 Dec 2023 08:26:42 +0900 Subject: [PATCH] first commit --- .gitignore | 37 +++++++++++++ CMakeLists.txt | 18 +++++++ DelayedMove.cpp | 141 ++++++++++++++++++++++++++++++++++++++++++++++++ LICENSE.txt | 21 ++++++++ 4 files changed, 217 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 DelayedMove.cpp create mode 100644 LICENSE.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..368e089 --- /dev/null +++ b/.gitignore @@ -0,0 +1,37 @@ +*.autosave +*.cmake +*.dir +*.exe +*.filters +*.idb +*.map +*.ncb +*.o +*.obj +*.opensdf +*.res +*.sdf +*.sln +*.suo +*.tds +*.user +*.vcproj +*.vcxproj +*~ +.vs +CMakeCache.txt +CMakeFiles +Debug +Makefile +Release +TMP1.$$$ +build +ipch +x64 +*-old +*.skrold +a.* +*.ninja +.ninja* +lib*.a +*.dll diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..275e44b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,18 @@ +# CMake settings for DelayedMove + +# CMake minimum version +cmake_minimum_required(VERSION 3.0) + +# project name and languages +project(DelayedMove CXX) + +############################################################################## + +# Unicode support +add_definitions(-DUNICODE -D_UNICODE) + +# DelayedMove.exe +add_executable(DelayedMove DelayedMove.cpp) +target_link_libraries(DelayedMove PRIVATE shlwapi) + +############################################################################## diff --git a/DelayedMove.cpp b/DelayedMove.cpp new file mode 100644 index 0000000..30b152b --- /dev/null +++ b/DelayedMove.cpp @@ -0,0 +1,141 @@ +// DelayedMove by katahiromz +// Copyright (C) 2023 Katayama Hirofumi MZ. +// License: MIT +#include +#include +#include +#include +#include +#include +#include +#include + +typedef std::vector files_t; + +void show_version(void) +{ + printf("DelayedMove by katahiromz 0.0\n"); +} + +void usage(void) +{ + printf( + "DelayedMove --- Move files by using MoveFileEx API\n" + "\n" + "Usage: DelayedMove [options] \"src_1\" \"dest_1\" \"src_2\" \"dest_2\" ...\n" + "\n" + "Options:\n" + " --help Display this message.\n" + " --version Display version info.\n"); +} + +int just_do_it(const files_t& src, const files_t& dest) +{ + if (src.size() != dest.size()) + return 1; // Logical error + + WCHAR szSrc[MAX_PATH], szDest[MAX_PATH]; + + for (size_t iFile = 0; iFile < dest.size(); ++iFile) + { + auto& s = src[iFile]; + auto& d = dest[iFile]; + GetFullPathNameW(s.c_str(), _countof(szSrc), szSrc, NULL); + if (!PathFileExistsW(szSrc)) + { + wprintf(L"DelayedMove: File not found '%ls'.\n", szSrc); + return 1; + } + } + + for (size_t iFile = 0; iFile < dest.size(); ++iFile) + { + auto& s = src[iFile]; + auto& d = dest[iFile]; + GetFullPathNameW(s.c_str(), _countof(szSrc), szSrc, NULL); + + LPWSTR pszDest; + if (d.empty() || d == L"null" || d == L"NULL") + { + pszDest = NULL; + } + else + { + GetFullPathNameW(d.c_str(), _countof(szDest), szDest, NULL); + if (PathIsDirectoryW(szDest) && !PathIsDirectoryW(szSrc)) + PathAppendW(szDest, PathFindFileNameW(szSrc)); + + pszDest = szDest; + } + + // Delayed file move + if (!MoveFileExW(szSrc, pszDest, MOVEFILE_DELAY_UNTIL_REBOOT | MOVEFILE_REPLACE_EXISTING)) + { + wprintf(L"DelayedMove: FAILED: '%ls' --> '%ls'.\n", szSrc, pszDest); + return 1; + } + else + { + wprintf(L"DelayedMove: SUCCESS: '%ls' --> '%ls'.\n", szSrc, pszDest); + continue; + } + } + + return 0; +} + +int wmain(int argc, LPWSTR *argv) +{ + setlocale(LC_ALL, ""); + + if (argc <= 1) + { + usage(); + return 0; + } + + files_t src, dest; + + for (INT iarg = 1; iarg < argc; ++iarg) + { + std::wstring arg = argv[iarg]; + if (arg == L"--help" || arg == L"/?") + { + usage(); + return 0; + } + if (arg == L"--version") + { + show_version(); + return 0; + } + if (arg.empty() || arg[0] == '-') + { + fwprintf(stderr, L"DelayedMove: invalid argument '%s'\n", arg.c_str()); + return 1; + } + if (iarg + 1 < argc) + { + src.push_back(argv[iarg]); + ++iarg; + dest.push_back(argv[iarg]); + } + else + { + fwprintf(stderr, L"DelayedMove: invalid number of arguments\n"); + return 1; + } + } + + return just_do_it(src, dest); +} + +// For MinGW/clang compilers +int main(void) +{ + INT argc; + LPWSTR *argv = CommandLineToArgvW(GetCommandLineW(), &argc); + INT ret = wmain(argc, argv); + LocalFree(argv); + return ret; +} diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..55115aa --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (C) 2023 Katayama Hirofumi MZ . + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE.