bat скрипт
@Echo off
setlocal enabledelayedexpansion
set /p folder_path1=In
set /p folder_path2=From
set /p output_folder=Output
xcopy /dey "%folder_path1%" "%output_folder%" > nul
xcopy /dey "%folder_path2%" "%output_folder%" > nul
for %%I in ("%output_folder%\*") do (
set "file1_size="
set "file2_size="
for %%F in ("%folder_path1%\%%~nxI") do set "file1_size=%%~zF"
for %%F in ("%folder_path2%\%%~nxI") do set "file2_size=%%~zF"
if defined file1_size (
if defined file2_size (
if !file1_size! lss !file2_size! (
del /q "%output_folder%\%%~nxI"
copy "%folder_path1%\%%~nxI" "%output_folder%\"
) else (
del /q "%output_folder%\%%~nxI"
copy "%folder_path2%\%%~nxI" "%output_folder%\"
)
)
)
)
pause
Python: import os
import shutil
def compare_folders(folder_path1, folder_path2, output_folder):
# Создаем выходную папку, если ее нет
os.makedirs(output_folder, exist_ok=True)
# Копируем все файлы из обеих папок в выходную папку
for folder_path in [folder_path1, folder_path2]:
for file_name in os.listdir(folder_path):
src_file = os.path.join(folder_path, file_name)
dst_file = os.path.join(output_folder, file_name)
shutil.copy(src_file, dst_file)
# Сравниваем файлы и оставляем только файлы с наименьшим размером
for file_name in os.listdir(output_folder):
file1 = os.path.join(folder_path1, file_name)
file2 = os.path.join(folder_path2, file_name)
output_file = os.path.join(output_folder, file_name)
if os.path.exists(file1) and os.path.exists(file2):
size1 = os.path.getsize(file1)
size2 = os.path.getsize(file2)
if size1 < size2:
os.remove(file2)
else:
os.remove(file1)
if __name__ == "__main__":
folder_path1 = input("In: ")
folder_path2 = input("From: ")
output_folder = input("Output: ")
compare_folders(folder_path1, folder_path2, output_folder)