This script implement the following requirements.
1. Find files from specific folder and subfolder
2. Find files which has specific extension name
3. Filter the files modification date greater than yesterday
4. Filter the files by the file size greater than a specific size (10M etc.)
There are a few tricks for this script.
1. “Setlocal EnableDelayedExpansion” this declaration is must be set part, as if you use loop in the batch, and the variable inside the loop will only keep the first round value or the last round value because batch variable didn’t get at run time, so if you want to get it at run time, the top words need to be declared, and when try to reference the variable, ensure use !myvariable! not %myvariable%. More details can reference to http://ss64.com/nt/delayedexpansion.html
2. How to get the file properties, reference the following
FOR %%W IN (file_to_be_queried) DO (
ECHO File Name Only : %%~nW
ECHO File Extension : %%~xW
ECHO Name in 8.3 notation : %%~snW
ECHO File Attributes : %%~aW
ECHO Located on Drive : %%~dW
ECHO File Size : %%~zW
ECHO Last-Modified Date : %%~tW
ECHO Parent Folder : %%~dpW
ECHO Fully Qualified Path : %%~fW
ECHO FQP in 8.3 notation : %%~sfW
ECHO Location in the PATH : %%~dp$PATH:W
more details can reference to http://www.robvanderwoude.com/batchfiles.php
3. Batch for ftp. In batch file to run ftp command, either use vbscript or run a ftp command file. My script use the second way, “>” will overwrite the whole file, and “>>” will append text in the last line, so the ftp command file will be overwritten every time the batch run and generate the latest.
4. Batch command use byte to calculate the file size, so if the file size is 100GB, that number would exceed the batch int limit and complain errors. So if it does need to reference the big file size number, you can divide the file size by 1024*1024, then it should reduce the number.
The following is the script .
SET /A FileSizeLimit=10
ECHO Looking for files which size is less than %FileSizeLimit%M, file extention name is %Filter%, folder is %StartFolder%…..
SET /A m -= 1
ECHO %FTPUserName%> %FTPCommandFile%
ECHO %FTPPassword%>> %FTPCommandFile%
ECHO binary >> %FTPCommandFile%
ECHO cd %FTPPath% >> %FTPCommandFile%
FOR /R “%StartFolder%” %%F IN (%Filter%) DO (
SET /a realsize=%%~zF
SET /a filesizeinMB = !realsize! / %constvalue% / %constvalue%
ECHO filesize in MB: !filesizeinMB!
ECHO file name is: %%F
IF !filesizeinMB! GTR %FileSizeLimit% (
FOR %%p IN (%%F) DO (
IF !filedatetime! GEQ %yesterdaydate% (
ECHO put %%F >> %FTPCommandFile%
ECHO close >> %FTPCommandFile%
ECHO bye >> %FTPCommandFile%
FTP -d -i -s:%FTPCommandFile% %FTPServer%