Skip to content

Commit

Permalink
Added extensions to ignore
Browse files Browse the repository at this point in the history
Added temporary extensions for the program to ignore that are used by browsers like ".crdownload" to prevent the program from moving the file while it is being downloaded and corrupt or ruin the downloading process.
  • Loading branch information
RamezzE committed Oct 31, 2023
1 parent 78b892e commit 0db4585
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,24 @@
#include <windows.h>
#include <shlobj.h>

// YOU HAVE TO COMPILE THIS PROGRAM WITH C++17 STANDARD: -std=c++17
// YOU HAVE TO INCLUDE THESE LIBRARIES WHEN COMPILING: -lole32 -lshell32 -luuid

namespace fs = std::filesystem;

// Ignoring temporary files and files that are being downloaded
// This is necessary because the program will try to move the file while it is being downloaded
// This will cause an error and the download will fail
// The program will try to move the file again when the download is complete with its normal extension
std::set<std::string> extensionsToIgnore = {
".crdownload",
".part",
".download",
".partial",
".ut",
".tmp",
".filepart",
".incomplete",
".!download",
};

// The use of wide strings is necessary for the program to work with non-ASCII characters

fs::path targetDirectory;
Expand All @@ -21,12 +34,18 @@ std::set<fs::path> currentFiles;
std::set<std::wstring> myExtensions;
std::unordered_map<std::wstring, std::wstring> myMap;

bool shouldIgnoreFile(const fs::path &filePath)
{
std::string extension = filePath.extension().string();
return extensionsToIgnore.count(extension) > 0;
}

bool detectChange(const fs::path &path)
{
currentFiles.clear();

for (const auto &entry : fs::directory_iterator(path))
if (entry.is_regular_file())
if (entry.path().has_extension() && entry.is_regular_file() && !shouldIgnoreFile(entry.path()))
currentFiles.insert(entry.path());

if (previousFiles.empty())
Expand Down Expand Up @@ -61,18 +80,11 @@ void collectingExtensions()
{
try
{
const fs::path p{targetDirectory};
for (auto const &dir_entry : fs::directory_iterator{p})
for (auto const &dir_entry : currentFiles)
{
if (!dir_entry.path().has_extension())
continue;

if (!dir_entry.is_regular_file())
continue;

try
{
myExtensions.insert(dir_entry.path().extension().wstring()); // Use wide strings for extensions
myExtensions.insert(dir_entry.extension().wstring()); // Use wide strings for extensions
}
catch (const std::exception &e)
{
Expand Down Expand Up @@ -136,19 +148,12 @@ void moveFiles()
{
try
{
const fs::path p{targetDirectory};
for (auto const &currentFile : fs::directory_iterator{p})
for (auto const &currentFile : currentFiles)
{
if (!currentFile.path().has_extension())
continue;

if (!currentFile.is_regular_file())
continue;

try
{
std::wstring extension = currentFile.path().extension().wstring(); // Use wide strings for extensions
std::wstring filename = currentFile.path().filename().wstring(); // Use wide strings for filenames
std::wstring extension = currentFile.extension().wstring(); // Use wide strings for extensions
std::wstring filename = currentFile.filename().wstring(); // Use wide strings for filenames
std::wstring folderName = myMap.at(extension);
std::wstring movePath = targetDirectory / fs::path(folderName) / fs::path(filename); // Use wide strings for paths
std::wstring oldPath = targetDirectory / fs::path(filename);
Expand Down

0 comments on commit 0db4585

Please sign in to comment.