Jump to content

[CMD] Compare two directories


Recommended Posts

I'm trying to follow this guide. For the very last step, we are told to delete any files that are not in the original directory from the new directory...

So... if I have a directory structure as follows:

D:\
D:\New
D:\New\file1.txt
D:\New\file2.txt

D:\Old
D:\Old\file1.txt

I want to delete the file D:\New\file2.txt. That much I can do with the following...

FOR /R D:\New %%a IN (*.*) DO IF NOT EXIST D:\Old\%%~nxa DEL %%a

the problem I run into is when I have sub directories... imagine we add

D:\New\Folder\file3.txt
D:\New\Folder\file4.txt

D:\Old\Folder\file3.txt

I'm not sure how I would go about using the modifiers to get the child directories and filenames.

Any help would be appreciated!

Link to comment
Share on other sites


Thanks Yzowl, but I don't want to copy over the files. The new directory will contain files that have the same filenames as the old (as well as some extras), but the files themselves are different.

Link to comment
Share on other sites

Okay, I understand now…

This is the best I can come up with atm

@echo off&setlocal enableextensions
set olddir=%userprofile%\desktop\old folder
set newdir=%userprofile%\desktop\new folder
pushd %newdir%
for /f "tokens=*" %%c in ('cd') do set basepath=%%c
for /f "tokens=*" %%d in ('dir/b/s/a') do (
set fullpath=%%d
call:chkit "%%fullpath:%basepath%=%%"
)
popd&endlocal&goto :eof
:chkit
pushd %olddir%
if not exist ".%~1" del /f/a/q "%newdir%%~1"
popd&goto :eof

Just change the locations of olddir and newdir to suit.

Edited by Yzöwl
Link to comment
Share on other sites

Thanks for the help. However, when I try to run it, I get a lot of "The File could not be found" errors.

To be honest, I'm not entirely sure what the script does...

@echo off&setlocal enableextensions
set olddir=%userprofile%\desktop\old folder
set newdir=%userprofile%\desktop\new folder
pushd %newdir%

That much I can figure out... set the variables and then move to the newdir.

for /f "tokens=*" %%c in ('cd') do set basepath=%%c

This sets the variable to the current directory.

for /f "tokens=*" %%d in ('dir/b/s/a') do (
 set fullpath=%%d
 call:chkit "%%fullpath:%basepath%=%%"
)

When I run this on its own (with echo instead of call:chkit), I get the same file listed many many times (probably the number of times that there are files in the current directory). Is it supposed to take the fullpath of each file and remove the basepath?

popd&endlocal&goto :eof
:chkit
pushd %olddir%
if not exist ".%~1" del /f/a/q "%newdir%%~1"
popd&goto :eof

So... return to the previous directory before the pushd. Move to olddir... and that's where I get lost. I'm not entirely familiar with all the modifiers for variable names and such.

Thanks again. :)

Link to comment
Share on other sites

@echo off
setlocal enableextensions enabledelayedexpansion
set olddir=%userprofile%\desktop\old folder
set newdir=%userprofile%\desktop\new folder
pushd %newdir%
for /f "tokens=*" %%c in ('cd') do set basepath=%%c
for /f "tokens=*" %%d in ('dir/b/s/a') do (
set fullpath=%%d
echo "!fullpath:%basepath%=!"
)
endlocal

I've altered it a little, namely

setlocal enableextensions enabledelayedexpansion
and
echo "!fullpath:%basepath%=!"
I've pulled my hair out on a similar issue. It has to do with the way cmd processes for loops. It seems variables don't get updated properly without delayed expansion enabled and the way to get variabled to update is to use ! instead of %. Also note that for that fancy replace line, you can't have !s around basepath.

Note, this probably isn't a complete solution, but might give you a little help.

Edited by JoeMSFN
Link to comment
Share on other sites

Delayed Expansion is not necessary, just changing it slightly to this should do it

@echo off&setlocal enableextensions
set olddir=%userprofile%\desktop\old folder
set newdir=%userprofile%\desktop\new folder
pushd %newdir%
for %%a in (.) do set basepath=%%~fa
for /f "tokens=*" %%b in ('dir/b/s/a ^2^>nul') do (
if %errorlevel% equ 0 (
set fullpath=%%b
call:chkit "%%fullpath:%basepath%=%%"
)
)
popd&endlocal&goto :eof
:chkit
pushd %olddir%
if not exist ".%~1" del /f/a/q "%newdir%%~1" 2>nul
popd&goto :eof

If your new directory is liable to have sub-directories in it which are also not in the old directory and need removing too, then this may be better

@echo off&setlocal enableextensions
set olddir=%userprofile%\desktop\old folder
set newdir=%userprofile%\desktop\new folder
pushd %newdir%
for %%a in (.) do set basepath=%%~fa
for /f "tokens=*" %%b in ('dir/b/s/a ^2^>nul') do (
if %errorlevel% equ 0 (
set fullpath=%%b
call:chkit "%%fullpath:%basepath%=%%"
)
)
popd&endlocal&goto :eof
:chkit
set relpath=%~1
if not exist "%olddir%%relpath%" (
pushd %newdir%%relpath% 2>nul&&(
popd
rd /s/q "%newdir%%relpath%" 2>nul
)||(
del /f/a/q "%newdir%%relpath%" 2>nul
)
)
goto :eof

<Edit>

Codes changed according to the below two responses!

</Edit>

<Edit 2>

Added a couple of 2>nul to hide some standard error echoing

</Edit 2>

Edited by Yzöwl
Link to comment
Share on other sites

Ok.. this is strange. I took the code that JoeMSFN gave and ran it with my entries for the newdir and olddir and set it to output to a text file. (Office_clean.cmd > output.txt)

The contents of the output.txt file are essentially a list of the contents of my C: drive... even though neither of the newdir or olddir variables are on C.... :wacko:

When I manually run the script line by line, the following line

for /f "tokens=*" %%a in ('cd') do set basepath=%%a

tells me that basepath is set to C:\. When I run cd it says G:\OFFICE\OFFICE2 (which is what it should be)...

I'm so confused...

Link to comment
Share on other sites

Try replacing that line with

for %%a in (.) do set basepath=%%~fa

does it help?

<Edit>

I don't know if it'll make any difference to you but I will make that change in both codes in my previous response too!

</Edit>

Edited by Yzöwl
Link to comment
Share on other sites

Ok... with the edited line you gave me in Post #9, the basepath shows as it should.

However, I tried the second code you gave me in #7, but I get errors that "The system cannot find the file specified". I haven't tried the first code yet...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...