Skip to content

Commit

Permalink
Add CMakeLists.txt and main.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
BC46 committed Aug 22, 2024
1 parent 8f47b21 commit 361cc05
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.28)
project(dircpy)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

add_executable(dircpy main.cpp)
target_link_libraries(dircpy libcmt.lib)
73 changes: 73 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

void RemoveReadOnly(LPCWSTR filePath)
{
DWORD fileAttributes = GetFileAttributesW(filePath);

if ((fileAttributes & FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY)
{
fileAttributes ^= FILE_ATTRIBUTE_READONLY;
SetFileAttributesW(filePath, fileAttributes);
}
}

bool IsWine()
{
HMODULE ntdllHandle = LoadLibraryA("ntdll.dll");
return GetProcAddress(ntdllHandle, "wine_get_version") != nullptr;
}

int wmain(int argc, wchar_t *argv[])
{
if (argc < 5) {
puts("Usage: dircpy.exe source-path dest-path move skip-fl-exe");
return 0;
}

LPCWSTR sourcePath = argv[1];
LPCWSTR destPath = argv[2];
bool move = _wtoi(argv[3]);
bool skipFlExe = _wtoi(argv[4]);
bool isWine = IsWine();

try {
using recursive_directory_iterator = fs::recursive_directory_iterator;
for (const auto& dirEntry : recursive_directory_iterator(sourcePath))
{
if (!dirEntry.is_directory()) {
auto relativeFile = fs::relative(fs::path(dirEntry), sourcePath);

if (skipFlExe && _wcsicmp(relativeFile.filename().c_str(), L"Freelancer.exe") == 0)
continue;

auto destFile = destPath / relativeFile;
auto destFolder = destFile.parent_path();

if (!fs::is_directory(destFolder)) {
fs::create_directories(destFolder);
}

fs::copy(dirEntry, destFile, fs::copy_options::overwrite_existing);

if (move)
fs::remove(dirEntry);

if (!isWine)
RemoveReadOnly(destFile.c_str());
}
}

if (move)
fs::remove_all(sourcePath);

} catch(...) {
return 1;
}

return 0;
}

0 comments on commit 361cc05

Please sign in to comment.