Need to move any given quantity of files from a given type (csv in example) from ./source to a ./target folder and that:
- the total size of all files moved is below a given value SrcMax
- files are moved as a pair, so "twin" stay together
file on source folder are like this:
d:\source\base1-yyyymmddhhmm-data.csv
d:\source\base1-yyyymmddhhmm-flag.csv
d:\source\base2-yyyymmddhhmm-data.csv
d:\source\base2-yyyymmddhhmm-flag.csv
The requirement is to move the "data" and respective "flag" files together. They are created with the same "date modified", so the loop can be run based on this date, and all files will be shorted properly as a pair. The "flag" file is really small (less than 200 KB) when compared with the "data" (less than 100 MB).
The code so far, that moves all files, or doesn't anything at all, with issue seems to be how the loop accumulates the file size to compare with the SrcMax.
set DataLoc=d:\target
Set HoldLoc=d:\source
set SrcCount=0
set SrcSize=0
REM Set 2MB total volume to move
set SrcMax=2000000
FOR /F "TOKENS=*" %%a IN ('dir /A-D /O-D /B "%HoldLoc%"\*.csv') DO (
SET /A SrcCount += 1
SET /A SrcSize+=%%~za
oddn=%SrcCount% %% 2
if !SrcSize! LEQ %SrcMax% && !oddn!==0 (
MOVE /y "%HoldLoc%\%%a" "%DataLoc%"
)
)
this Batch to move files based on total size provides some guidance, but still missing something
This is working for now, except the counter to ensure that "twin" files are moved together.
ISSUE: when move the .bat file to another location, maintaining the same code, it stops working. Why?