From 5a1b02b8e59428d44b2c6cf8d1a21367c7003f6a Mon Sep 17 00:00:00 2001 From: Salvatore Ansani Date: Thu, 29 Dec 2022 18:31:11 +0100 Subject: [PATCH 1/2] Check if a downloaded file already exists --- shareaza/FragmentedFile.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/shareaza/FragmentedFile.cpp b/shareaza/FragmentedFile.cpp index 929afcb33e..3a45c7c582 100644 --- a/shareaza/FragmentedFile.cpp +++ b/shareaza/FragmentedFile.cpp @@ -664,6 +664,20 @@ DWORD CFragmentedFile::Move(DWORD nIndex, LPCTSTR pszDestination, LPPROGRESS_ROU strTarget.Format( _T("%s\\%s_%u"), pszDestination, sName, index ); } + // Check if the target file exists + // Try to get the file attributes + WIN32_FILE_ATTRIBUTE_DATA fileAttr; + if (GetFileAttributesEx(strTarget, GetFileExInfoStandard, &fileAttr)) + { + // The file exists + CString fileExtension = strTarget.Right(strTarget.GetLength() - strTarget.ReverseFind(_T('.'))); + CString fileName = strTarget.Left(strTarget.ReverseFind(_T('.'))); + CString currentTimestamp = CTime::GetCurrentTime().Format(_T("%Y%m%d%H%M%S")); + strTarget = fileName + _T(" (") + currentTimestamp + _T(" (") + _T(".") + fileExtension; + theApp.Message(MSG_DEBUG, _T("File \"%s\" already exists in the destination folder, renaming it to %s"), + (LPCTSTR)sPath, (LPCTSTR)strTarget); + } + // Move/copy file using very long filenames bSuccess = MoveFileWithProgress( CString( _T("\\\\?\\") ) + sPath, CString( _T("\\\\?\\") ) + strTarget, lpProgressRoutine, pTask, From d8a68e58554898d99d1be31435b87a7f3a05b143 Mon Sep 17 00:00:00 2001 From: Salvatore Ansani Date: Thu, 29 Dec 2022 18:32:54 +0100 Subject: [PATCH 2/2] Fixes output message --- shareaza/FragmentedFile.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/shareaza/FragmentedFile.cpp b/shareaza/FragmentedFile.cpp index 3a45c7c582..f8512798bb 100644 --- a/shareaza/FragmentedFile.cpp +++ b/shareaza/FragmentedFile.cpp @@ -673,9 +673,10 @@ DWORD CFragmentedFile::Move(DWORD nIndex, LPCTSTR pszDestination, LPPROGRESS_ROU CString fileExtension = strTarget.Right(strTarget.GetLength() - strTarget.ReverseFind(_T('.'))); CString fileName = strTarget.Left(strTarget.ReverseFind(_T('.'))); CString currentTimestamp = CTime::GetCurrentTime().Format(_T("%Y%m%d%H%M%S")); - strTarget = fileName + _T(" (") + currentTimestamp + _T(" (") + _T(".") + fileExtension; + CString newFileName = fileName + _T(" (") + currentTimestamp + _T(")") + fileExtension; theApp.Message(MSG_DEBUG, _T("File \"%s\" already exists in the destination folder, renaming it to %s"), - (LPCTSTR)sPath, (LPCTSTR)strTarget); + (LPCTSTR)strTarget, (LPCTSTR)newFileName); + strTarget = newFileName; } // Move/copy file using very long filenames