Page 1 of 1

If statement compare two files.

Posted: 30 Oct 2019 15:57
by jmair
I need a quick way to see if there is a different file, and if there is to copy said file and replace the first file.
The below works fine, however i'm not sure how to add the return into a variable. Any advice would be welcomed.
comp sample1.txt sample2.txt /m

Re: If statement compare two files.

Posted: 31 Oct 2019 02:49
by penpen
You could use a for/f loop to store the resulting output into a variable if that is what you want (untested):

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
for /f "skip=1 tokens=* delims=" %a in ('comp sample1.txt sample2.txt /m') do (
	set "line=%~a"
)
Another option were to use the errorlevel (probably 1 if files are different, 0 else; untested):

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
comp sample1.txt sample2.txt /m
if "%errorlevel%" == "1" (
	echo Do something if files are different.
) else if "%errorlevel%" == "0" (
	echo Do something if files are euqal.
)
penpen

Re: If statement compare two files.

Posted: 31 Oct 2019 07:03
by jmair
Penpen, thank you! You're a life saver. This works perfectly.

Re: If statement compare two files.

Posted: 31 Oct 2019 15:00
by jmair
Do you know if there is a way to NOT prompt in Windows 7? Win7 doesn't seam to have a /M argument with that version of comp.
I have 400 local machines (non domain) I need to put a batch file on to compare files and unfortunately about half of them are old windows 7 thin clients.

Re: If statement compare two files.

Posted: 31 Oct 2019 15:12
by aGerman
Give it a go:

Code: Select all

echo n|comp sample1.txt sample2.txt
Steffen