比较 2 个文件,如果与目标不同,则复制源 - vbscript?
我正在使用 Windows XP,我需要制作一个脚本来比较 2 个文件(1 个在服务器上,1 个在客户端上)。基本上,我需要我的脚本来检查客户端的文件是否与服务器版本不同,如果发现差异(在文件本身,而不仅仅是修改日期),则替换客户端版本。
I'm working on Windows XP and I need to make a script that would compare 2 files (1 on a server and 1 on a client). Basically, I need my script to check if the file from the client is different from the server version and replace the client version if it finds a difference (in the file itself, not only the modification date).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所建议的,您可以跳过日期检查,因为可以在不更改内容的情况下更改日期检查。
首先检查尺寸是否不同。如果是这样,可能足以得出结论:它们是不同的。不过,根据文件类型的不同,这也可能会产生误报。例如,unicode 文本文件可能包含与 ansi 文本文件完全相同的内容,但每个字符使用两个字节进行编码。如果它是一个脚本,它将以完全相同的结果执行,但大小是原来的两倍。
如果大小相同,它们仍可能包含不同的字节。强力测试是将每个文件加载到一个字符串中并比较它们是否相等。如果它们是大文件,并且如果没有必要,您不想将它们全部读入内存,那么请逐行读取它们,直到遇到差异。假设它们是文本文件。如果它们不是文本文件,您可以通过以固定大小的块读取它们并进行比较来执行类似的操作。
另一种选择是对两个文件运行“fc”文件比较命令并捕获结果并基于该结果进行更新。
As you suggest, you can skip the date check as that can be changed without the contents changing.
First check that the sizes are different. If so, that may be enough to conclude that they are different. This can have false positives too though depending on the types of files. For example a unicode text file may contain the exact same content as an ansi text file, but be encoded with two bytes per character. If it's a script, it would execute with exactly the same results, but be twice the size.
If the sizes are the same, they may still contain different bytes. The brute force test would be to load each file into a string and compare them for equality. If they are big files and you don't want to read them all into memory if not necessary, then read them line by line until you encounter a difference. That's assuming they are text files. If they aren't text files, you can do something similar by reading them in fixed size chunks and comparing those.
Another option would be to to run the "fc" file compare command on the two files and capture the result and do your update based on that.