Computer Hope
Microsoft => Microsoft DOS => Topic started by: am_rutapatil on September 20, 2011, 09:38:18 AM
-
Hi,
I have a list of files in a directory. I need to write code in .bat to find the largest file in the directory and rename it.
I am trying to use the below code
set FileSize=0
set Filename=
for %%a in (%Temp%abc*") do (
if %%~za GTR %FileSize% (
set FileSize=%%~za
set Filename=%%a
)
)
But this is not working out..
Can anybody give some suggestions.
Thanks
Amruta
-
I also tried to use
for %%a in ('dir /b /os E:Tempabc*') do (
But it is also not giving correct result.
-
Try this
dir /b /o-s>{temp}
set /P biggest=<{temp}
del {temp}
ren "%biggest%" the_biggest_file_here
-
Thank you so much...It worked..
I am trying since last 3-4 days...
Thank you so much once again...
BTW...what is temp here.. ?
-
ok, Ill explain here
dir /b /o-s>{temp}
create a directory listing of the directory, the /b means just the filename, the /o-s means sort the list with the biggest first
now send the list into a file called {temp} ....the curly brackets makes these files stand out in a directory listing
set /P biggest=<{temp}
set creates a variable, the /P means wait for the user to type something and press enter, BUT we are redirecting the input from a file, so it will start to read the temporary file, until it hits the first carriage return, when it will stop, the variable %biggest% now holds the first line of the file (which is why we sorted the listing biggest to smallest)
and thats it
-
here's another way that does not involve a temp file
for /f "delims=" %%A in ('dir /b /os') do set biggest=%%A
echo The biggest file is %biggest%
FOR /f .... treat the dataset (the stuff in the brackets) as a source of lines
"delims=" .... take all of each line
%%A .... the FOR loop variable
('dir /b /os') .... the FOR dataset. The single quotes mean "take the output of this command" line by line (because of /f). The dir sort order is smallest first, biggest last, so that %%A will successively hold each file name. We assign this to the variable %biggest% so that when the loop exits (after the last line output by dir) %biggest% holds the name of the last file in the list.
By using the various dir list order switches you can get the earliest or latest dated file, the smallest or largest, etc.
Developing it a bit...
%%~zA is the file size in bytes
for /f "delims=" %%A in ('dir /b /os') do set biggest=%%A & set size=%%~zA
echo The biggest file is %biggest% (%size%) bytes
example
The biggest file is X15-65732.iso (2376366352 bytes)
With a bit of batch arithmetic you could get the file size in GB MB or KB but batch arithmetic runs out of steam at 2G so I wouldn't like to suggest it really.
-
Salmon ...the solution is working fine...
Thanks for helping me out
-
i've a question.
this script is almost what i need but, it must not rename the file but open a explorer.exe
i need a script that check if a file is bigger then 100MB.
if have 10 folders that contains each 2 files (c:\backup\example1.bak & c:\backup\example2.bak)
each file must be bigger than 100MB
if the file "example.bak" is smaller then 100mb, it must open the explorer.exe, so i can check that pad.
if the size is bigger then i must check the next folder
i'm a noob with dosscript and read some script here, but must i use "if exist" or "errorlevel" ???
can somebody help me pls with a bat file ?
thx
-
i've a question.
Do NOT hijack somebody else´s thread. Start a new one.