批次:比较文件大小 [英] Batch: Compare File Sizes

查看:80
本文介绍了批次:比较文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了几种使用COMP,FC和其他几种比较文件的方法,但是我需要做的是基本文件大小的比较,然后对文件进行本地复制(如果文件较大),到网络文件.

I have seen a few different ways to compare files, using COMP, FC and a few others, but what I need to do is a basic file size compare, and then do a local copy of the file if it is larger, to the network file.

文件可以是200kb到5gb之间的任何文件.所以我真的只对文件大小而不是内容感兴趣.

The files could be anything from 200kb and 5gb. So I am really only interested in the file size and not content.

我希望基本的Dos脚本通过登录脚本运行.

I would prefer a basic Dos script to run through a login script.

Check local file A
Check remote file B
If file A is bigger than file B
  Copy B to A

推荐答案

FOR循环扩展 z 将返回文件的大小(例如%〜zl ).延迟变量扩展是此脚本的要求.仅当文件较大时,才使用此特定脚本进行复制,但是如果将文件大小更改为完全不同,只需将 GTR 更改为 NEQ ,就可以轻松地将其更改为复制文件.

The FOR loop expansion z will return the size of a file (e.g. %~zl). Delayed variable expansion is a requirement for this script. This particular script only copies if the file is larger, but you could easily change it to copy the file if the size is at all different by changing GTR to NEQ.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET REMOTE_DIR=\\SERVER\share\path\to\files
SET LOCAL_DIR=.

FOR /F %%f IN (files_to_compare.txt) DO (
    SET "REMOTE_FILE=%REMOTE_DIR%\%%~nxf"
    SET "LOCAL_FILE=%LOCAL_DIR%\%%~nxf"
    FOR %%r IN (!REMOTE_FILE!) DO (
        @ECHO [REMOTE] %%~nxr: %%~zr
        FOR %%l IN (!LOCAL_FILE!) DO (
            @ECHO [LOCAL] %%~nxl: %%~zl
            IF %%~zr GTR %%~zl (
                @ECHO %%~nxr: Remote file IS larger [%%~zr] ^> [%%~zl]
                COPY /Y "%%~r" "%%~l"
            ) ELSE (
                @ECHO %%~nxr: Remote file is not larger [%%~zr] ^<= [%%~zl]
            )
        )
    )
)

这篇关于批次:比较文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆