Help - How to process each target file in order of size ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Help - How to process each target file in order of size ?

#1 Post by alan_b » 30 Oct 2013 14:41

My script launches action by routine ":MY-C" upon each file with a folder "W:\SpeedTest\" with this :-

Code: Select all

FOR %%f IN ("W:\SpeedTest\*.*") DO CALL :My-C "%%f" 

I am now into "brain fade" after debugging ":My-C" which is now working well.

Unfortunately the files are being processed in the wrong sequence.
I would like to process them in order of size -smallest file first, largest file last (though I could live with the opposite sequence)
Please tell me how to achieve this by fixing or replacing the the initial

Code: Select all

FOR %%f IN ("W:\SpeedTest\*.*") ...


Regards
Alan

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Help - How to process each target file in order of size

#2 Post by foxidrive » 30 Oct 2013 15:11

Use something like this:

Code: Select all

pushd "W:\SpeedTest\"
for /f "delims=" %%f in (' dir /b /os /a-d ') do CALL :My-C "%%f"
popd

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Help - How to process each target file in order of size

#3 Post by alan_b » 30 Oct 2013 17:23

Many Thanks - that is working.

I assume the double quotes at the start of ""W:\SpeedTest\" is a harmless typo,
It seems to have no effect on operation if I retain or delete the excess ".

There is one strange side-effect that puzzles me.

My originally code can restrict its selection of files with, for example, the mask *.bin
e.g.

Code: Select all

FOR %%f IN ("W:\SpeedTest\*.bin") DO CALL :My-C "%%f"

the code :My-C is invoked whilst its current directory is C:\ or whatever the script happened to be launched with.

Your code has the side effect that My-C is executed with W:\SpeedTest\ as its current directory.

This adaptation of your code works as well as your original, and has the same side effect

Code: Select all

pushd ""W:\SpeedTest\"
for /f "delims=" %%f in (' dir "W:\SpeedTest\*.bin" /b /os /a-d ') do CALL :My-C "%%f"
popd

By removing the pushd and popd I remove the side effect

Code: Select all

for /f "delims=" %%f in (' dir "W:\SpeedTest\*.bin" /b /os /a-d ') do CALL :My-C "%%f"

unfortunately this somehow causes :My-C to misbehave.

Is there a simple fix that avoids the need for pushd and popd ?
If not I can live with it.

Again, many thanks.

Regards
Alan

Squashman
Expert
Posts: 3665
Joined: 23 Dec 2011 13:59

Re: Help - How to process each target file in order of size

#4 Post by Squashman » 30 Oct 2013 21:25

Does this help you understand.

Code: Select all

@echo off
FOR %%f IN ("C:\SpeedTest\*.bin") DO echo %%f

for /f "delims=" %%f in ('dir "C:\SpeedTest\*.bin" /b /os /a-d') do echo %%f

for /f "delims=" %%f in ('dir "C:\SpeedTest\*.bin" /b /os /a-d') do echo %%~dpnxf

pause



Output

Code: Select all

C:\SpeedTest\1.bin
C:\SpeedTest\2.bin
C:\SpeedTest\3.bin
2.bin
3.bin
1.bin
C:\Users\Squashman\Desktop\2.bin
C:\Users\Squashman\Desktop\3.bin
C:\Users\Squashman\Desktop\1.bin

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Help - How to process each target file in order of size

#5 Post by foxidrive » 31 Oct 2013 01:04

Good to see you back squashman.

I have to add that I get confused with %%~fa as it doesn't return the full path if you currently aren't in the folder. It returns the full path to the current folder.

Code: Select all

@echo off
for /f "delims=" %%f in (' dir "c:\files\*.jpg" /b /os /a-d ') do echo CALL :My-C "%%~ff"
pause


That above returns this, where the files are in c:\files folder:

CALL :My-C "d:\abc\28-10-2013 13-35-31.jpg"
CALL :My-C "d:\abc\keyboard with plate.jpg"
CALL :My-C "d:\abc\colo_1652_scan.jpg"
CALL :My-C "d:\abc\The Joke.jpg"
CALL :My-C "d:\abc\Swing.jpg"


But alan_b it can be worked around by using this, where you specify the path in two places.
Adding /s in the dir command is an option also if you don't have subdirectories in that folder.

Code: Select all

for /f "delims=" %%f in (' dir "W:\SpeedTest\*.bin" /b /os /a-d ') do CALL :My-C "W:\SpeedTest\%%~nxf"

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Help - How to process each target file in order of size

#6 Post by alan_b » 31 Oct 2013 02:24

Between waking up and getting out of bed this morning I was embarrassed to realise my brain had gone to sleep before I posted my last question. :(
I really must try to get my body away from the keyboard and into bed first :)

However, thank you both for rushing to my aid.

Also thank you for the additional insights you have now given me.

Regards
Alan

Post Reply