Computing.Net > Forums > Programming > a bat file to search & move more than 1 file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

a bat file to search & move more than 1 file

Reply to Message Icon

Name: niki chan
Date: May 18, 2009 at 23:23:09 Pacific
OS: Windows XP
CPU/Ram: 3.4
Subcategory: Batch
Comment:


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



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: May 19, 2009 at 00:36:30 Pacific
Reply:

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.

M2


0

Response Number 2
Name: ghostdog
Date: May 19, 2009 at 04:55:49 Pacific
Reply:

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



0

Response Number 3
Name: niki chan
Date: May 19, 2009 at 05:31:58 Pacific
Reply:

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


0

Response Number 4
Name: niki chan
Date: May 19, 2009 at 21:04:38 Pacific
Reply:

any other help please?


0

Response Number 5
Name: niki chan
Date: May 22, 2009 at 01:31:29 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: Shaka
Date: May 22, 2009 at 07:09:13 Pacific
Reply:

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


0

Response Number 7
Name: niki chan
Date: May 22, 2009 at 14:21:11 Pacific
Reply:

wow my dear Shaka
thank you its working very nice
thanks again


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: a bat file to search & move more than 1 file

passing parameters to a bat file www.computing.net/answers/programming/passing-parameters-to-a-bat-file-/13094.html

questions on logging a bat file www.computing.net/answers/programming/questions-on-logging-a-bat-file/17927.html

add a .bat file to right click contents men www.computing.net/answers/programming/add-a-bat-file-to-right-click-contents-men/19169.html