Page 1 of 1
Delete files in folder B which exist in folder A
Posted: 28 Aug 2021 05:02
by Yanta
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?
Re: Delete files in folder B which exist in folder A
Posted: 28 Aug 2021 12:22
by Squashman
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
Posted: 28 Aug 2021 19:54
by Yanta
Thanks.
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
Posted: 28 Aug 2021 20:49
by AR Coding
This might work
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"
)
)
obviously Dont forget to change the folder nameS
Re: Delete files in folder B which exist in folder A
Posted: 28 Aug 2021 22:40
by Squashman
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
Posted: 28 Aug 2021 22:43
by Squashman
AR Coding wrote: ↑28 Aug 2021 20:49
This might work
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"
)
)
obviously Dont forget to change the folder nameS
%%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.