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.
for %%a in (*.txt) do if %%~Za gtr 2097152 copy "%%a" "c:\experiment\1"
This assumes "c:\experiment\1" exists.
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?
It's possible, but involves more logic than I typically trust to batch files.
"I need to write a script that will copy the larger of 2 files " ================================
@echo off & setLocal EnableDELAYedeXpansionpushd 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 felicityM2
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.
Yes (14) | ![]() | |
No (14) | ![]() | |
I don't know (15) | ![]() |