I have a folder with a bunch of files and subfolders in it. I need to delete all files in this folder that have filenames matching files in any of the subfolders.
@echo off
for %%F in (*) do (
for /R . %%D in (*) do (
if %%~nxF==%%~nxD (
if not %%~dpF==%%~dpD (
if exist %%~dpnfF (
echo deleting %~nxF
del /f/q %%~dpnxF
)
)
)
)
)
echo done
for every file name in this folder
for every file name in this and all subfolders
if the simple names match
if the paths don't match (ie. it isn't a file in this folder)
if the file still exists
tell the user we're deleting it
and delete it
tell the user we're done
It requires command extensions. A much more elegant solution is possible, but this is all you're gonna get from me right now.
import os
from os import walk
from os import join
from os.path import isfile
rootDir = "" #assign root directory
for j in [g for g in [f for _, _,files in walk(rootDir) for f in files] if [f for _, _,files in walk(rootDir) for f in files].count(g) > 1 and isfile(join(rootDir,g))]: os.remove(join(rootDir, j))
edit: made it worse. Donn't know if this will work either.
I can't draw any conclusions other than what the OP posted.
Personally, I would be loathe to delete a file that has been touched more recently than its 'duplicate'. It might be worth putting a condition in for that.
But that may be a moot point. I assume that it isn't an issue or OP would have said something about it.