Search code examples
batch-filefilesizemovefile

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


Appreciate anyone's help. I am quite stuck on this.

I am writing a bat script to move files from one folder to anther folder. (this part is simple enough)

However, before actually moving the file, I'd like to check whether the size of the file changes or not every 45 seconds. If the last size of the file is equal to the current size, then finally will move it. (Reason for this check is because the file in the source folder might still be in progress writing and we don't want to move it until it is finished)

Please help, I am open to suggestions

Currently have this code and am stuck:

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%

Solution

  • I have placed comments in the code to explain what it is doing.

    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