0

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
smclintock
  • 91
  • 3
  • 14
  • 1
    Wouldn't it be better to check for current write access (e.g. as explained in [this SO thread](http://stackoverflow.com/a/10520609/1947205)) rather than relying on some file size limitations (which is very risky)? – Philip Allgaier Mar 13 '14 at 13:48

1 Answers1

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).

Community
  • 1
  • 1
Philip Allgaier
  • 3,505
  • 2
  • 26
  • 53