MSFN Forum: [CMD] Compare two directories - MSFN Forum

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[CMD] Compare two directories Rate Topic: -----

#1 User is offline   Zxian 

  • Scroll up - see the Google bar?
  • Group: Super Moderator
  • Posts: 5,063
  • Joined: 30-September 04
  • OS:none specified
  • Country: Country Flag

Posted 30 September 2005 - 06:11 PM

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!


#2 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,117
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 01 October 2005 - 05:02 AM

You could always use robocopy
robocopy "D:\Old" "D:\New" /mir>nul


#3 User is offline   Zxian 

  • Scroll up - see the Google bar?
  • Group: Super Moderator
  • Posts: 5,063
  • Joined: 30-September 04
  • OS:none specified
  • Country: Country Flag

Posted 01 October 2005 - 03:03 PM

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.

#4 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,117
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 02 October 2005 - 04:22 AM

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.

This post has been edited by Yzöwl: 02 October 2005 - 04:33 AM


#5 User is offline   Zxian 

  • Scroll up - see the Google bar?
  • Group: Super Moderator
  • Posts: 5,063
  • Joined: 30-September 04
  • OS:none specified
  • Country: Country Flag

Posted 03 October 2005 - 09:48 AM

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. :)

#6 User is offline   JoeMSFN 

  • Member
  • PipPip
  • Group: Members
  • Posts: 206
  • Joined: 28-September 04

Posted 03 October 2005 - 10:27 AM

 
@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

Quote

setlocal enableextensions enabledelayedexpansion
and

Quote

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.

This post has been edited by JoeMSFN: 03 October 2005 - 10:28 AM


#7 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,117
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 03 October 2005 - 11:15 AM

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>

This post has been edited by Yzöwl: 03 October 2005 - 04:36 PM


#8 User is offline   Zxian 

  • Scroll up - see the Google bar?
  • Group: Super Moderator
  • Posts: 5,063
  • Joined: 30-September 04
  • OS:none specified
  • Country: Country Flag

Posted 03 October 2005 - 11:21 AM

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...

#9 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,117
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 03 October 2005 - 12:01 PM

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>

This post has been edited by Yzöwl: 03 October 2005 - 01:03 PM


#10 User is offline   Zxian 

  • Scroll up - see the Google bar?
  • Group: Super Moderator
  • Posts: 5,063
  • Joined: 30-September 04
  • OS:none specified
  • Country: Country Flag

Posted 03 October 2005 - 03:12 PM

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...

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy