Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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 EnableDelayedExpansionset 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.M2

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): filecount=0 fileparams = [] for files in f: if files.endswith(".txt"): fileparams.append([os.path.join(r,files),os.path.getsize(os.path.join(r,files))]) filecount=filecount+1 if filecount==1: sys.exit() if filecount==2: smallestfile = sorted(fileparams, key=operator.itemgetter(1))[0] print "Moving to destination..." print smallestfile[0] shutil.copy(smallestfile[0],destinationfolder) if filecount >=3: #continue

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

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% myhomethis 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

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
%0

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |