2

This page has tells how to remove files in Directory B that also exist in Directory A.

I want to use the windows solution but without the copying.

Can someone tell me how to edit the FOR line to just do the compare and then delete? Thank you.

3
  • 1
    No. That is a completely different question.
    – DavidPostill
    Feb 14, 2018 at 21:44
  • I would of thought something close to FOR %%A IN ("%SourceDir%*.*") DO DEL /Q /F ""%DestDir%\"%%~A" - This doesnt check if it exists in DEST - it just tries and deletes it - if it fails - then it doesn't exist
    – Ross
    Feb 15, 2018 at 4:52
  • Revised comment: Thank you, Ross. Unfortunately it didn't work. I need to do some more testing. "The system cannot find the file specified." This was using test directories with the same five files in both directories. @ECHO ON SET SourceDir=C:\Users\Steve\Desktop\Barnes 330 Discovery Recd 2018-20-15\KV Emails\Source\ SET DestDir=C:\Users\Steve\Desktop\Barnes 330 Discovery Recd 2018-20-15\KV Emails\SourceDupes\ FOR %%A IN ("%SourceDir%*.*") DO DEL /Q /F ""%DestDir%\"%%~A" pause GOTO EOF
    – Steev43230
    Feb 15, 2018 at 13:05

1 Answer 1

0

cd /d "c:\dir_B" && for %i in (*)do if exist "C:\Dir_A\%~nxi" echo\Del "%~i" /q /f

`

1. Go to Dir_B folder: pushd "D:\Dir_B"

2. Use one simple for loop to list yours files: for %i in (*)do...

3. Test if exist your file in Dir_A folder: if exist "D:\Dir_A\%~nxi"

4. If last command return true, delete this file in current Dir_B: del "%~fi"

  • In command-line:
@(pushd "D:\Dir_B" && (for %i in (*)do if exist "D:\Dir_A\%~nxi" del "%~fi")) & popd
  • In bat File:
@(pushd "D:\Dir_B" && (for %%i in (*)do if exist "D:\Dir_A\%%~nxi" del "%%~fi")) & popd
  • One option using dir and operator &&
@(pushd "D:\Dir_B" && (for %i in (*)do 2>nul 2>&1 dir /b "D:\Dir_A\%~nxi" && del "%~fi")) & popd

@echo off

set "_SourceDir=C:\Users\Steve\Desktop\Barnes 330 Discovery Recd 2018-20-15\KV Emails\Source" 
set "_DestDir=C:\Users\Steve\Desktop\Barnes 330 Discovery Recd 2018-20-15\KV Emails\SourceDupes"
cd /d "%_SourceDir%" && for %%a in (*.*)do if exist "%_DestDir%\"%%~nxa" 2>nul del /q /f "%%~dpnxa"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .