I have two folders. Folder B contains 300 files. The complete set.
Folder A contains a subset of folder B. About 120 files.
I want to delete the files in folder B that exist in folder A
Essentially, folder A is a list of files that are safe to be deleted from Folder B.
If it's any easier, I can simply write the file names to a text file for folder A then delete any files in Folder B if the filename exists in the text file.
Which ever is easier.
How would I do this?
Delete files in folder B which exist in folder A
Moderator: DosItHelp
Re: Delete files in folder B which exist in folder A
Based on your previous questions I see no reason why you shouldn't be able to figure this out yourself. This could literally be written in one line of code. A simple for command to iterate the files in one directory and use that output with the delete command.
Re: Delete files in folder B which exist in folder A
Thanks.
Couldn't figure it out so I've had to code 150 lines in my batch file instead.
Couldn't figure it out so I've had to code 150 lines in my batch file instead.
Re: Delete files in folder B which exist in folder A
This might work
obviously Dont forget to change the folder nameS
Code: Select all
for /f %%a in ('dir C:\FolderA\* /b /s') do (
for /f %%A in ('dir C:\FolderB\* /b /s') do (
if exist "%%a" del "%%A"
)
)
Re: Delete files in folder B which exist in folder A
Code: Select all
for %%G in ("C:\FolderA\*") do del "C:\FolderB\%%~nxG" 2>nul
Re: Delete files in folder B which exist in folder A
%%a will always exist if the working directory is folder A because the first for command created it. You also have to remember that the folder path is not included in the FOR variable when capturing the dir output.AR Coding wrote: ↑28 Aug 2021 20:49This might workobviously Dont forget to change the folder nameSCode: Select all
for /f %%a in ('dir C:\FolderA\* /b /s') do ( for /f %%A in ('dir C:\FolderB\* /b /s') do ( if exist "%%a" del "%%A" ) )