Problem I want to solve: Compare files in one directory to files in a subdirectory. If they match, delete the file in the top directory. This is for Windows and I would like to use Windows Batch commands. If the file doesn't exist, the script will exit.

For example: c:\temp contains baz_foo.exe c:\temp\bar contains foo.exe

Attempt to copy foo.exe to c:\temp\bar, but robocopy notices they're the same, so it will exit and give a return code of 0. https://blogs.technet.microsoft.com/deploymentguys/2008/06/16/robocopy-exit-codes/

pseudo code:

@echo off
setlocal enabledelayedexpansion
SET ARCHIVEDIR=c:\temp\bar

FOR %%F in ('*baz_foo*.exe') DO SET (

if errorlevel 0 goto nocopy
if errorlevel 1 goto copy

echo Files are identical
goto exit

echo files different
goto exit


Call nextfile.bat

Clarification: Note, I don't need the files copied to bar, I just want to know if they're the same or different. If it's the same, delete from c:\temp and move onto the next file; at the end of finding all files, call a different script.

  • 1
    Well, @w32sh, the /MOV[E] option does delete from source; or, you could swap source and target and use /PURGE... – aschipfl Jun 12 at 9:03
  • 1
    robocopy supports an /L that lists files that would be copied without, perhaps you find this useful... – aschipfl Jun 12 at 9:04
  • I suggest to correct your sample because robocopy would not notice c:\temp\baz_foo.exe to be equal to c:\temp\bar\foo.exe. – LotPings Jun 12 at 12:22
  • Just to clarify: if a file exists in the top-folder but not in the sub-folder, do you want to leave it in the top-folder? i.e. you only want to delete the file in the top-folder if it exists in the sub-folder? OR do you only want to delete it from the top-folder if the sub-folder contains a newer version? or will they always be the same version? Why do both of your options GOTO exit? It will always exit regardless of what it's doing if it's laid out like that. I'll give an example answer and edit based on your response. – mael' Jun 12 at 12:58
  • If the file exists in both locations, I would like it to be deleted from both locations. If it doesn't exist in the sub folder, keep the file in the top level folder. – Registered User Jun 12 at 13:40

I believe the FC command will work better here if you're just trying to compare files; coupled with an if exist in your loop. Will likely look something like this:

@echo off

set "top=c:\temp"
set "sub=%top%\bar"

for /r "%sub%" %%A in (*) do (
    if exist "%top%\%%~nxA" fc "%%A" "%top%\%%~nxA" > nul
    if not errorlevel 1 (
        del /f /q "%top%\%%~nxA"
        del /f /q "%%A"

Call nextfile.bat

This looks at all files in c:\temp\bar and assigns each to parameter %%A; for each loop iteration it sees if the filename (including the extension; %%~nxA) of parameter %%A exists in c:\temp - and if it does, it compares the files with FC and outputs that to nul so it doesn't display the results in your window. After that, it determines the errorlevel - I used if not errorlevel 1 here so it would only apply to files that are the same, since sometimes errorlevel 0 will give you a false positive in this situation without some tweaking. If it passes this test, it will delete those matching files from both c:\temp and c:\temp\bar. It's true that there are other possible errorlevels beyond 0 and 1 for FC (can be -1 or 2), but the for-loop already establishes that it can't be 2 since the files have to exist in both directories for it to do anything, and -1 is also mostly ruled out for the same reasons. Hopefully this accomplishes what you are looking for - just leave a comment if I am misunderstanding what your requirements are.

  • Thanks for your assistance. FC is a better idea, I wasn't aware of it. – Registered User Jun 14 at 2:01
  • @RegisteredUser happy to help! – mael' Jun 14 at 2:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.