a bat file to search & move more than 1 file

May 18, 2009 at 23:23:09
Specs: Windows XP, 3.4

hi and thanks for your great site
i need a batch file (.bat file) to do following works
search current folder to find .txt files and if find them follow the orders
1- if find just 1 txt file == nothing happen it can go to next line or exit
2- if find 2 txt files ==it should find the smallest file (in size) and move it to myhome folder
3- if it find 3 or more txt files ==
first move the smallest file to myhome
then find the bigest and oldest txt file betwen them and keep it
then move the other ones
4- if find more than 3 file== it can follow previous order untill just 1 file remain

See More: a bat file to search & move more than 1 file

Report •

May 19, 2009 at 00:36:30
Your #3 needs clarification.
If there are exactly 3 and you move 1, biggest & oldest
doesn't mean much.

For starters, here's how you count the txt files.

@echo off & setLocal EnableDelayedExpansion

set N=0

for /f "tokens=* delims= " %%a in ('dir/b *.txt') do (
set /a N+=1

echo file count !N!

If at first you don't succeed, you're about average.


Report •

May 19, 2009 at 04:55:49
If you can afford to use languages like Python (on windows), here's an example script
import os
import sys
import shutil #module to move/copy files
import operator

currentfolder = os.path.join("c:\\","test")
destinationfolder = os.path.join("c:\\","tmp")
for r,d,f in os.walk(currentfolder):
    fileparams = []
    for files in f:
        if files.endswith(".txt"):
    if filecount==1: sys.exit()
    if filecount==2:
        smallestfile = sorted(fileparams, key=operator.itemgetter(1))[0]
        print "Moving to destination..."
        print smallestfile[0]
    if filecount >=3:

Report •

May 19, 2009 at 05:31:58
dear Mechanix2Go

# 3 says=
if there where 3 or more txt files all the files should be moved to myhome directory but one of them should be remain.. that file should be oldest from date modified and biggest from size
in this case if there where more than 1 file that had the same modified date bigger size can help
and by the way in some folders i have more than 200 txt file so this algoritm should work until the last file remains
please help thanks

dear ghostdog
i cant use Python i need batch code
thanks but my program doesnt suport Python
i am waitting

Report •

Related Solutions

May 19, 2009 at 21:04:38
any other help please?

Report •

May 22, 2009 at 01:31:29
ok i found a way please help me to finish it
or /f "tokens=* delims=" %%f in ('dir /b /o-s %%~i *.txt') do set SmallestFile="%%f"
move %smallestfile% myhome

this code moves the smallest .txt file to myhome folder so remains bigest
but problem is that if in my current folder there was just even 1 txt file it will move it to myhome
i want if there was more than 1 file operation begin and if there was 1 file it should be exit
operation should continue to just 1 file remain that file should be the biggest txt file

Report •

May 22, 2009 at 07:09:13
Sorry about the delay don't do this often.
So to include your piece but I have not included the %%~i if you need this put it in again.

@echo off
for /f "tokens=1 delims= " %%a in ('dir *.txt ^| find "File(s)"') do set var=%%a
if %var% lss 2 goto eof
for /f "tokens=*" %%f in ('dir /b /o-s *.txt') do set smallestfile="%%f"
move %smallestfile% myhome

Report •

May 22, 2009 at 14:21:11
wow my dear Shaka
thank you its working very nice
thanks again

Report •

Ask Question