?
Solved

(DOS BATCH) How do i use the COMP command correctly to write the name of a differing file to a logfile.txt

Posted on 2005-05-09
8
Medium Priority
?
1,502 Views
Last Modified: 2008-02-01
Hi, we are currently having a problem with a batch file that we are writing. The batch file compares 2 directory's and if a file in directory B is of a different  size or missing then the missing or altered file's name is written to the log file. We find that when a file is missing, the file's name is logged because when using the COMP command to search for "can't find/open" DOS see's -- "can't find/open FILENAME" and so writes the name of the file to the log. But when you search for "files are different sizes" using the COMP command - DOS only see's -"files are different sizes." This is because the name of the file is printed on screen before the text "files are different sizes". how can i change this code so that the second COMP command also writes the file names? This is the code that needs to be changed.

ECHO Comparing both root directory's for missing files ...
ECHO N | COMP %DIRA% %DIRB% /L | find /I "can't find/open" >>%LOGFILE%
:: Report findings
IF ERRORLEVEL 1 ECHO Files are identical
IF NOT ERRORLEVEL 1 GOTO COPYDIR
ECHO Comparing both root directory's for changes ...
ECHO N | COMP %DIRA% %DIRB% /L | find /I "files are different sizes" >>%LOGFILE%
:: Report findings
IF ERRORLEVEL 1 ECHO Files are identical
IF NOT ERRORLEVEL 1 GOTO COPYDIR
0
Comment
Question by:zoomzoomdevelopers
  • 4
  • 2
6 Comments
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13960122
Hello zoomzoomdevelopers,

Give this a go:


SETLOCAL ENABLEDELAYEDEXPANSION
ECHO Comparing both root directory's for missing files ...
ECHO N | COMP %DIRA% %DIRB% /L | find /I "can't find/open" >>%LOGFILE%
:: Report findings
IF ERRORLEVEL 1 ECHO Files are identical
IF NOT ERRORLEVEL 1 GOTO COPYDIR
ECHO Comparing both root directory's for changes ...
SET Line=
FOR /F "delims=" %%L in ('ECHO N ^| COMP %DIRA% %DIRB% /L') DO CALL :PROCESS "%%L"
GOTO :SKIP

:PROCESS
if /i %1=="Files are different sizes."
(
   ECHO %Line% >>%LOGFILE%
) ELSE (
   SET Line=%~1
)
EXIT /B

:SKIP
:: Report findings
IF ERRORLEVEL 1 ECHO Files are identical
IF NOT ERRORLEVEL 1 GOTO COPYDIR

0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13960218
Sorry, the logic there is a little incomplete. Give this a go instead:

SETLOCAL ENABLEDELAYEDEXPANSION
ECHO Comparing both root directory's for missing files ...
ECHO N | COMP %DIRA% %DIRB% /L | find /I "can't find/open" >>%LOGFILE%
:: Report findings
IF ERRORLEVEL 1 ECHO Files are identical
IF NOT ERRORLEVEL 1 GOTO COPYDIR
ECHO Comparing both root directory's for changes ...
SET Line=-1
FOR /F "delims=" %%L in ('ECHO N ^| COMP %DIRA% %DIRB% /L') DO CALL :PROCESS "%%L"
GOTO :SKIP

:PROCESS
if /i "%~1"=="-1" (
   set line=%~1
)

if /i "%~1"=="Files are different sizes." (
   echo %line%>>%LOGFILE%
) else (
   set line=%~1
)
EXIT /B

:SKIP
:: Report findings
IF ERRORLEVEL 1 ECHO Files are identical
IF NOT ERRORLEVEL 1 GOTO COPYDIR
0
 

Author Comment

by:zoomzoomdevelopers
ID: 13960521
Hi InteractiveMind,

Thanks for your answer as it does write the names of the altered files to the log but there is 1 problem.

1: when i run the BATCH it does'nt detect that any files are different sizes so it doesnt call COPYDIR even though it writes to the log file.

Any ideas
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Accepted Solution

by:
InteractiveMind earned 2000 total points
ID: 13960589
Okay, it would appear to be down to errorlevels. I've simulated an Errorlevel, with use of a Flag variable. Hopefully this does it:


SETLOCAL ENABLEDELAYEDEXPANSION
ECHO Comparing both root directory's for missing files ...
ECHO N | COMP %DIRA% %DIRB% /L | find /I "can't find/open" >>%LOGFILE%
:: Report findings
IF ERRORLEVEL 1 ECHO Files are identical
IF NOT ERRORLEVEL 1 GOTO COPYDIR
ECHO Comparing both root directory's for changes ...
SET Line=-1
SET FLAG=0
FOR /F "delims=" %%L in ('ECHO N ^| COMP %DIRA% %DIRB% /L') DO CALL :PROCESS "%%L"
GOTO :SKIP

:PROCESS
if /i "%~1"=="-1" (
   set line=%~1
)

if /i "%~1"=="Files are different sizes." (
   echo %line%>>%LOGFILE%
   SET FLAG=1
) else (
   set line=%~1
)
EXIT /B

:SKIP
:: Report findings
IF %FLAG%==0 ECHO Files are identical
IF %FLAG%==1 GOTO COPYDIR

0
 

Author Comment

by:zoomzoomdevelopers
ID: 13960760
Ok, im in work next wednesday so i'll test it with the team then and hopefully it will work, thanks for your help
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13960781
Sure. Good luck with it.

Rob.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Simple Linear Regression
Introduction to Processes

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question