简体   繁体   中英

Batch script to copy latest file larger than 700MB

I'm trying to write a batch script that will copy the latest file created in a directory that is larger than 700MB. I require the file size because I don't want to copy a file that is currently being created, thus is smaller than a typical backup file.

Would someone be able to modify the script below to include the >700MB requirement?

Thanks!

:: Erase the content of the "sql_individual_backup" directory
cd "C:\Backup\sql_individual_backup"
del *.* /Q

:: Start to copy over the most recent file
@echo off

set "source=C:\Backup\sql_hourly"
set "dest=C:\Backup\sql_individual_backup"

pushd "%source%" ||(
echo.Source does not exist&pause&goto EOF)

for /f "tokens=*" %%f in (
'dir /A-D /OD /B') Do set "file=%%f"

popd

xcopy /d /i "%source%\%file%" "%dest%\"

:: Close the command prompt window
exit 0

This SO answer outlines how to get the size of a file in a batch script. With that information you should be able to extend your script to meet your requirement.

@echo off
setlocal
set file="test.cmd"
set maxbytesize=1000

FOR /F "usebackq" %%A IN ('%file%') DO set size=%%~zA

if %size% LSS %maxbytesize% (
    echo.File is ^< %maxbytesize% bytes
) ELSE (
    echo.File is ^>= %maxbytesize% bytes
)

But as I already mentioned in the comments, I would not rely on a fixed file size, but rather check if the file is currently written to = is locked, as outlined in this SO thread ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM