I am trying to make my script which copies only files above 3KB from a folder to another copy only 20 files not all
for /r "downloads" %F in (*.mp4) do @if %~zF geq 3000 move "%F" "test"
this works but it copies all files. I found solutions for nested for loops but all those solutions also affect ALL files.
Is there even a way to copy only 20 files from the downloads folder above 3KB with windows batch script?. The file order or the name don't really matter, just 20 files above 3KB, in my case I work with short logo videos.
Any solution is appreciated! Sincerely Gram
CodePudding user response:
As mentioned by @Squashman in the comment, you just need to add a counter. Note, this is batch-file format and I had to double up on the %:
@echo off & set cnt=0
setlocal enabledelayedexpansion
for /R "downloads" %%F in ("*.mp4") do (
Rem # increment the variable cnt by one before processing each file
set /a cnt =1
Rem # test if the variable cnt is less than 21
Rem # if it is test if file size is less than 3KB
Rem # then execute the move command
if !cnt! lss 21 if %%~zF geq 3000 move "%%~F" "test"
)
I have added some remarks to the code to explain some of it. To find out more about what was done you can open. Just so you under stand the if statements. Both if's needs to return true else it will not move the file. So if the count is 21 or more it will stop processing files and it will also skip any files less than 3KB, as you already know.
To read up on delayedexpansion see set /? from cmd as well as setlocal /? and for /?.
