Link to home
Create AccountLog in
Avatar of agawel
agawel

asked on

Compare Sizes of Files using Batch Scripting

This is my problem:  I have these two (2) files to process daily:

06/12/2007  03:20 AM        25,243,829 DATAFILE_06.12.2007_03.20.31.txt
06/12/2007  03:37 AM        59,481,843 DATAFILE_06.12.2007_03.37.47.txt

If you notice, the file names are almost exact, except for the "time" part at the end, which changes daily so the only way to tell which file is which is based on size.

This is the conditions:  One file will always be larger then the other.  These are the only two (2) files with this "name"..

Using a batch file how can I figure out which file is which based on size?

TIA :-)

PS..I was thinking of pulling in these file name into variables then comparing based on size to determine how to process which file..Is something like this possible?
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

If you simply want to find out the newest file I'd recommend this:

@echo off

setlocal

for /f "tokens=*" %%a in ('dir /b /a-d /o-d "DATAFILE_*.txt" 2^>NUL') do set fileName=%%a&goto FOUNDIT

echo Couldn't find files
goto :EOF

:FOUNDIT

echo %fileName% is newest
ASKER CERTIFIED SOLUTION
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
I'm confused. The filenames obey a standard datetimestamp format. The most recent filename (not based upon time, but simply sorting the filenames in descending order and taking the last one) would be enough.

"the only way to tell which file is which is based on size." I don't understand this.

You want the most recent file per day based upon the file NAME (i.e. 03.20.31 is older than 03.37.47).

SteveGTR, nice use of &.

I've not seen that. My solution was to do them in the reverse order, and simply overwrite the previous set, leaving you with the most recent one at the top.

But &GOTO like that is excellent.

OOI. Is this documented anywhere in windows? I know most of the cmdline stuff is in IF/FOR/SET, but I don't think I've ever seen & used like this.
cmd /? has some information on that.
Aha! Thanks