Compare two folders and get new and modified file names

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
monty_0309
Posts: 4
Joined: 25 Apr 2012 03:10

Compare two folders and get new and modified file names

#1 Post by monty_0309 » 25 Apr 2012 03:14

Hi,
I want a shell script which compare two folders (with some similar files) and get new files and the files which are modified.
It will be good if the new and the modified files moved to third folder.

I have the following script, which identifies the new script and find the comarision of the files and store in log files. I am unable to get the modified file names.
Please look and help

----------------------------------------------------------------------------------------------------
if exist compare.log del compare.log
if exist missing.log del missing.log
for /f "delims=" %%a in ('dir /b/a-d "C:\TFSworkspace\Scripts"') do (
if exist "C:\tfs\Scripts\%%a" (
fc "C:\TFSworkspace\Scripts\%%a" "C:\tfs\Scripts\%%a" >> compare.log
) else (
echo %%a >> missing.log
)
)
-------------------------------------------------------------------------------------------------------------

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Compare two folders and get new and modified file names

#2 Post by foxidrive » 25 Apr 2012 07:48

What is the purpose?

Robocopy can copy changed files to another folder.

monty_0309
Posts: 4
Joined: 25 Apr 2012 03:10

Re: Compare two folders and get new and modified file names

#3 Post by monty_0309 » 25 Apr 2012 08:02

Actually I have two folders

eg
C:\TFSworkspace --> which have latest files
C:\tfs\ --> which has some old files

So, out of these, some are identical files, some are modified files and some are new.
So I need these modified and new files to be executed in some test env.
That's why I need the list of files

I cannot use and software tool as this batch script will be the part of my ANT script.

please help quickly ..

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Compare two folders and get new and modified file names

#4 Post by foxidrive » 25 Apr 2012 08:47

I wrote this once - launch it like this: Checkfolders.bat "c:\folder one" "d:\folder two"

Code: Select all

@echo off
del diff.tmp 2>nul

call :compare %1 %2
echo.
call :compare %2 %1
echo.

setlocal enabledelayedexpansion
for /f "delims=" %%b in (diff.tmp) do (
for %%c in (%%b) do set c="%%~tc - %%~zc bytes"
for %%d in ("%~2\%%~nxb") do set d="%%~td - %%~zd bytes"
if not !c!==!d! (
echo file %%~nxb exists but is different
echo      !c! "%~1\%%~nxb"
echo      !d! "%~2\%%~nxb"
echo.
)
)
pause
del diff.tmp 2>nul
goto :EOF

:compare
for /f "delims=" %%a in ('dir %1 /a:-d /o:n /b') do (
if not exist "%~2\%%~nxa" (
echo %2 does not have %%~nxa
) else (
echo "%~1\%%~nxa">>diff.tmp
)
)



Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Compare two folders and get new and modified file names

#5 Post by Aacini » 25 Apr 2012 16:08

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Build Original File array with modification dates
cd \tfs
for %%f in (*.*) do (
   set originalFile["%%f"]="%%~Tf"
)

rem Process latest files and build New Or Modified list
cd \TFSworkspace
set newOrMod=
for %%f in (*.*) do (
   if defined originalFile["%%f"] (
      if !originalFile["%%f"]! neq "%%~Tf" (
         rem File is modified
         set newOrMod=!newOrMod! "%%f"
      )
   ) else (
      rem File is new
      set newOrMod=!newOrMod! "%%f"
   )
)

rem Process New Or Modified file list in any way you wish, i.e.
for %%f in (%newOrMod%) do echo %%~f

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Compare two folders and get new and modified file names

#6 Post by foxidrive » 25 Apr 2012 16:14

@Aacini: It will have problems with ! in filenames also, correct?

I must be the odd one out because I have ! in a lot of filenames. :)


The line to handle NewOrMod is also limited to around 160 filenames @50 characters each, with the 8K command length limit in XP. W2K has a 2K line length limit. I'm not sure what Vista or Win7 have.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Compare two folders and get new and modified file names

#7 Post by Aacini » 25 Apr 2012 16:45

The version below solve the problems addressed by foxidrive

Code: Select all

@echo off
setlocal DisableDelayedExpansion

rem Build Original File array with modification dates
cd \tfs
for %%f in (*.*) do (
   set originalFile["%%f"]="%%~Tf"
)

rem Process latest files and build New Or Modified array
cd \TFSworkspace
set i=0
for %%f in (*.*) do (
   if defined originalFile["%%f"] (
      setlocal EnableDelayedExpansion
      if !originalFile["%%f"]! neq "%%~Tf" (
         rem File is modified
         set /A i+=1
         for %%i in (!i!) do endlocal & set "newOrMod[%%i]=%%f"& set i=%%i
      ) else (
         endlocal
      )
   ) else (
      rem File is new
      set /A i+=1
      setlocal EnableDelayedExpansion
      for %%i in (!i!) do endlocal & set "newOrMod[%%i]=%%f"
   )
)

rem Process New Or Modified file array in any way you wish, i.e.
setlocal EnableDelayedExpansion
for /L %%i in (1,1,%i%) do echo !newOrMod[%%i]!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Compare two folders and get new and modified file names

#8 Post by foxidrive » 25 Apr 2012 17:01

@Aacini - That's impressive.

If I was were to comment any further, I would comment on variable names.

When I learned elementary programming it was pointed out that i, l, 1, I, o, O, 0 all make poor choices of variable names because it is hard to distinguish them in various fonts in many cases.

Of course Microsoft themselves chose %%i as the example in FOR /? as the epitome of poor programming choices.

monty_0309
Posts: 4
Joined: 25 Apr 2012 03:10

Re: Compare two folders and get new and modified file names

#9 Post by monty_0309 » 25 Apr 2012 23:40

Thanks for the help !!

But, if I understand the scripts correctly, they are finding the files based on the modifiaction time.

As all the files are get from TFS on the local system, the modification date and time will be same.

I want something like fc command which displays the name of the files those are modified.

monty_0309
Posts: 4
Joined: 25 Apr 2012 03:10

Re: Compare two folders and get new and modified file names

#10 Post by monty_0309 » 26 Apr 2012 03:46

Yipee !!

I have got it..
Just a small line to be added ..
--------------------------------------------------------------------------------------------
for /f "delims=" %%a in ('dir /b/a-d "C:\TFSworkspace\Scripts"') do (
if exist "C:\TFSworkspace_pre\Scripts\%%a" (
fc "C:\TFSworkspace\Scripts\%%a" "C:\TFSworkspace_pre\Scripts\%%a" >> compare_scripts.log
if errorlevel 1 echo %%a >>modifed_scripts.log
) else (
echo %%a >> missing_scripts.log
)
)
--------------------------------------------------------------------------------------------

I added :

if errorlevel 1 echo %%a >> modifed_scripts.log

Post Reply