copy larger file with a batch file

Dell / Dell dxp051...
May 3, 2010 at 16:34:34
Specs: Microsoft Windows XP Professional, 2.793 GHz / 2046 MB
I need to write a script that will copy the larger of 2 files to another directory. The names will be different each time, so this will be based solely on file size. If they can't be compared to each other, then the smaller will be less than 1MB and the other above 2MB. I've tried some of the scripts I've found online, but the only one that did anything noticeable copied both files.
The latest I was trying is

@echo off & setLocal EnableDelayedexpansion

for /f "tokens=* delims= " %%a in ("*.txt") do (
set Z+=%%~Za
if !Z! gtr 2000000 (
copy *.text c:\experiment\1

I'll probably be doing some more of this, so any suggestions on a good place to learn more would be helpful too.

See More: copy larger file with a batch file

Report •

May 3, 2010 at 16:39:31
for %%a in (*.txt) do if %%~Za gtr 2097152 copy "%%a" "c:\experiment\1"

This assumes "c:\experiment\1" exists.

Report •

May 3, 2010 at 16:46:13
I created the folder and subfolder just to try out the batch files. I copied that into a test batch file and adjusted the size for the dummy files I had created and it worked (copying only the larger). I'll be able to use this, but for personal edification, is there a way to compare the file sizes or just use a set file size to compare against?

Report •

May 3, 2010 at 16:56:37
It's possible, but involves more logic than I typically trust to batch files.

Report •

Related Solutions

May 3, 2010 at 18:45:59
"I need to write a script that will copy the larger of 2 files "

@echo off & setLocal EnableDELAYedeXpansion

pushd d:\test
for /f "tokens=* delims= " %%a in ('dir/b/a-d/o-s') do (
copy %%a d:\out
goto :eof

Helping others achieve escape felicity


Report •

May 4, 2010 at 02:32:17
Version XP26 of Robocopy has got these options:

/MAX:n :: MAXimum file size - exclude files bigger than n bytes.
/MIN:n :: MINimum file size - exclude files smaller than n bytes.

It's a bit silly to re-invent the wheel every day, while there are tools pre-created to do that. Robocopy is not standard on XP, but you can download from MS sites, and it is standard in Vista and 7 (meaning it's not another 3rd party tool).

My advice is to actually test which file is biggest, not relying one of them being bigger than 2 MB, but that is just my idea.

Report •

Ask Question