Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
katahiromz committed Dec 6, 2023
0 parents commit c68aaea
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)

##############################################################################
141 changes: 141 additions & 0 deletions DelayedMove.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// DelayedMove by katahiromz
// Copyright (C) 2023 Katayama Hirofumi MZ.
// License: MIT
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <locale.h>
#include <shlwapi.h>
#include <string>
#include <vector>
#include <strsafe.h>

typedef std::vector<std::wstring> 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;
}
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (C) 2023 Katayama Hirofumi MZ <[email protected]>.

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.

0 comments on commit c68aaea

Please sign in to comment.