On 2023-03-12 15:08, Neil wrote:
...
So, then I asked ChatGPT to be sure it accounted for the case of the
same name, but different content;
ChatGPT:
==================================
No, the script I provided only checks for new files based on their
filename, so it doesn't take into account the contents of the files.
If you want to check for new files based on both their filename and
contents, you can modify the script to read the contents of each file
and compare them before deciding whether to add the file to folder A or
not. Here's an example of how you can do this in Python:
import os
import filecmp
import shutil
folderA = 'path/to/folderA'
folderB = 'path/to/folderB'
# Get a list of file names in folder A
filesA = os.listdir(folderA)
# Iterate over each file in folder B
for fileB in os.listdir(folderB):
# Check if the file already exists in folder A
if fileB in filesA:
# If the file already exists, compare the contents
if not filecmp.cmp(os.path.join(folderA, fileB),
os.path.join(folderB, fileB), shallow=False):
# If the contents are different, copy the file to folder A
with a unique name
shutil.copy2(os.path.join(folderB, fileB),
os.path.join(folderA, fileB + '_new'))
else:
# If the file doesn't exist in folder A, copy it over
shutil.copy2(os.path.join(folderB, fileB),
os.path.join(folderA, fileB))
==================================
Which may be better. Please give it a go!