首页 > 解决方案 > 如何使用bat脚本检查文件夹中文件的大小并移动它

问题描述

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

我正在编写一个 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%

标签: batch-filefilesizemovefile

解决方案


我在代码中添加了注释来解释它在做什么。

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

推荐阅读