2

I have this simple batch script which copies the newest file in some dir to another place. I want this script to also ignore all files (in the DIR command section) under 1GB of size.

FOR /F "delims=|" %%I IN ('DIR "Y:\DEVL\*.*" /B /A-D') DO SET NewestFile=%%I 

copy "Y:\DEVL\%NewestFile%" "F:\DEVL\%NewestFile%"

Any help would be very appreciated.

quanta
  • 51,413
  • 19
  • 159
  • 217
Yoffe
  • 53
  • 2
  • 7

1 Answers1

2

Option 1: If you want to copy the newest file, but only if it's greater than 1GB, just replace your copy command with:

robocopy "Y:\DEVL" "F:\DEVL\" "%NewestFile%" /min:1073741824

/min:n defines the minimum size in bytes.

Option 2: If you want to copy the newest file of all files that are greater than 1GB, it's more complicated.

@echo off

Set CopyResult=0
FOR /F "delims=|" %%I IN ('DIR "Y:\DEVL\*.*" /B /O:-d /T:w /A-D') DO Call :DoCopy "%%I"
Goto :EOF

:DoCopy
  IF %CopyResult%==1 Goto :EOF
  set CurrentFile=%1
  robocopy "Y:\DEVL" "F:\DEVL" %CurrentFile% /njs /njh /is /min:107374182
  Set CopyResult=%errorlevel%

/min:n defines the minimum size in bytes.

ZEDA-NL
  • 846
  • 1
  • 6
  • 13
  • That will only skip the copying if the file is less than 1gb. It will not, as I think the OP wants, copy the newest file that IS less than 1gb. – Glenn Sullivan Feb 27 '13 at 15:13
  • Glenn: Thanks, I guess you can read the question two ways. I updated my answer. – ZEDA-NL Feb 28 '13 at 11:32
  • ERROR : Invalid Parameter #3 : " /min:1073741824" And it took the wrong file :) The script now runs over all files, taking the actually newest (after listing all files as newest one after the other) - a 70kb file. – Yoffe Mar 04 '13 at 06:33
  • Hi Zeda, Again I'm not receiving any successful results here. Please try and run your script and see what happens – Yoffe Mar 18 '13 at 09:37
  • About the error. If you remove @ECHO OFF command you'll see the actual cammand that is executed. Can you copy that in a comment for me? – ZEDA-NL Mar 18 '13 at 15:06
  • If it took the wrong file the sorting of the dir command is not correct. I edited the script and included /O:-d (order by date, newest first) and /t:w (date for sorting = date last written, use /t:w if you want the creation date). – ZEDA-NL Mar 18 '13 at 15:23