如何使用bat脚本检查文件夹中文件的大小并移动它

How to use bat script to check for the size of a file in the folder and move it

感谢任何人的帮助。我很坚持这一点。

我正在编写一个 bat 脚本来将文件从一个文件夹移动到另一个文件夹。 (这部分够简单了)

但是,在实际移动文件之前,我想每 45 秒检查一次文件大小是否发生变化。如果文件的最后大小等于当前大小,则 finally 将移动它。 (进行此检查的原因是源文件夹中的文件可能仍在写入中,我们不想在完成之前移动它)

请帮忙,我愿意接受建议

目前有此代码但卡住了:

for %%a in (%_fromFolder%\*%_fileName%*%_extension%) DO (
echo %%~za
set "fname=%~1"
set fSize = %%~za
timeout /t 45 /nobreak
:loop
if fSize == %%~za (
    echo %fSize%
    echo %%~za
    goto :moveF
) else (
    set fSize =%%~za
    timeout /t 45 /nobreak
    goto :loop
)

)

:moveF
move %~1 %_toFolder%

我在代码中放置了注释来解释它在做什么。

REM Get list of files
for %%G in ("%_fromFolder%\*%_fileName%*%_extension%") DO (
    REM Call function to check for the size of the file
    CALL :CHECK "%%G"
    REM Back from the Function. now move the file
    move "%%~G" "%_toFolder%"
)

GOTO :EOF

:CHECK
REM set the size of the file to a variable
set "fsize=%~z1"
REM delay program
timeout /t 45 /nobreak
REM check to see if the file size is the same. IF it is then leave the function
if "%fSize%"=="%~z1" GOTO :EOF
REM Go back to the check if the file size is not the same
GOTO CHECK