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
If statement compare two files.
Moderator: DosItHelp
Re: If statement compare two files.
You could use a for/f loop to store the resulting output into a variable if that is what you want (untested):
Another option were to use the errorlevel (probably 1 if files are different, 0 else; untested):
penpen
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"
)
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.
)
Re: If statement compare two files.
Penpen, thank you! You're a life saver. This works perfectly.
Re: If statement compare two files.
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.
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.
Give it a go:
Steffen
Code: Select all
echo n|comp sample1.txt sample2.txt